总是使用C编程语言在uva 10008中获得WA [英] Always got WA in uva 10008 with C programming language

查看:72
本文介绍了总是使用C编程语言在uva 10008中获得WA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
        #include<string.h>
        
        int main()
        {
            
        /* input the number of the line */
            int n;
            scanf("%d",&n);
            fflush(stdin); 
        
            /* build an array to compute the number of times that each alphabet appears */
            /* countt[0] stands for 'a' , countt[1] stands for' b' ,and so on... */
            int countt[26];
            int i;
            for(i=0;i<26;i++)
            {
                countt[i]=0;
            }
        
            /*build an array storing the alphabet*/
            char alpha[26];
            char u;
            int numal=0;
            for(u='A';numal<26;u++)
            {
                alpha[numal]=u;
                numal++;
            }
        
            /*build an array prepared to receive the input(string) */
            char mmm[1000][1000];
            int t,g;
        
            for(t=1;t<=n;t++)
            {
                scanf("%[^\n]",mmm[t]);
                fflush(stdin);
            }
        
            /* compute each alphabet appears in the all strings , and the number of times is documented in array"countt" */
            for(t=1;t<=n;t++)
            {
                for(g=0;mmm[t][g]!='\0';g++)
                {
                    if(mmm[t][g]>='a' && mmm[t][g]<='z')
                    {
                        countt[mmm[t][g]-'a']++;
                    }
        
                    if(mmm[t][g]>='A' && mmm[t][g]<='Z')
                    {
                        countt[mmm[t][g]-'A']++;
                    }
                }
            }
        
            /* sort the array"countt" in ascending order with selection sort */
            /* sort the array"alpha" according to the array"countt" */
            int swap,position;
            int o;
            char swap1;
            for(i=0;i<=24;i++)
            {
                for(o=i+1;o<=25;o++)
                {
                    position=i;
                    if(countt[i]<countt[o])
                        position=o;
                    if(position==o)
                    {
                        swap=countt[i];
                        countt[i]=countt[o];
                        countt[o]=swap;
        
                        swap1=alpha[i];
                        alpha[i]=alpha[o];
                        alpha[o]=swap1;
                    }
                }
            }
            
            /* if the number of time that alphabets appear in the strings is equal ,then order in dictionary form (G is front of I)*/
            
            char swap2;
            for(i=0;i<=24;i++)
            {
                for(o=i+1;o<=25;o++)
                {
                    if(countt[i]==countt[o])
                    {
                        if(alpha[i]>alpha[o])
                        {
                            swap2=alpha[i];
                            alpha[i]=alpha[o];
                            alpha[o]=swap2;
                        }
                    }
                }
            }
        
        
        /* display the result*/
            for(i=0;i<26;i++)
            {
                if(countt[i]!=0)
                {
                    printf("%c %d\n",alpha[i],countt[i]);
                }
            }
        
        
        
        
            return 0;
        }



以上是我的代码(回答UVa 10008)。

我总是得到结果,错误答案,但我使用调试功能(https://www.udebug.com/UVa/10008)检查我的答案,总是与接受的输出相同。



我没有想法是什么问题。

谢谢。



我尝试了什么:



我试图调试显示我的答案是正确的,但是当我提交给UVa时,我得到了WA。


Above is my code (answer to UVa 10008).
I always got the result,Wrong Answer,but I used the Debug Function(https://www.udebug.com/UVa/10008)to check my answer,always identical to the accepted output.

I have no idea what the problem is.
Thanks.

What I have tried:

I have tried to debug that display my answer is correct,but when I submitted to UVa ,I got WA.

推荐答案

此任务由您决定使用调试器来查找它。检查关键区域的数据。使用Visual Studio的内存调试程序。



提示:

a)第一步应该是在关键代码行之前添加更多输出。

b)A好的方法也是重构一些代码行到函数中并用准备好的数据编写测试。就像从文件加载mm数据一样。这也有助于避免输入错误。

c)为预期结果编写控制函数

d)仔细听编译警告,当使用char溢出时可能会欺骗你; - )
This task is up to you to use the debugger to find it out. Inspect the data at the critical areas. Use the memory debugger of Visual Studio.

Tips:
a) First step should be to add more output before critical code lines.
b) A good approach is also the refactor some code lines into functions and write tests with prepared data. Like loading from file for the mm data. This also helps avoiding input errors.
c) write control function for the expected results
d) listen carefully to compiler warnings, when working with char overflows may fool you ;-)


这篇关于总是使用C编程语言在uva 10008中获得WA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆