UVA - 10162 Last Digit

Description

 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

时间: 2024-10-13 11:08:26

UVA - 10162 Last Digit的相关文章

uva 10162 - Last Digit(数论)

题目链接:uva 10162 - Last Digit 题目大意:给定n,求s的个位的数值是多少. 解题思路:对于ii,重复周期为20,这样就有 1 4 7 6 5 6 3 6 9 0 1 6 3 6 5 6 7 4 9 0 但是这个周期的值是不为0的,总的话是100为一个大周期. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxt =

10162 - Last Digit (数论+周期规律)

UVA 10162 - Last Digit 题目链接 题意:求S=(11+22+...NN)%10 思路:打出0-9的每个周期,发现周期为1或2或4.所以S是以20一个周期,打出表后发现20为4,所以对应的40为8,60为2,80为6,100为0,100为1个周期,且为0,所以先把数字mod上100,然后在mod 20求出对应位置. 代码: #include <stdio.h> #include <string.h> const int Z2[10] = {0, 4, 8, 2

uva 1583 B - Digit Generator (暴力)

B - Digit Generator Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description For a positive integer N<tex2html_verbatim_mark> , the digit-sum of N<tex2html_verbatim_mark> is defined as the sum of N&l

(UVA)1225 --Digit Counting(数数字)

题目链接:http://vjudge.net/problem/UVA-1225 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int f[10000][10]; int main() { memset(f, 0, sizeof(f)); for (int i = 1 ; i < 10000 ; ++ i) { for (int j = 0 ; j <

UVa 1583 Digit Generator(数)

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 

UVa 1225 Digit Counting --- 水题

UVa 1225 题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字 解题思路:用一个cnt数组记录0-9这10个数字出现的次数,先将cnt初始化为0,接着让i从1枚举到n, 对每个i,处理以活的i的每一个位置上的数,并在相应的cnt下标上+1 最后输出cnt数组即可 /* UVa 1225 Digit Counting --- 水题 */ #include <cstdio> #include <cstring

UVa 10533 - Digit Primes

题目:输出给定区间中,本身是素数,并且这个数的各位之和也是素数的数(称为位素数)的个数. 分析:数论.首先利用筛法,求出1000000内的所有的素数:然后在利用生成的素数表, 判断每个数是不是各位之和也是素数:再后求出从0开始到任意区间中包含位素数数的个数: 最后输出两个区间之差就是区间中的位素数的个数. 说明:达标法计算,查询输出. #include <iostream> #include <cstdlib> #include <cstring> #include &

紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威

来源:http://m.blog.csdn.net/article/details?id=70861055 Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number

Digit Counting UVA - 1225

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 toN(1 < N < 10000) . After that, he counts the number of times each digit (0 to 9) appears in the sequence