对于一个整数X,定义操作rev(X)为将X按数位翻转过来,并且去除掉前导0。例如:
如果 X = 123,则rev(X) = 321;
如果 X = 100,则rev(X) = 1.
如果 X = 123,则rev(X) = 321;
如果 X = 100,则rev(X) = 1.
现在给出整数x和y,要求rev(rev(x) + rev(y))为多少?
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include<stdio.h>
#include<stdlib.h> #include<math.h> #include <iostream> int rev(int i) { int data[10]; int t = 0; int j; int result = 0; while (i / 10 != 0) { if (i % 10 != 0) data[t++] = i % 10; i = i / 10; } data[t] = i; for (j = 0; j <= t; j++) { result = result + data[j] * pow(10,t - j); } return result; } int main() { int m, n; int result = 0; printf("请输入m,n\n"); scanf("%d%d", &m,&n); result = rev(rev(m) + rev(n)); printf("%d\n", result); system("pause"); } |