牛客小白月赛33 B-连分数求助
#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[100], t = 0;
bool flag = false;
memset(a, 0, sizeof(a));
cout << m << "/" << n << " = ";
for (int i = 0; i < s.size(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
flag = true;
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;
} 求助!!! 我不知道哪里错了TnT
查看23道真题和解析