PAT-BASIC1088——三人行
阿新 • • 發佈:2018-11-06
我的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++解題報告: