1 //改代码用于精确计算除法的位数,比如求无限循环小数的循环节 2 //求循环节时,需要定义一个数组,用与标记是否有相同的余数,若是遇到时,结束循环,即得到循环节 3 #include<iostream> 4 using namespace std; 5 6 int main() { 7 int a, b; 8 while (cin >> a >> b) { 9 cout << a << "/" << b << "="; 10 int cnt = a / b; 11 cout << cnt; 12 a -= cnt * b; 13 a *= 10; 14 if (a != 0) cout << "."; 15 // if(a==0) cout<<"0"; 16 int count = 0; 17 while (a) { 18 int ans = a / b; 19 cout << ans; 20 count++; 21 if (count == 20) break; 22 a -= ans * b; 23 a *= 10; 24 } 25 cout << endl; 26 } 27 28 return 0; 29 }