[ACM ] 10035
題號:
Q10035: Primary Arithmetic
在小學時我們都做過加法的運算,就是把2個整數靠右對齊然後,由右至左一位一位相加
。如果相加的結果大於等於10就有進位(carry)的情況出現。你的任務就是要判斷2個整
數相加時產生了幾次進位的情況。這將幫助小學老師分析加法題目的難度。
Input
每一列測試資料有2個正整數,長度均小於10位。最後一列有2個0代表輸入結束。
Output
每列測試資料輸出該2數相加時產生多少次進位,請參考Sample Output。注意進位超過1
次時operation有加s
Sample Input
123 456
555 555
123 594
0 0
Sample Output
No carry operation.
3 carry operations.
1 carry operation.
遇到的問題:
測資都對,特殊狀況如999+1為3個carry , 100+900為1個carry 也都對。
不知少考慮了什麼情況。
有問題的code: (請善用置底文的標色功能)
#include <iostream>
using namespace std;
int main()
{
char stra[999]={'0'},strb[999]={'0'};
int num_of_times,little_digit;
int inta[999]={0},intb[999]={0};
while(true)
{
scanf("%s%s",stra,strb);
if(stra[0]=='0' && strlen(stra)==1 && strb[0]=='0' && strlen(strb)==1 )
break;
int i;
for(i=strlen(stra)-1;i>=0;i--)
{
inta[strlen(stra)-i-1]=stra[i]-'0';
}
inta[strlen(stra)-i-1]='\0';
for(i=strlen(strb)-1;i>=0;i--)
{
intb[strlen(strb)-i-1]=strb[i]-'0';
}
intb[strlen(strb)-i-1]='\0';
num_of_times=strlen(stra)>strlen(strb)?strlen(stra):strlen(strb);
little_digit=strlen(stra)>strlen(strb)?strlen(strb):strlen(stra);
i=0;
int carry=0,count=0;
while(i<num_of_times)
{
if(inta[i]+intb[i]+carry>=10)
{
carry=1;
count++;
}
else
carry=0;
++i;
}
if(count>1)
cout<<count<<" carry operations."<<endl;
else if(count ==1)
cout<<"1 carry operation."<<endl;
else
cout<<"No carry operation."<<endl;
}
return 0;
}
補充說明:
請問有ACM測資的網站嗎XD
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.235.115
→
11/19 13:53, , 1F
11/19 13:53, 1F
→
11/19 23:20, , 2F
11/19 23:20, 2F
→
11/20 00:49, , 3F
11/20 00:49, 3F
→
11/20 00:50, , 4F
11/20 00:50, 4F
推
11/20 00:52, , 5F
11/20 00:52, 5F
→
11/20 01:56, , 6F
11/20 01:56, 6F
→
11/20 01:56, , 7F
11/20 01:56, 7F