AOJ0002 Digit Number【水題】
阿新 • • 發佈:2019-02-06
Digit Number
Write a program which computes the digit number of sum of two integersaandb.
Input
There are several test cases. Each test case consists of two non-negative integersaandbwhich are separeted by a space in a line. The input terminates with EOF.
Constraints
- 0 ≤a,b≤ 1,000,000
- The number of datasets ≤ 200
Output
Print the number of digits ofa+bfor each data set.
Sample Input
5 7 1 99 1000 999
Output for the Sample Input
2 3 4
問題簡述:(略)
問題分析:
輸入兩個整數求其和,然後計算其位數。按照進位制原理來就可以了,不斷用10除移位,直到和變為0為止,就可以計數其位數。
程式說明:(略)題記:功能封裝到函式是個好主意。
參考連結:(略)
AC的C語言程式如下:
/* AOJ0002 Digit Number */ #include <stdio.h> int sum_digits(int n, int base) { int count = 0; while(n) { count++; n /= base; } return count; } int main(void) { int a, b; while(~scanf("%d%d", &a, &b)) printf("%d\n", sum_digits(a + b, 10)); return 0; }