坤坤的难题
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 49 Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
坤坤在成功学习完A+B、A-B以及A*B之后,开始学习A/B,但是,结果会有循环小数,这下就难倒了坤坤,为了简化问题,我们假设A=1,请计算A/B
Input
第一行整数T,表示测试组数。后面T行,每行一个整数 B (1<=|B|<=10^5).
Output
输出A/B. (是循环小数的,只输出第一个循环节).
Sample Input
3 1 3 7
Sample Output
1 0.3 0.142857
Author
ac代码
#include<stdio.h> #include<string.h> int v[100010]; int main() { int t; scanf("%d",&t); while(t--) { int n,mod; scanf("%d",&n); if(n==1) { printf("1\n"); continue; } if(n==-1) { printf("-1\n"); continue; } if(n<0) { printf("-0."); n=-n; } else printf("0."); memset(v,0,sizeof(v)); v[1]=1; mod=1; while(1) { mod*=10; if(mod>=n) { printf("%d",mod/n); mod=mod%n; } else printf("0"); if(mod==0) break; if(v[mod]) break; v[mod]=1; } printf("\n"); } }