POJ 2562 Primary Arithmetic(高精度)

来源:互联网 发布:中国农产品进出口数据 编辑:程序博客网 时间:2024/06/06 12:21

Description
给你两个数,问如果有两个数用笔算相加,有多少次进位
Input
Output
Sample Input
123 456
555 555
123 594
0 0
Sample Output
No carry operation.
3 carry operations.
1 carry operation.
Solution
简单模拟加法过程即可
Code

#include<stdio.h>int main(){    int a,b,i,res,n,s[100];    while(scanf("%d%d",&a,&b)&&!(a==0&&b==0))    {        res=n=0;        while(!(a==0&&b==0))        {            s[res++]=a%10+b%10;            a/=10;            b/=10;        }        for(i=0;i<res;i++)        {            if(s[i]>=10)//需要进位             {                s[i+1]++;//进位                 n++;//进位次数加一             }        }        if(n==0)            printf("No carry operation.\n");        else if(n==1)//注意单复数             printf("%d carry operation.\n",n);            else                printf("%d carry operations.\n",n);    }    return 0;} 
0 0
原创粉丝点击