#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while (cin >> n){
float m, p;
for (int i=1; i<=n; i++){
cin >> m >> p;
float x=(p-m)/m*100;
cout << fixed << setprecision(2) << x << '%' << " ";
if (x>=10 || x<=-7){
cout << "dispose" << endl;
}
else {
cout << "keep" << endl;
}
}
}
return 0;
}
#includeusing namespace std;
int main()
{
int n;
while (cin >> n){
float m, p;
for (int i=1; i<=n; i++){
cin >> m >> p;
float x=(p-m)/m*100;
cout << fixed << setprecision(2) << x << '%' << " ";
if (x>=10 || x<=-7){
cout << "dispose" << endl;
}
else {
cout << "keep" << endl;
}
}
}return 0;
}
我的作法是先乘以10000,取整數後再除以100,就能避免誤差問題
#includeusing namespace std;
int main()
{
int n;
while (cin >> n){
float m, p;
for (int i=1; i<=n; i++){
cin >> m >> p;
float x=(p-m)/m*100;
cout << fixed << setprecision(2) << x << '%' << " ";
if (x>=10 || x<=-7){
cout << "dispose" << endl;
}
else {
cout << "keep" << endl;
}
}
}return 0;
}
我的作法是先乘以10000,取整數後再除以100,就能避免誤差問題
感恩😊