POJ 1503 && HDU 1047 Integer Inquiry(高精度)

来源:互联网 发布:淘宝号查询信誉网址 编辑:程序博客网 时间:2024/06/06 07:14

Description
大整数加法,每个整数至多包含100个数字,最多100个整数相加,计算并输出结果
Input
最多输入100行数据,每行输入一个正整数,输入0结束输入
Output
输出所有正整数累加的结果
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
Solution
简单模拟
用数组存正整数,从低位往高位存,加的时候注意进位
Code

#include<stdio.h>#include<string.h>#define maxn 105int main(){    int map[maxn][maxn];    memset(map,0,sizeof(map));//初始化     char s[maxn];    int k=0;    while(gets(s)&&strcmp(s,"0")!=0)//以字符串形式读入整数     {        int len=strlen(s);        for(int i=len-1;i>=0;i--)//将字符串转化为整型数组存储             map[k][len-i-1]=s[i]-'0';        k++;//记录正整数数量     }    int temp=0,ans[maxn];//temp为每次进位值,ans存储最终答案     for(int i=0;i<maxn;i++)    {        int res=0;        for(int j=0;j<k;j++)            res+=map[j][i];        ans[i]=(res+temp)%10;//记录答案         temp=(res+temp)/10;//记录进位值     }    int i=maxn-1;    while(ans[i]==0)//判断答案位数        i--;    for(;i>=0;i--)        printf("%d",ans[i]);    printf("\n");    return 0;}
0 0