UVA - 10162 Last Digit
Description
Problem B.Last Digit
Problem B.Last Digit |
Background
Give you a integer number N (1<=n<=2*10100). Pleasecompute
S=11+22+33+…+NN
Give the last digit of S to me.
Input
Input file consists of several Ns, each N a line. It is ended with N=0.
Output
For each N give a line containing only one digit, which is the lastdigit of S.
Sample Input
1
2
3
0
Sample Output
1
5
2
题意:求S的个位是多少
思路:看到这么大的数,先打个表试试,发现每20项是个小循环。每100项是个大循环。直接记录100项的结果计算
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int maxn = 300; int num[maxn]; char str[maxn]; int main() { int ans = 0; for (int i = 1; i <= 200; i++) { int tmp = 1; for (int j = 1; j <= i; j++) tmp = tmp * i % 10; ans = (ans + tmp) % 10; num[i] = ans; } while (scanf("%s", str) != EOF && str[0] != '0') { int len = strlen(str); int cnt = 0; for (int i = 0; i < len; i++) cnt = (cnt * 10 + str[i] - '0') % 100; if (!cnt) cnt = 100; printf("%d\n", num[cnt]); } return 0; }