站点图标 陌路寒暄

圆周率

圆周率

时间: 1ms        内存:128M

描述:

YT大学附小举办背诵圆率 PI 的比赛。谁背的正确的位数越多,谁为胜者。很多小学生背的位数很多,但是往往会有少数位置的数是错误的。为了快速加测出错误的圆周率,将圆周率 PI 小数点后的数字求模进行简单验证。
例如:某学生背的圆周率为 3.15,则1510 mod 9 = 6,可以初步判定该学生背的圆周率错误。
数有不同的进制表示,比如二进制、八进制、十进制等。现在给你一个任务,给定一个n进制,要它对n-1求模,比如:
             782910 mod 9 =  8
             377777777777777738 mod 7 =6
             1234567 mod 6 =3
(注意:377777777777777738=112589990684261910   1234567 =2287510 )
你的任务是读入一些不同进制的数,求模。

输入:

 第一行表示为整数P(1P1000),表示一共的测试数据组数。
每组测试测试数据一行,由三个数组成,第一个数表示组号,第二个数B(2B10),表示B进制,第三个数D表示要求模的数,D的位数不超过10,000,000位。

输出:

每组测试数据一行,每一个数为组号,第二个为 D mod (B-1)

示例输入:

6
1 10 7829
2 7 12345
3 6 432504023545112
4 8 37777777777777773
5 2 101011111111110000000000000000000011111111111111111111111
6 10 145784444444444457842154777777777547845993

示例输出:

1 8
2 3
3 1
4 6
5 0
6 6

提示:

参考答案(内存最优[800]):

#include <stdio.h>
#include <assert.h>
#include <ctype.h>

int main()
{
	int p, i, i1, b, result, c;
	scanf("%d", &p);				/* get # of data sets */
	for (i = 0; i < p; i++) {
		scanf("%d %d ", &i1, &b);	/* trailing space in fmt eats spaces */
		assert(i1 == i + 1);		/* between B and N */
		result = 0;
		while (isdigit(c = getchar())) {
			result = (result + c - '0') % (b - 1);
		}
		printf("%d %d\n", i1, result);
	}
	return 0;
}

参考答案(时间最优[0]):

#include <stdio.h>
#include <assert.h>
#include <ctype.h>

int main()
{
	int p, i, i1, b, result, c;
	scanf("%d", &p);				/* get # of data sets */
	for (i = 0; i < p; i++) {
		scanf("%d %d ", &i1, &b);	/* trailing space in fmt eats spaces */
		assert(i1 == i + 1);		/* between B and N */
		result = 0;
		while (isdigit(c = getchar())) {
			result = (result + c - '0') % (b - 1);
		}
		printf("%d %d\n", i1, result);
	}
	return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版