hdu 1002 A + B Problem II

来源:互联网 发布:马刺 霍尔特 知乎 编辑:程序博客网 时间:2024/05/17 13:11

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

 

代码:

#include<iostream>#include<cstdio>#include<string.h>using namespace std;int main(){ char a[1005],b[1005]; int c[1005]; int N,cnt=0; while(scanf("%d",&N)!=EOF){  for(int i=1;i<=N;i++)  {   cnt=0;   scanf("%s%s",a,b);   int len1,len2,length,x1,x2,pos=0;   len1=strlen(a);   len2=strlen(b);   while(len1>0&&len2>0){    x1=a[len1-1]-'0';//字符转换为数字     x2=b[len2-1]-'0';    //printf("x1 = %d    x2 = %d\n",x1,x2);    if(x1+x2+pos>=10){          c[cnt++]=x1+x2+pos-10;     pos=1;//大于10用pos记下      //cout<<"1__"<<c[cnt-1]<<endl;    }    else{     c[cnt++]=x1+x2+pos;     //cout<<"2__"<<c[cnt-1]<<endl;     pos=0;    }    len1--;    len2--;   }   if(len1>len2){    while(len1>0){    c[cnt++]=a[len1-1]-'0'+pos;    //cout<<"3__"<<c[cnt-1]<<endl;    pos=0;    len1--;   }   }   if(len1<len2)   {    while(len2>0){     c[cnt++]=pos+b[len2-1]-'0';     //cout<<"4__"<<c[cnt-1]<<endl;     pos=0;     len2--;    }   }   if(len1==len2){    if(pos==1)    c[cnt++]=1;   }  if(i==1){//任意两组数中间有空格    printf("Case %d:\n",i);   printf("%s + %s = ",a,b);    for(int k=cnt-1;k>=0;k--)    printf("%d",c[k]);   printf("\n");  }  else  {   printf("\nCase %d:\n",i);   printf("%s + %s = ",a,b);    for(int k=cnt-1;k>=0;k--)    printf("%d",c[k]);   printf("\n");  } }   } return 0;}


 

原创粉丝点击