(hnust 1208)Problem C: Primary Arithmetic(水題)
時間限制: 1 Sec 記憶體限制: 128 MB
提交: 4 解決: 3
[提交][狀態][討論版]
題目描述
Problem C: Primary Arithmetic
Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the “carry” operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.
Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0. For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below.
輸入
輸出
樣例輸入
123 456
555 555
123 594
0 0
樣例輸出
No carry operation.
3 carry operations.
1 carry operation.
題意:給兩個不超過10位的數,相加,問有多少次進位?
分析:模擬, 注意No 和1的時候沒有s !!!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
const double INF=0x3f3f3f3f+1.0;
const double eps=1e-6;
const double PI=2.0*acos(0.0);
typedef long long LL;
const int N=15;
int a[N],b[N],ans;
int main()
{
char str1[N],str2[N];
while(~scanf("%s%s",str1,str2))
{
if(str1[0]=='0'&&str2[0]=='0') break;
ans=0;
mem(a,0),mem(b,0);
int len1,len2,len;
// printf("%s %s\n",str1+1,str2+1);
len1=strlen(str1),len2=strlen(str2);
str1[len1]='\0',str2[len2]='\0';
// printf("%d %d\n",len1,len2);
for(int i=len1-1,j=0; i>=0; i--,j++)
a[j]=str1[i]-'0';
for(int i=len2-1,j=0; i>=0; i--,j++)
b[j]=str2[i]-'0';
len=max(len1,len2);
for(int i=0; i<len; i++)
{
a[i]+=b[i];
if(a[i]>9)
{
ans++;
a[i+1]+=(a[i]/10);
a[i]%=10;
}
}
if(ans)
{
if(ans==1) printf("1 carry operation.\n");
else printf("%d carry operations.\n",ans);
}
else printf("No carry operation.\n");
}
return 0;
}
相關文章
- ABC353C Sigma Problem 題解
- 洛谷 P1208混合牛奶 題解
- 每日作業1208
- [Bash] Backticks, xargs and Arithmetic
- Problem C: 你過線了嗎?
- 1088 Rational Arithmetic (20分)
- “alter database switchover to xx“過程不當導致的primary-primary 雙主問題Database
- Phantom Problem(幻讀)問題
- Online Judge——1000.A+B Problem(c++)C++
- 水題得分...
- C#12中的Primary Constructors(主建構函式)C#Struct函式
- CF1618G Trader Problem 題解
- CF1603E A Perfect Problem 題解
- [ARC145D] Non Arithmetic Progression Set
- Acwing Arithmetic Learning:資料結構(2)資料結構
- 最大子陣列問題(Maximum subarray problem)陣列
- Problem E - Steps(問題e-步驟)
- ABC365E Xor Sigma Problem 題解
- CF1982F Sorting Problem Again 題解AI
- 原創題目 白銀之春 Problem and Solution
- CF1934B Yet Another Coin Problem 題解
- 人工智慧的人類問題(The human problem of AI)人工智慧AI
- [CF1188E] Problem from Red Panda 題解
- 2020ICPC小米網路賽 C.Data Structure ProblemStruct
- Sum Problem
- Mathematical Problem
- 3 月水題練習
- 每日水題 11-26
- AT_arc175_a [ARC175A] Spoon Taking Problem 題解
- 【PR #12】劃分序列 / Yet Another Mex Problem 題解
- Spring系列第十篇 primary可以解決什麼問題?Spring
- leetcode-盛最多水的容器(c++)LeetCodeC++
- hdu2045 遞迴水題遞迴
- Prime Ring Problem
- 2019 MCM Problem A
- Yet Another Problem
- Nanami and the Constructive ProblemNaNStruct
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'Error
- 組合數學水題 $19$ 道