版本比较
题解:AT_past202212_b 分数比較
正文
增加 4 行删除 4 行
1234456787 行未改动1617181920202122232425252627282929303132333 行未改动# 题意:输入两个分数,比较大小。# 思路:由基础数学知识得: 由基础数学知识得:1. 当 $$\frac{a}{b} \ge \frac{c}{d} \ge 0$$ 时,有 $$a \times d \ge b \times c$$。2. 当 $$\frac{a}{b} \le \frac{c}{d} \le 0$$ 时,有 $$abs(b \times c) \le abs(a \times d)$$。
所以只需定义变量 $$f$$ 记录 $$\frac{a}{b} $$ 和 $$\frac{c}{d} $$ 的正负,再判断 $$a \times d$$ 和 $$b \times c$$ 的大小即可。7 行未改动{ int a,b,c,d; cin >> a >> b >> c >> d; bool f = false; if(a * b < 0 && c * d < 0) //判断正负 if(a * b < 0 && c * d < 0) //判断正负 f = true; if(abs(a * d) < abs(b * c)) if(!f) cout << "<"; else else cout << ">"; else if(a * d == b * c) cout << "="; else else if (!f) cout << ">"; else cout << "<";3 行未改动