题解 P1482 【Cantor表(升级版)】
引领天下
2017-04-16 08:36:38
首先,你要知道:
约分后的分母是行,分子是列。
于是问题来了:
怎么约分?
很简单:用辗转相除法求最大公约数,然后约分。
然后输出。
代码:
```cpp
#include<bits/stdc++.h>//包含所有头文件的头文件
using namespace std;
int gcd(int a,int b){//辗转相除法求最大公约数
if (!b)return a;//如果b=0,a是最大公约数
else return gcd(b,a%b);//不然继续
}//提示:现在也可以使用__gcd(a,b)直接求。
int main(){
ios::sync_with_stdio(false);//流优化
int a,b,c,d,t;
char hh;//这家伙对付‘/’号
cin>>a>>hh>>b>>c>>hh>>d;//读入,不解释
a*=c;b*=d;//乘一下
t=gcd(a,b);//求最大公约数
a/=t,b/=t;//约分
cout<<b<<" "<<a;//输出
return 0;
}
```