题解 P1482 【Cantor表(升级版)】

我想问一下楼下是肿么想的。

写了读入优化不用。。。。。

还有求最大公约数何必暴力??

额额。


以上是个人吐槽内容。


重点来了!

首先,你要知道:

约分后的分母是行,分子是列。

于是问题来了:

怎么约分?

很简单:用辗转相除法求最大公约数,然后约分。

然后输出。

完事。


让路


代码上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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);//不然继续
}
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;//庄严地结束
}