码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 10162 Last Digit

时间:2014-08-09 21:30:19      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

Description

bubuko.com,布布扣


 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;
}



UVA - 10162 Last Digit,布布扣,bubuko.com

UVA - 10162 Last Digit

标签:des   style   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/u011345136/article/details/38460239

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!