hdu 2522 A simple problem

 

A simple problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2730    Accepted Submission(s): 958


Problem 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
 

 

Author
yifenfei
 

 

Source
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  1264 2523 2527 2526 2524 
 

 

 

 1 //1046MS    1004K    743 B    C++    
 2 /*
 3 
 4     题意:
 5         中文
 6     
 7     hash:
 8         要利用到数学知识了...出现相同余数的时候说明有相同的
 9     循环节了. 
10      
11 */
12 #include<stdio.h>
13 #include<string.h>
14 int main(void) 
15 {
16     int t,n;
17     int mark[100005],ans[100005];
18     scanf("%d",&t);
19     while(t--)
20     {
21         scanf("%d",&n);
22         if(n==1){
23             puts("1");continue;
24         }
25         if(n<0){n=-n;printf("-0.");}
26         else printf("0.");
27         memset(mark,0,sizeof(mark));
28         memset(ans,0,sizeof(ans));
29         int cnt=0;
30         int temp=1;
31         mark[temp]=1;
32         while(temp){
33             temp*=10;
34             ans[cnt++]=temp/n;
35             temp%=n;
36             if(mark[temp]==1) break;
37             mark[temp]=1;
38         }
39         for(int i=0;i<cnt;i++)
40             printf("%d",ans[i]);
41         printf("\n");
42     }
43     return 0;
44 }

 

posted @ 2013-10-24 22:03  heaventouch  阅读(121)  评论(0编辑  收藏  举报