【hdoj1002】 A + B Problem II(模拟加法)



思路:

        普通的加法模拟,

        注意直接输入的数和所要运算的数刚好顺序颠倒

        本题最大难点:格式!!!!我PE了不下3次。。。最关键的是最后三行的回车

       <记住,oj中每行最后不能多空格,运行结果最后只能一个回车。具体情况看题目要求。。。>


#include<stdio.h>
#include<string.h>
void main()
{
   int T,x;
   scanf("%d",&T);
   getchar();
   for(x=0;x<T;x++)
   {
          char a1[1001]={'0'};
	  char b1[1001]={'0'};
	  char a[1001]={'0'};
	  char b[1001]={'0'};
	  char ans[1001]={'0'};
	  int t=0,i,j,k=0;
	  scanf("%s %s",a1,b1);
	  printf("Case %d:\n",x+1);
	  int len1=strlen(a1);
	  int len2=strlen(b1);
	  int len=(len1<len2)?len2:len1;
	  for(i=0;i<len1;i++)
		  a[i]=a1[len1-1-i];
	  for(i=0;i<len2;i++)
		  b[i]=b1[len2-1-i];
	  i=0,j=0;
	  while(k<len)
	  {
		  if(a[i]&&b[j])
		  {
			  ans[k++]=(a[i]+b[j]+t-'0'-'0')%10+'0';
		      t=(a[i++]+b[j++]+t-'0'-'0')/10;
		  }
		  else if(a[i]||b[j])
		  {
		      ans[k++]=(a[i]+b[j]+t-'0')%10+'0';
		      t=(a[i++]+b[j++]+t-'0')/10;
		      //printf("%c %d\n",ans[k-1],t);
		  }
	     else 
		 {
		      ans[k++]=t+'0';
			  t=0;
		 }
	  }
	  if(t==1)
	  {
	      ans[k++]=t+'0';
	  }
	  printf("%s + %s = ",a1,b1);
	  for(i=k-1;i>=0;i--)
		  printf("%c",ans[i]);
	  printf("\n");
	  if(x!=T-1)
		  printf("\n");
   }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值