一个简单的数学题
时间限制:
3000
ms
|
内存限制:
65535
KB
难度:
3
-
描述
- zyc最近迷上了数学,一天,dj想出了一道数学题来难住他。算出1/n,但zyc一时答不上来希望大家能编程帮助他。
-
-
输入
- 第一行整数T,表示测试组数。后面T行,每行一个整数 n (1<=|n|<=10^5). 输出
- 输出1/n. (是循环小数的,只输出第一个循环节). 样例输入
-
4 2 3 7 168
样例输出
-
0.5 0.3 0.142857 0.005952380
来源
- hdu
- 用一个数组代表余数,如果余数第二次出现,则肯定是第二个循环节的开始,则跳出循环即可。
- 代码:
-
#include
#include
#include
#include
#include
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==1)
printf("1");
else
{
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;
}