UVA - 10162 Last Digit

Description

Download as PDF


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



转载于:https://www.cnblogs.com/brucemengbm/p/6729492.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值