回 帖 发 新 帖 刷新版面

主题:请各位帮忙看看

Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 

Sample Input
2
1 2
112233445566778899 998877665544332211
 

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

本人是用DEV C++编译的,编译通过, 可是提交是编译出错,这是杭电ACM上面的题目,我试了很多次,在DEV C++编译运行是没错的,请各位大侠指点一下我,万分感谢啊,想了一天了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
        int T = 0;
        char A[1024]={};
        char B[1024]={};
        char C[1024]={};
        int j = 1;                                //控制输出格式的case的第几种情况 
        int m,n,length_A,length_B;
        int i = 0;
        int K = 0;                                //存放进位的标识,如果进位K=1,否则K=0
        char a[1024]={};
        char b[1024]={};
        while(1 == scanf("%d",&T))
        { 
                while(T--)
                {
                        scanf("%s %s",A,B);
                        
                        for(i = strlen(A)>=strlen(B)?strlen(A):strlen(B);i > 0 ;i--)                //一位位的模拟数学运算,有进行则用K记录 
                        {
                                A[i] = A[i - 1];
                                B[i] = B[i - 1];
                                if((A[i] - '0') + (B[i] - '0') < 10)
                                {        
                                        C[i] = (A[i] - '0') + (B[i] - '0') + K + '0' ;
                                        K = 0;                                        
                                }
                                if((A[i] - '0') + (B[i] - '0') >= 10)
                                {
                                        C[i] = (A[i] - '0') + (B[i] - '0') - 10 + K + '0';
                                        K = 1;
                                }
                        }
                        A[0] = '0';                        //在字符数组A首位补0 
                        B[0] = '0';                        //在字符数组B首位补0 
                        
                        length_A = strlen(A) - 1;
                        length_B = strlen(B) - 1;
                        
                        printf("Case %d:\n",j++);
                        if(i == 0 && A[1] -'0' + B[1] - '0' >= 10)                        //判断字符数组A首位和字符数组B首位相加是否有进位
                        {
                                C[0] = '1';                                                                                //首位进位 
                                for(m = length_A - 1;m >= 0;m--)                                    //对字符数组A和B进行首位去掉0的操作 
                                {
                                        a[m] = A[m + 1];
                                }
                                for(n = length_B - 1;n >= 0;n--)
                                {
                                        b[n] = B[n + 1];
                                }
                                if(T > 0)                                                                                    //对格式输出进行控制,在case情况下再输出一个换行 
                                {
                                        printf("%s ",a);
                                        printf("+");
                                        printf(" %s = ",b);
                                        printf("%s\n",C);
                                        printf("\n");
                                }
                                if(T <= 0)                                                                                //最后的一种case不用另行换行 
                                {
                                        printf("%s ",a);
                                        printf("+");
                                        printf(" %s = ",b);
                                        printf("%s\n",C);
                                }
                        }
                        else
                        {
                                C[0] = '\b';                                                                            //如果字符数组A首位和字符数组B首位相加不需要进位,则取消掉字符数组首位的0 
                                for(m = length_A - 1;m >= 0;m--)                                    //对字符数组A和B进行首位去掉0的操作
                                {
                                        a[m] = A[m + 1];
                                }
                                for(n = length_B - 1;n >= 0;n--)
                                {
                                        b[n] = B[n + 1];
                                }
                                if(T > 0)                                                                                    //对格式输出进行控制,在case情况下再输出一个换行
                                {
                                        printf("%s ",a);
                                        printf("+");
                                        printf(" %s = ",b);
                                        printf(" %s\n",C);
                                        printf("\n");
                                }
                                if(T <= 0)                                                                                //最后的一种case不用另行换行
                                {
                                        printf("%s ",a);
                                        printf("+");
                                        printf(" %s = ",b);
                                        printf(" %s\n",C);
                                }
                        }
                }        
        }
        return 0;
}

回复列表 (共1个回复)

沙发

我知道哪里错了。。
不是编译问题,是算法有问题,有个点没考虑到

我来回复

您尚未登录,请登录后再回复。点此登录或注册