一个简单的数学题
时间限制:
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
-
一个最简分数化为小数有三种情况: (1)如果分母只含有质因数2和5,那么这个分数一定能化成有限小数,并且小数部分的位数等于分母中质因数2与5中个数较多的那个数的个数; (2)如果分母中只含有2与5以外的质因数,那么这个分数一定能化成纯循环小数; (3)如果分母中既含有质因数2或5又含有以外的质因数,那么这个分数一定能化成混循环小数,并且不循环部分的位数等于分母中质因数2与5中个数较多的那个数的个数。 http://wenku.baidu.com/view/a115698dcc22bcd127ff0c04.html
-
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[100000]; char b[100000]; int main() { int t; scanf("%d",&t); while(t--) {memset(a,0,sizeof(a)); int n; scanf("%d",&n); if(n==1) { printf("1\n"); } else if(n==-1) { printf("-1\n"); } else { if(n>0) { b[0]='0'; b[1]='.'; int s=1; int p=2; a[1]=1; while(1) { s=s*10; b[p++]=s/n+'0'; s=s%n; if(s==0) { break; } if(a[s]) { break; } a[s]=1; } b[p]='\0'; printf("%s\n",b); } if(n<0) { b[0]='-'; b[1]='0'; b[2]='.'; int s=1; int p=3; a[1]=1; n=-n; while(1) { s=s*10; b[p++]=s/n+'0'; s=s%n; if(s==0) { break; } if(a[s]) { break; } a[s]=1; } b[p]='\0'; printf("%s\n",b); } } } }