zoukankan      html  css  js  c++  java
  • 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");
    			}
    	 }
      } 
    }
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    《计算学科系统导论》系列路线图
    “知行合一”的高职计算机教学观
    文化的概念
    关于班级博客地址
    一个字符编码处理小程序(一)
    关于计应151/152《软件工程》课程实践的安排
    人人都要学一点系统论
    我与软件工程
    关于本人与本博客
    THUWC前集训9
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734127.html
Copyright © 2011-2022 走看看