45/56 = 0.803(571428)
对于做出这道题我表示心很累,对多种情况的处理和细节分析都感觉有些麻烦,
言归正传,思路的关键,主要是找循环节,我们可以通过求余来判断,因为只要除到某一位置,所得余数和曾经的某个地方一样,这一串就是所求循环节
这种题多试试,要找出求解的根源,即此题的普遍性,来减少特判,并且特判多了其实就是有细节没有考虑上。还是要找普遍性
#include<cstdio> #include<algorithm> #include<iostream> #include<cmath> #include<cstring> #include<string> using namespace std; int n,d,a[1000009],h[1000009],tot=0; bool b[1000009]; void write(int a) { printf("%d",a); tot++;if (tot==76) tot=0,printf("\n"); } int main() { scanf("%d%d",&n,&d); printf("%d.",n/d); int rt=n/d;if (rt==0)tot=1; while (rt) tot++,rt=rt/10; tot++; int j=0,i=0; n=n%d;if (n==0)printf("0"); while(n) { i++; if (!b[n]) { b[n]=true; h[n]=i; }else { j=h[n]; break; } n=n*10; a[i]=n/d; n=n%d; } int m=i; if (j) { for (i=1;i<j;i++) write(a[i]); printf("(");tot++;if (tot==76) printf("\n"),tot=0; for (i=j;i<m;i++) write(a[i]); printf(")");tot++;if (tot==76) printf("\n"),tot=0; }else for (int i=1;i<=m;i++)write(a[i]); return 0; }