#34852: c++ 請問有人可以幫我看一下哪裡有錯嗎?


qaz0977609582@gmail.com (TYC MW)

學校 : 不指定學校
編號 : 130433
來源 : [118.163.199.144]
最後登入時間 :
2024-03-05 00:42:14
c014. 10035 - Primary Arithmetic -- UVa10035 | From: [49.215.23.220] | 發表日期 : 2023-04-21 14:30

#include <iostream>
using namespace std;
// 計算

int count(int a, int b)
{
int box = 0, c = 0, d = 0;
c = (a % 10 + b % 10);
while (a != 0 || b != 0)
{
if (c >= 10) // carry
{
box += 1;
a /= 10;
b /= 10;
c = (a % 10 + b % 10) + 1;
}
else
{
a /= 10;
b /= 10;
}
}
return box;
}

int main()
{
int a, b;
int ans;
while (cin >> a >> b)
{
if (a == 0 && b == 0)
break;
ans = count(a, b);
if (ans == 0)
cout << "No carry operation." << endl;
else if (ans < 2)
cout << ans << " carry operation." << endl;
else
cout << ans << " carry operations." << endl;
}

return 0;
}
 
#34855: Re: c++ 請問有人可以幫我看一下哪裡有錯嗎?


qaz0977609582@gmail.com (TYC MW)

學校 : 不指定學校
編號 : 130433
來源 : [118.163.199.144]
最後登入時間 :
2024-03-05 00:42:14
c014. 10035 - Primary Arithmetic -- UVa10035 | From: [118.163.199.192] | 發表日期 : 2023-04-21 19:16

#include
using namespace std;
// 計算

int count(int a, int b)
{
int box = 0, c = 0, d = 0;
c = (a % 10 + b % 10);
while (a != 0 || b != 0)
{
if (c >= 10) // carry
{
box += 1;
a /= 10;
b /= 10;
c = (a % 10 + b % 10) + 1;
}
else
{
a /= 10;
b /= 10;
}
}
return box;
}

int main()
{
int a, b;
int ans;
while (cin >> a >> b)
{
if (a == 0 && b == 0)
break;
ans = count(a, b);
if (ans == 0)
cout << "No carry operation." << endl;
else if (ans < 2)
cout << ans << " carry operation." << endl;
else
cout << ans << " carry operations." << endl;
}

return 0;
}

不用咯 已解決

 
ZeroJudge Forum