我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/1038429286185074688
题目描述:
知识点:浮点数比较
思路:按题述编程即可
时间复杂度和空间复杂度都是O(1)。
注意点:
丙可能有小数。
C++代码:
#include<iostream>
#include<math.h>
using namespace std;
int change(int num);
void compare(int num1, int num2);
int main() {
int M, X, Y;
scanf("%d %d %d", &M, &X, &Y);
int jia;
int yi;
double bin;
for(jia = 99; jia >= 10; jia--) {
yi = change(jia);
bin = abs(jia - yi) * 1.0 / X;
if(yi * 1.0 / Y - bin < 0.001 && yi * 1.0 / Y - bin > -0.001) {
break;
}
}
if(jia >= 10) {
cout << jia << " ";
compare(jia, M);
cout << " ";
compare(yi, M);
cout << " ";
if(bin - M < 0.001 && bin - M > -0.001){
cout << "Ping";
}else if(bin > M){
cout << "Cong";
}else{
cout << "Gai";
}
}else{
cout << "No Solution" << endl;
}
return 0;
}
int change(int num) {
int num1 = num / 10;
int num2 = num % 10;
return num2 * 10 + num1;
}
void compare(int num1, int num2) {
if(num1 > num2) {
cout << "Cong";
} else if(num1 == num2) {
cout << "Ping";
} else {
cout << "Gai";
}
}
C++解题报告: