题目:1088 三人行 - PAT (Basic Level) Practice (中文) (pintia.cn)
解析:PAT 1088 三人行(20 分)- 乙级_pat1088-CSDN博客
认真读题呀
我刚开始还在想至少给一个数,让我来推吧,实际上这个数就是让你自己枚举的,题目已经说了甲的范围是一个二位数,枚举就行了。
用字符串的方式
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int m, x, y;
void print(double x)
{
if (m == x)cout << " Ping";
else if (x > m)cout << " Cong";
else cout << " Gai";
}
int main()
{
cin >> m >> x >> y;
//推甲
for (int jia = 99; jia >= 10; jia--)
{
string str_yi = to_string(jia);
reverse(str_yi.begin(), str_yi.end());
int yi = stoi(str_yi);
double bing = abs(jia - yi) * 1.0 / x;
if (yi == bing * y)
{
cout << jia;
print(jia); print(yi); print(bing);
return 0;
}
}
cout << "No Solution";
return 0;
}
用整数的方式
整数逆转Tips:
#include<bits/stdc++.h>
using namespace std;
int m,x,y;
void Print(double x)
{
if(x>m)printf(" Cong");
else if(x==m)printf(" Ping");
else printf(" Gai");
}
int main()
{
cin>>m>>x>>y;
for(int i=99;i>10;i--) //倒着数,因为题目要求输出甲的最大解
{
int a=i;
int b=i/10+(i%10)*10;
double c=abs(a-b)*1.0/x; //逆转
if(b==c*y)
{
cout<<a;
Print(a);Print(b);Print(c);
return 0; //这里一定要提前返回值,不然会一直输出
}
}
cout<<"No Solution";
return 0;
}