【HDOJ】2522 A simple problem

小数除法。放大然后求除数和余数。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 char a[100005];
 5 char visit[100005];
 6 
 7 int main() {
 8     int t, n;
 9     int i, j, k;
10 
11     scanf("%d", &t);
12 
13     while (t--) {
14         scanf("%d", &n);
15         if (n==1 || n==-1) {
16             printf("%d\n", 1/n);
17             continue;
18         }
19         memset(visit, 0, sizeof(visit));
20         k = n<0 ? -n:n;
21         i = 1;
22         j = 0;
23         visit[1] = 1;
24         while (i) {
25             i *= 10;
26             a[j++] = i/k;
27             i %= k;
28             if (visit[i])
29                 break;
30             visit[i] = 1;
31         }
32         if (n>0)
33             printf("0.");
34         else
35             printf("-0.");
36         for (i=0; i<j; ++i)
37             printf("%d", a[i]);
38         printf("\n");
39     }
40 
41     return 0;
42 }

 

posted on 2014-04-21 10:58  Bombe  阅读(160)  评论(0编辑  收藏  举报

导航