HDU 2522 1/n Hash

哎~~简直是坑爹的setbuf(stdout,NULL),不理解这个函数的机理,结果为杭电贡献了N次的time limit exceed,其实这道题目还是非常简单的,了解了除法的机制以后,就很好做了,注意余数相同时,循环节开始

 

View Code
 1 /*
2 * Author:lonelycatcher
3 * problem:hdu 2522
4 * Type:Hash
5 */
6 #include <iostream>
7 #include<stdio.h>
8 #include<string.h>
9 #include<cstdlib>
10 using namespace std;
11 int n;
12 bool Hash[100002];
13 void div(int x)
14 {
15 int left=1;
16 Hash[1]=1;
17 while(left)
18 {
19 left*=10;
20 printf("%d",left/x);
21 left%=x;
22 if(Hash[left])break;
23 Hash[left]=1;
24 }
25 }
26 int main()
27 {
28 int T;
29 scanf("%d",&T);
30 while(T--)
31 {
32 memset(Hash,0,sizeof(Hash));
33 scanf("%d",&n);
34 if(n<0){printf("-");n*=-1;}
35 if(n==1){printf("1\n");continue;}
36 printf("0.");
37 div(n);
38 printf("\n");
39 }
40
41 return 0;
42 }

posted on 2011-08-23 21:10  lonelycatcher  阅读(399)  评论(0编辑  收藏  举报

导航