题解 | #连分数#
连分数
https://ac.nowcoder.com/acm/problem/220559
看了其他大佬AC的代码,感觉和他们差距好远。
ACM赛制真的锻炼代码能力,之前刷题都是OI和IOI赛制,遇到ACM赛制没有反馈就有些慌^^
#include<iostream> #include<string> #include<memory.h> using namespace std; string s; int m, n; int main() { int t; cin>>t; while (t--) { int cnt = 0; cin >> m >> n; s = to_string(m) + "/" + to_string(n); //转换成字符串(可能没必要?) int a[1000], t = 0; memset(a, 0, sizeof(a)); cout << m << "/" << n << " = "; if(m == n) //特判相等的点(我原来错的点TnT) { cout << "1" <<endl; continue; } if(!m) { cout << "0" << endl; continue; } if(n % m == 0) //注意如果是2/4 要输出0+1/2 不是1/2 { cout << "0+1/" << n / m << endl; continue; } for (int i = 0; i < s.size(); i++) { if (s[i] >= '0' && s[i] <= '9') { a[t] = a[t] * 10 + (int)(s[i] - '0'); } else t++; } if (t == 0) cout << a[0] << endl; else { int max = a[0], min = a[1]; while (max % min != 0) { int temp = min; min = max % min; max = temp; } int x = a[0] / min, y = a[1] / min; t = 0; while (x != 1) { int div = x / y, rem = x % y; if (t == 0) cout << div; else cout << "+1/{" << div; cnt++; //记录括号数 x = rem; if (x != 1) swap(x, y); t++; } if (y) cout << "+1/" << y; for (int i = cnt - 1; i > 0; i--) cout << '}'; cout << endl; } } return 0; }
整体思路就是把按位除了存放在数组里输出