A simple problemTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2709 Accepted Submission(s): 946Problem Description
Zty很痴迷数学问题.。一天,yifenfei出了个数学题想难倒他,让他回答1 / n。但Zty却回答不了^_^. 请大家编程帮助他.
Input
第一行整数T,表示测试组数。后面T行,每行一个整数 n (1<=|n|<=10^5).
Output
输出1/n. (是循环小数的,只输出第一个循环节).
Sample Input
4
2
3
7
168
Sample Output
0.5
0.3
0.142857
0.005952380
|
关键处理循环小数。。。。。
/* 原理是:eg:1/4....商0,余1;
1*10/4....商2,余2;
2*10/4.....商5余0;
1/3....
1/3....商0,余1;
1*10/3....商3,余1;
余数相同,退出..... */
详见代码,,,,,,
#include<iostream> #include<cstdio> #include<cstring> using namespace std; //出现余数为零,或者余数出现过,即为循环节终止。 int a[10001]; int hash[100001]; int main() { int T; cin>>T; while(T--) { int n,cn=0,mod=1; int sig=1; //符号 cin>>n; if(n==1 || n==-1 ) { cout<<n<<endl; continue; } n<0?(n=-n,sig=0):1; memset(hash,0,sizeof(hash));//手误hash memset(a,0,sizeof(a)); hash[mod]=1; //首先就是 1/n....商0,余1; while(1) { mod*=10; a[cn++]=mod/n; mod%=n; if(hash[mod]||!mod) break; hash[mod]=1; } printf(!sig?"-0.":"0."); for(int i=0;i<cn;i++) { cout<<a[i]; } cout<<endl; } return 0; }