剪刀石头布
时间: 1ms 内存:128M
描述:
小慧小时候很喜欢和她的小伙伴们一起玩剪刀(Scissors)、石头(Rock)、布(Cloth)的游戏,但现在她上大学了,和小伙伴们不能经常见面,但可以通过网络交流,她现在很想和小伙伴们重温这个小游戏。
学了c++的你,可以用类帮她完成这个小游戏吗?每局游戏进行n场对战,赢得次数多的是最后的赢家,可以出现平局。
输入:
第1行 n(1<=n<=20) ,表示下面有n组对战信息。
从第2行到第n+1行,每行两个英文单词,表示对战信息。小慧的信息是第一列
输出:
一个英文字母W或L或D 表示小慧的赢,输或平手。
示例输入:
5
Rock Scissors
Scissors Rock
Cloth Cloth
Cloth Rock
Rock Rock
示例输出:
W
提示:
参考答案(内存最优[1096]):
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int i,k=0,j=0,n;
char a[20],b[20];
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%s%s",a,b);
if((a[0]=='S'&&b[0]=='C')||(a[0]=='R'&&b[0]=='S')||(a[0]=='C'&&b[0]=='R'))k++;
else if(a[0]==b[0])
{
k++;
j++;
}
else j++;
a[0]='\0';b[0]='\0';
}
if(k==j)printf("D");
if(k<j)printf("L");
if(k>j)printf("W");
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
using namespace std;
class RSC
{
private:
char a[21][10];
char b[21][10];
int n;
char result;
int judge(char a,char b);
public:
void input();
void output();
void decide();
};
int RSC::judge(char a,char b)
{
int w;
if(a>b)
w=-1;
else if(a<b)
w=1;
else
w=0;
return(w);
}
void RSC::input()
{
cin>>n;
int i;
for(i=0; i<n; i++)
cin>> a[i]>>b[i];
}
void RSC::output()
{
decide();
cout<<result<<endl;
}
void RSC::decide()
{
int num=0,i;
for(i=0; i<n; i++)
num = num + judge(a[i][0],b[i][0]);
if(num>0)
result ='W';
else if(num==0)
result= 'D';
else
result = 'L';
}
int main()
{
RSC test;
test.input();
test.output();
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。