当前位置: 代码迷 >> C语言 >> HDOJ上的A+B题,应该没问题的,怎么就是通不过?
  详细解决方案

HDOJ上的A+B题,应该没问题的,怎么就是通不过?

热度:174   发布时间:2007-11-24 21:16:29.0
HDOJ上的A+B题,应该没问题的,怎么就是通不过?

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

21 2112233445566778899 998877665544332211


Sample Output

Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110我的答案是:#include<stdio.h>
int main()
{
__int64 A,B;
int T,i=1;
while((scanf("%d",&T))!=EOF)
{
  for(i=0;i<T;i++)
  {
   scanf("%I64d%I64d",&A,&B);
   printf("Case %d:\n",i+1);
   printf("%I64d + %I64d = %I64d\n\n",A,B,A+B);
  }
}
return 0;
} 还有就是:#include<stdio.h>
int main()
{
int T,i,j;
__int64 A[20],B[20];
while((scanf("%d",&T))!=EOF)
{
  for(i=0;i<T;i++)
   scanf("%I64d%I64d",&A[i],&B[i]);
  for(j=0;j<T;j++)
  {
   printf("Case %d:\n",j+1);
   printf("%I64d + %I64d = %I64d\n\n",A[j],B[j],A[j]+B[j]);
  }
}
return 0;
} 请大家多多指教,谢谢了
搜索更多相关的解决方案: HDOJ  

----------------解决方案--------------------------------------------------------
知道了
You may assume the length of each integer will not exceed 1000.的是意思是大数的位数要在1000位以内
题目理解错了   要用数组来做的
----------------解决方案--------------------------------------------------------
还是这个问题啊    要把每一位数字放入数组里的吧    应该怎么做呢??
设A[1000]   如何使输入200位数字后判断输入结束呢???
----------------解决方案--------------------------------------------------------
大数的加法啦.
----------------解决方案--------------------------------------------------------
看看这个可以不啦.输出格式自己改下.
程序代码:
#include<stdio.h>
#include<string.h>

typedef struct Big_num{
    int data[1001];
    int len;
  };
void char_num(Big_num &a,char *str)
{
    int i=0;
    a.len=strlen(str);
    for(i=0;i<a.len;i++)
    {
        a.data[i]=str[a.len-i-1]-'0';
    }
}

void Add_Big_Num(Big_num &a,Big_num &b)
{
    int len,i,temp=0;
    while(a.len<b.len)
    {
        a.data[a.len++]=0;
    }
    while(a.len>b.len)
    {
        b.data[b.len++]=0;
    }

    for(i=0;i<a.len;i++)
    {
        int flag=a.data[i]+b.data[i]+temp;
        a.data[i]=flag%10;
        temp=flag/10;
    }
    if(temp)
    {
        a.data[a.len++]=temp;
    }
}
        
        
int main()
{
    char str1[301],str2[301];
    Big_num a,b;
    while(EOF!=(scanf("%s%s",str1,str2)))
    {
         char_num(a,str1);
         char_num(b,str2);
         Add_Big_Num(a,b);
         while(--a.len>=0)
         {
            printf("%d",a.data[a.len]);
         }
         printf("\n");
    }
    return 0;
}

----------------解决方案--------------------------------------------------------
哦  谢谢了
----------------解决方案--------------------------------------------------------
恩  知道了   应该用字符串才行的啊
----------------解决方案--------------------------------------------------------
回复 5# 的帖子
为什么要写成str1[301],str2[301];这样呢???
为什么是301而不是1000???
----------------解决方案--------------------------------------------------------