题目
思路
这题有两个考点:
- 两位数个位和十位的转换。
有两种办法:
一是用整型:d2 = d1 % 10 * 10 + d1 / 10
(适用于位数比较少的情况)
二是利用字符串函数:s1=to_string(d1); reverse(s1.begin(),s1.end()); d2=stod(s1);
- 如何找出甲乙丙;
一开始我用丙遍历,寻找甲乙,事倍功半。
改用甲来遍历,用double型储存乙丙即可。
AC代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int m,x,y; cin>>m>>x>>y;
int d1=0;
double d2=0,d3=0;
string s1;
for(int i=99;i>=0;i--){
d1=i;
s1=to_string(d1);
reverse(s1.begin(),s1.end());
d2=stod(s1);
d3=d2/y;
double temp=abs(d1-d2)/x;
if(temp==d3){
break;
}
}
if(d1==0 || d2==0 || d3==0){
cout<<"No Solution";
return 0;
}
cout<<d1<<' ';
if(d1>m) cout<<"Cong ";
else if(d1==m) cout<<"Ping ";
else cout<<"Gai ";
if(d2>m) cout<<"Cong ";
else if(d2==m) cout<<"Ping ";
else cout<<"Gai ";
if(d3>m) cout<<"Cong";
else if(d3==m) cout<<"Ping";
else cout<<"Gai";
return 0;
}