古代先民认为,天下万物皆由五类元素组成,分别是金、木、水、火、土,彼此之间存在相生相克的关系。
相生关系为:木生火,火生土,土生金,金生水,水生木。
相克关系为:金克木,木克土,土克水,水克火,火克金。
本题就请你编写程序,判断任意一对给定的元素之间的关系。
输入格式:
输入在一行中给出一个正整数 N(≤10),随后 N 行,每行给出
2 个正整数 A
和 B
,为两种元素的编号。这里假设金、木、水、火、土的编号顺次为 1、2、3、4、5。
输出格式:
对输入的每一对 A
和 B
,如果 A
生 B
,则输出 A sheng B
;如果 A
克 B
,则输出 A ke B
。反之亦然。
输入样例:
2
4 2
5 3
输出样例:
2 sheng 4
5 ke 3
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解题思路:这道题直观思路应该就是枚举了,枚举A的情况,再分析当B取不同的值的时候输出的不同情况。因为输出的要求是数字 sheng/ke 数字所以我们可以直接这么干,不然要判断Asheng/keB 还是Bsheng/keA,那样就比这题的难度高很多了。
AC代码(C++):
#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
int n;
cin>>n;
int A,B;
for(int i =0;i<n;i++)
{
cin>>A>>B;
//枚举A的各种情况
if(A==1) //若A是金
{
if(B==2)
cout<<"1 ke 2"<<endl;
if(B==5)
cout<<"5 sheng 1"<<endl;
if(B==3)
cout<<"1 sheng 3"<<endl;
if(B==4)
cout<<"4 ke 1"<<endl;
}
else if(A==2) //若A是木
{
if(B==4)
cout<<"2 sheng 4"<<endl;
if(B==3)
cout<<"3 sheng 2"<<endl;
if(B==1)
cout<<"1 ke 2"<<endl;
if(B==5)
cout<<"2 ke 5"<<endl;
}
else if(A==3)//若A是水
{
if(B==1)
cout<<"1 sheng 3"<<endl;
if(B==2)
cout<<"3 sheng 2"<<endl;
if(B==5)
cout<<"5 ke 3"<<endl;
if(B==4)
cout<<"3 ke 4"<<endl;
}
else if(A==4)//若A是火
{
if(B==2)
cout<<"2 sheng 4"<<endl;
if(B==5)
cout<<"4 sheng 5"<<endl;
if(B==3)
cout<<"3 ke 4"<<endl;
if(B==1)
cout<<"4 ke 1"<<endl;
}
else if(A==5)//若A是土
{
if(B==4)
cout<<"4 sheng 5"<<endl;
if(B==1)
cout<<"5 sheng 1"<<endl;
if(B==2)
cout<<"2 ke 5"<<endl;
if(B==3)
cout<<"5 ke 3"<<endl;
}
}
}