My Calendar

2014年3月4日 星期二

UVa Problem 12289 - One-Two-Three

UVa Problem 12289 - One-Two-Three

Time limit: 1.000 seconds

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=3710&mosmsg=Submission+received+with+ID+19897820

Your little brother has just learned to write one, two and three, in English. He has written a lot of those words in a paper, your task is to recognize them. Note that your little brother is only a child so he may make small mistakes: for each word, there might be at most one wrong letter. The word length is always correct. It is guaranteed that each letter he wrote is in lower-case, and each word he wrote has a unique interpretation.

Input 
The first line contains the number of words that your little brother has written. Each of the following lines contains a single word with all letters in lowercase. The words satisfy the constraints above: at most one letter might be wrong, but the word length is always correct. There will be at most 10 words in the input.

Output 
For each test case, print the numerical value of the word.

Sample Input 
3
owe
too
theee

Sample Output 
1
2
3

解題方法 : Input 敘述中提到每個word的長度一定是對的 那首先只要判斷字的長度是 5 肯定就是3. one 和 two 擇一判斷輸入的每個字元是否有兩個或以上的字元相等.


import java.util.Scanner;

class Uva12289 {
 public static void main(String args[]) {
  
  Scanner scan = new Scanner(System.in); 
  int n= scan.nextInt();
  for(int i = 0 ; i < n ;i++) {
   String inputWords = scan.next();
   if(inputWords.length() == 5 )
    System.out.println("3");
   else{
    int cnt = 0 ;
    if( inputWords.charAt(0)=='o')
     cnt++;
    if( inputWords.charAt(1)=='n')
     cnt++;
    if( inputWords.charAt(2)=='e')
     cnt++;
    
    if(cnt >=2 )
     System.out.println("1");
    else
     System.out.println("2");
   }      
  }   
 }

}

沒有留言:

張貼留言