mark:题意很简单。。。输出小数形式,要标记循环节。不难就是写起来挺麻烦,开辟一个数组来标记余数是否出现过可以找到循环节。而且要记录循环节起始出现的位置。写的比较恶心。还有76个字符一行,要注意换行。最长的是1/9991,循环周期是1632位。。。代码写的比较恶心。。。
代码:
# include <stdio.h> # include <string.h> int vis[1000010], ans[1000010] ; char str[1000000] ; int main () { int cnt, cc, n, d, i; freopen ("fracdec.in", "r", stdin) ; freopen ("fracdec.out", "w", stdout) ; while (~scanf ("%d%d", &n, &d)) { memset (vis, 0, sizeof(vis)) ; cnt = 0 ; cnt = sprintf (str, "%d.", n/d) ; n = (n%d)*10 ; if (n == 0) cnt = cnt+sprintf (str+cnt, "0") ; for (cc = 1 ; n && !vis[n]; cc++, n=(n%d)*10) vis[n]=cc, ans[cc]=n/d ; if (n==0) for (i = 1 ; i < cc ; i++) cnt = cnt + sprintf (str+cnt, "%d", ans[i]) ; else { for (i = 1 ; i < vis[n] ; i++) cnt = cnt + sprintf (str+cnt, "%d", ans[i]) ; cnt = cnt + sprintf (str+cnt, "(") ; for (i = vis[n] ; i < cc ; i++) cnt = cnt + sprintf (str+cnt, "%d", ans[i]) ; cnt = cnt + sprintf (str+cnt, ")") ; } for (i = 0 ; str[i] ; i++){ if (i != 0 && i % 76 == 0) printf (" ") ; printf ("%c", str[i]) ; } printf (" ") ; } return 0 ; }