NYOJ-330 一个简单的数学题

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=330

解题思路:

这道题可以用暴力解决,其实有数学方法。地址如下:

http://wenku.baidu.com/view/56df57c708a1284ac850430d.html

http://wenku.baidu.com/view/53a96582d4d8d15abe234eab.html

但是这道题范围不是太大,而且数据很弱,所以可以用一个数组代表余数,如果余数第二次出现,则肯定是第二个循环节的开始,则跳出循环即可。

代码如下:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int a[100010]; //存余数

int main()
{
	int ncase;
	int num, temp;
	scanf("%d", &ncase);
	while(ncase--)
	{
		temp = 1;
		memset(a, 0, sizeof(a));
		scanf("%d", &num);
		if(num < 0)
			printf("-0.");
		else
			printf("0.");
		while(a[temp] != 1 && temp != 0)
		{
			a[temp] = 1;
			temp *= 10;
			printf("%d", temp / num);
			temp %= num;
		}
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(c)