测试样例和我的思路解题之后结果不一样,但是自己觉得是应该逻辑是对的,所以就试了下提交一直有个用例过不了,后来发现问题是int类型写成了doublel类型,其他的没有问题
#include<iostream>
using namespace std;
string str;
int a,b;
int q,r;
void fun(){
a=0;
int i=0;
while(str[i]>='0'&&str[i]<='9'){
a=a*10+(str[i]-'0');
i++;
}
i++;
b=0;
while(str[i]>='0'&&str[i]<='9'){
b=b*10+(str[i]-'0');
i++;
}
}
void gcd(int n1,int n2){
if(n1<n2) swap(n1,n2);
while(n1%n2){
int tmp=n1%n2;
n1=n2;
n2=tmp;
}
a=a/n2;
b=b/n2;
}
int main(){
while(cin>>str){
//取出a 和b这两个数
fun();
gcd(a,b);
while(a>1){
q=b/a;
r=b-a*q;
cout<<1<<"/"<<q+1<<"+";
a=a-r;
b=b*(q+1);
gcd(a,b);
}
cout<<1<<"/"<<b<<endl;
}
return 0;
}
京公网安备 11010502036488号