A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 556634 Accepted Submission(s): 106071
Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.InputThe 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.OutputFor 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 Input21 2112233445566778899998877665544332211Sample OutputCase 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
这算是我第一次接触大数的算法吧 想了很久现在才勉强相通 我真的好笨www 现在来理一理自己的思路
题意简述:
首先输入一个数字T(1 <= T <= 20)代表输入的数据组数,其次下边有T行,每行有2个欲求和的数字,并且每个数字不超过1000位。
题意分析:
这是一道大数(高精度)数字计算的题目,unsigned long long double 是不能满足所需要的数字位数的,所以需要别的方法进行计算。
解题思路:
- 首先用2个字符串接受并储存要输入的2个加数;
- 去掉前导零以及要考虑全是0的情况; // 00000015215561261612612 000000000000000000000
- 按照ASCII的关系,让字符串的每个字符减去“字符0”,获得数值并且倒置存储在2个整数数组中;
- 模拟加法运算,并储存在另一个数组中;
- 按要求输出结果;
- 初始化各个数据,为下一组大数运算复位。
代码:
#include<stdio.h>
#include<string.h>
#define N 1010
#define max(x,y) (x>y?x:y)
int main()
{
// 把标准输入 更改为 文件输入
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// 好处:输入输出可以分开显示,以便核对对错,但是交的时候记得删掉
//声明时初始化数据,该方式只能在声明时初始化一次,不能定义之后再这样初始化了,要注意!
//声明大数a
char a1[N]={0}; //输入时用字符数组接收 a
int a2[N]={0};//计算时用整数数组进行运算
//声明大数b
char b1[N]={0}; //输入时用字符数组接收 b
int b2[N]={0}; //输入时用字符数组进行运算
int c2[N] = {0};//最终结果用c存储
int T,n1,n2,digit,k,m1,m2;
scanf("%d",&T);//测试数据组数
for(int i=1;i<=T;i++)//这里不用while循环是因为循环里需要用到i
{
//输入两个大数
scanf("%s%s",a1,b1);
//得出大数a1[]的:m1---有效起始位置, n1---有效长度(全0则长度为1,否则还是n1)
n1=strlen(a1);
int j;
for(j=0;j<n1;j++)//找有效起始位置m1
{
if(a1[j]!='0' )//找到第一个不为0的位置
{
m1=j;
break;
}
else if(a1[j]=='0' && j==n1-1)//如果找到最后一位仍然为0
{
m1=0;//起始位置是0
n1=1;//有效长度是1
break;
}
}
//按上述同样的步骤得出b2[]的有效起始位置与有效长度
n2=strlen(b1);
for(j=0;j<n2;j++)
{
if(b1[j]!='0' )
{
m2=j;
break;
}
else if(b1[j]=='0' && j==n2-1)
{
m2=0;
n2=1;
break;
}
}
//把字符数组形式的大数a即a1[] 提取成 整数形式的大数a即 a2[],同时把顺序颠倒一下
for(j=n1-1,k=0; j>=m1; j--,k++)
a2[k] = a1[j]-'0';
//把字符数组形式的大数b即b1[] 提取成 整数形式的大数b即 b2[],同时把顺序颠倒一下
for(j=n2-1,k=0; j>=m2; j--,k++)
b2[k] = b1[j]-'0';
//c2[] = a2[] + b2[]
digit = max(n1-m1,n2-m2);//最大位数(这里不直接用n是因为n的长度里可能包含前导0)
for(j=0; j<digit; j++)
{
c2[j] = a2[j] + b2[j] + c2[j];
c2[j+1] = c2[j]/10;//进位(注意:如果最后一位没有进位,将为多一个额外的0)
c2[j] = c2[j]%10;
}
//考虑最后一位是否有进位
if(c2[digit]==0)//如果最后一位没有进位,就不让额外的0所在位数参与下面的循环
digit--;
printf("Case %d:\n",i);
for(j=m1;j<n1;j++)
printf("%c",a1[j]);
printf(" + ");
for(j=m2;j<n2;j++)
printf("%c",b1[j]);
printf(" = ");
for(j=digit;j>=0;j--)
printf("%d",c2[j]);
if(i!=T)
printf("\n\n");
else
printf("\n");
//数组(大数)复位,以免影响下一次运算
for(j=0;j<=digit;j++)
a2[j]=b2[j]=c2[j]=0;
}
//fclose(in);
//fclose(out);
return 0;
}
听说作为acmer 大数四则运算要求闭眼敲出..救命 多看几遍吧