小学生算术

计算两个整数相加需要多少次进位.?

如:输入 123 456 /555 555/ 123 594

输出 0 3 1

 

#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a,b;
while(cin>>a&&cin>>b)
{
if(!a&&!b) return 0;//如果输入都是0的话.程序中断退出.
int c = 0,ans = 0;
for(int i = 9;i>=0;i--)
{
c=(a%10+b%10+c)>9?1:0;//加上c的原因是获取前面的进位
ans+=c;
a/=10;b/=10;//降一位.
}
cout<<ans<<endl;
}
return 0;

posted @ 2014-01-14 17:02  CrazyCode.  阅读(99)  评论(0编辑  收藏  举报