学校1026: 大数加法

来源:互联网 发布:卓讯数据库打开 编辑:程序博客网 时间:2024/04/30 22:09

Description
计算A+B的值A,B是不超过200位的正整数


Input
多组数据读入
每组数据输入为一行两个整数A,B


Output
每组数据输出为一行,为其运算结果


Sample Input
9 9
987 654
Sample Output
18
1641



上课的例题;

主要方法:首先倒序;g作为前一位满十,进的数;


#include <stdio.h>#include <stdlib.h>#include<string.h>#define N 220int main(){    char str1[N],str2[N];    int i,j,len1,len2;    while(scanf("%s%s",str1,str2)!=EOF)    {        int a[N]= {0},b[N]= {0},c[N]= {0};        for(i=strlen(str1)-1,j=0; i>=0; i--)            a[j++]=str1[i]-'0';        for(i=strlen(str2)-1,j=0; i>=0; i--)            b[j++]=str2[i]-'0';        int g=0;        for(i=0; i<N; i++)        {            c[i]=(a[i]+b[i]+g)%10;            g=(a[i]+b[i]+g)/10;        }        int flag;        for(i=N-1; i>=0; i--)            if(c[i]!=0)            {                flag=i;                break;            }        for(i=flag; i>=0; i--)            printf("%d",c[i]);       printf("\n");    }    return 0;}


















0 0
原创粉丝点击