天天看點

PAT1088 三人行 (C++)1088 三人行 (C++)題目AC代碼:

1088 三人行 (C++)

題目

子曰:“三人行,必有我師焉。擇其善者而從之,其不善者而改之。”

本題給定甲、乙、丙三個人的能力值關系為:甲的能力值确定是 2 位正整數;把甲的能力值的 2 個數字調換位置就是乙的能力值;甲乙兩人能力差是丙的能力值的 X 倍;乙的能力值是丙的 Y 倍。請你指出誰比你強應“從之”,誰比你弱應“改之”。

輸入格式

輸入在一行中給出三個數,依次為:M(你自己的能力值)、X 和 Y。三個數字均為不超過 1000 的正整數。

輸出格式

在一行中首先輸出甲的能力值,随後依次輸出甲、乙、丙三人與你的關系:如果其比你強,輸出 Cong;平等則輸出 Ping;比你弱則輸出 Gai。其間以 1 個空格分隔,行首尾不得有多餘空格。

注意:如果解不唯一,則以甲的最大解為準進行判斷;如果解不存在,則輸出 No Solution。

輸入樣例 1:

輸出樣例 1:

48 Ping Cong Gai
           

輸入樣例 2:

輸出樣例 2:

No Solution
           

AC代碼:

#include<iostream>
#include<math.h>
using namespace std;
int change(int i){
	int temp=0;
	while(i>0){
		temp=temp*10+i%10;
		i=i/10;
	}
	return temp;
}
int main(){
	int m,x,y;
	int a,b;
	double c;
	cin>>m>>x>>y;
	int temp;
	bool flag=0;
	for(int i=99;i>9;i--){
		temp=change(i);
		c=(double)temp/y;//cout<<c<<endl;		
		if(abs(i-temp)==x*c&&temp==y*c){
			a=i;
			b=temp;
			flag=1;
			break;
		}
//		c=(double)abs(temp-i)*1.0/x;//該解法也正确 
//		if(temp==c*y){
//			a=i;
//			b=temp;
//			flag=1;
//			break;
//		}
	}
	//cout<<a<<" "<<b<<" "<<c<<endl;
	if(flag){
	
	cout<<a<<" ";
	if(m<a)cout<<"Cong ";
	if(m==a)cout<<"Ping ";
	if(m>a)cout<<"Gai ";
	
	if(m<b)cout<<"Cong ";
	if(m==b)cout<<"Ping ";
	if(m>b)cout<<"Gai ";
	
	if(m<c)cout<<"Cong";
	if(m==c)cout<<"Ping";
	if(m>c)cout<<"Gai";	}
	else{
		cout<<"No Solution";
	}
}
           
c++

繼續閱讀