Poj 2328 Guessing Game(猜数字游戏)

一、题目大意

        两个小盆友玩猜数字游戏,一个小盆友心里想着1~10中的一个数字,另一个小盆友猜。如果猜的数字比实际的大,则告诉他“too high”,小则“too low”,正好则“right on”。直到猜对为止。但是那个猜的朋友怀疑他的小伙伴作弊,给他的回答不正确。于是让你根据他们的对话来判断一下这个小伙伴是否说谎。

二、题解

        这个题看上去是个水题,但是我RE4次,WA2次。主要的原因是输入格式的读取,输入中既有数字又有字符串。而我的思路则是先用数组存放数组和字符串,然后用数组的最后一个数字来和前面的数字比较,如果结果和字符串数组中的描述符合则Honest,否则有一个描述不一样则dishonest。

三、java代码

import java.util.Scanner;
public class Main{
	static int a[];
	static String s[];
	public static boolean check(int length){
		int i,n,m;
		n=a[length];
		for(i=0;i<length;i++){
			m=a[i]-n;
			if(m>0 && !s[i].equals("high")){
				return false;
			}
			if(m<0 && !s[i].equals("low")){
				return false;
			}
			if(m==0 && !s[i].equals("on")){
				return false;
			}
		}
		return true;
	}
  public static void main(String args[]){
	 Scanner sc=new Scanner(System.in);
	 int i,n;
	 while((n=sc.nextInt())!=0){
		 a=new int[20];
		 s=new String[21];
		 a[0]=n;
		 i=0;
		 while(true){
			sc.next();
		    s[i]=sc.next();
		    if(s[i].equals("on"))
		    	break;
		    i++;
			a[i]=sc.nextInt();
		 }
		 if(check(i)){
				System.out.println("Stan may be honest");
			}else{
				System.out.println("Stan is dishonest");
			}
	 }
  } 
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值