hdoj1004遇到的问题总结及源代码

今天在做hdoj的1004题时自己测试感觉答案是正确的,但是就是死活过不了,一直wronganswer,看了半天自己写的代码,一直觉得没问题,在洗澡的时候突然想起我每次测试都是直接拷贝的给的示例输入,我用Scanner读入字符串是用的nextLine();这个方法是读取一行,包括空格,TAB,知道遇到回车换行结束,就是因为这个,在拷贝的时候中间有空格没注意导致一直wrong answer。想了想应该用next()方法输入字符串,next()方法是回到下一个分隔符的完整字符串。

 

next()与nextLine()的api解释

public String next()
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

Specified by:
next in interface Iterator<String>

Returns:
the next token

Throws:
NoSuchElementException - if no more tokens are available
IllegalStateException - if this scanner is closed

See Also:
Iterator



public String nextLine()
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Returns:
the line that was skipped

Throws:
NoSuchElementException - if no line was found
IllegalStateException - if this scanner is closed

//代码,大家可以参考参考

importjava.util.*;
 
public classMain
{
      
       public static void main(String[] args)
       {
              Scanner s = newScanner(System.in);
              ArrayList<String> al = newArrayList<String>();
              while (s.hasNext())
              {
                     int t = s.nextInt();
                     if (0 == t)
                            break;
                     HashMap<String,Integer> hm = new HashMap<String, Integer>();
                     while (t > 0)
                     {
                            t--;
                            String str =s.next();
                            int times;
                            if(hm.containsKey(str))
                            {
                                   times =hm.get(str).intValue()+1;
                            }else
                                   times=0;
                            hm.put(str, times);
                     }
                     Set<String> keys =hm.keySet();
                     String ans = "";
                     int max = -1;
                     for(String str:keys)
                     {
                            int temp =hm.get(str).intValue();
                            if(temp>max)
                            {
                                   max=temp;
                                   ans = str;
                            }
                     }
                     al.add(ans);
              }
              for(String str:al)
              {
                     System.out.println(str);
              }
              s.close();
       }
      
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值