首页 > 专题系列 > Java解POJ > POJ 2328 Guessing Game [解题报告] Java
2013
11-11

POJ 2328 Guessing Game [解题报告] Java

Guessing Game

问题描述 :

Stan and Ollie are playing a guessing game. Stan thinks of a number between 1 and 10 and Ollie guesses what the number might be. After each guess, Stan indicates whether Ollie’s guess is too high, too low, or right on.

After playing several rounds, Ollie has become suspicious that Stan cheats; that is, that he changes the number between Ollie’s guesses. To prepare his case against Stan, Ollie has recorded a transcript of several games. You are to determine whether or not each transcript proves that Stan is cheating.

输入:

Standard input consists of several transcripts. Each transcript consists of a number of paired guesses and responses. A guess is a line containing single integer between 1 and 10, and a response is a line containing “too high”, “too low”, or “right on”. Each game ends with “right on”. A line containing 0 follows the last transcript.

输出:

For each game, output a line “Stan is dishonest” if Stan’s responses are inconsistent with the final guess and response. Otherwise, print “Stan may be honest”.

样例输入:

10
too high
3
too low
4
too high
2
right on
5
too low
7
too high
6
right on
0

样例输出:

Stan is dishonest
Stan may be honest

解题代码:

import java.util.*;  
      
    public class Main {  
      
        public static void main(String[] args) {  
              
            Scanner cin = new Scanner(System.in);  
          
            List guess = new ArrayList();  
           List response = new ArrayList();  
             
           while(cin.hasNext())  
           {  
               String guessNum = cin.nextLine();  
               if(guessNum.equals("0"))  
                   break;  
              String result = cin.nextLine();  
                
              guess.add(guessNum);  
              response.add(result);  
                 
               if(!result.equals("right on"))  
                   continue;  
                 
               else  
               {  
                   int honest = check(guess, response);  
                   if(honest == 1)  
                       System.out.println("Stan may be honest");  
                   else  
                       System.out.println("Stan is dishonest");  
                   guess.clear();  
                   response.clear();  
               }  
           }  
       }  
         
       private static int check(List guess, List response)  
       {  
           int honest = 1;  
             
           int lower = 0;  
           int upper = 11;  
             
           for(int i = 0; i < guess.size(); i++)  
           {  
               int g = Integer.valueOf((String)guess.get(i)).intValue();  
               String r = (String)response.get(i);  
                 
               if(r.equals("too high"))  
               {  
                   if(g <= lower)  
                   {  
                       honest = -1;  
                       break;  
                  }  
                   if(g < upper)  
                   {  
                       upper = g;  
                   }  
                     
               }  
               if(r.equals("too low"))  
               {  
                   if(g >= upper)  
                   {  
                       honest = -2;  
                       break;  
                   }  
                   if(g > lower)  
                   {  
                       lower = g;  
                   }  
               }  
                 
               if(r.equals("right on"))  
               {  
                   if(upper <= g || lower >= g || upper < lower)  
                   {  
                       honest = -3;  
                       break;  
                   }  
               }  
           }  
             
           return honest;  
       }  
     
   }

  1. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

  2. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.