码迷,mamicode.com
首页 > 其他好文 > 详细

【CodeChef】Enormous Input Test

时间:2014-07-31 19:39:57      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   strong   io   

The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime.

Input

The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each.

Output

Write a single integer to output, denoting how many integers ti are divisible by k.


题解:记录这道题主要是为了记录java中Scanner和BufferReader的区别,开始用Scanner,效率非常低,所以就TLE了。根据StackOverFlow里面解释:BufferReader只是从流中读入数据,但不对数据做任何处理,Scanner按照需求解析数据并读入,比如nextInt(),nextDouble()等。更详细的答案还有这里

总结一下:

  • A BufferedReader is a simple class meant to efficiently read from the underling stream.
  •  BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads.
  • Scanner can parse the underlying stream for primitive types and strings using regular expressions.
  • A scanner however is not thread safe, it has to be externally synchronized.

对于原文中的“ A scanner can do all that a BufferedReader can do and at the same level of efficiency as well.”不太认同,因为通过OJ来看,BufferReader的效率确实比Scanner高。

BufferReader的用法就用这道题的AC代码记录:

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
10         try{
11             String[] line =(bf.readLine()).split(" ");
12             int n = Integer.parseInt(line[0]);
13             int k = Integer.parseInt(line[1]);
14             int count = 0;
15             while(n-- > 0){
16                 int num = Integer.parseInt(bf.readLine());
17                 if(num%k == 0)
18                     ++count;
19             }
20             System.out.println(count);
21         }
22         catch(IOException e){
23             System.out.print("input error");
24         }
25     }
26 
27 }

 

【CodeChef】Enormous Input Test,布布扣,bubuko.com

【CodeChef】Enormous Input Test

标签:style   blog   http   color   java   os   strong   io   

原文地址:http://www.cnblogs.com/sunshineatnoon/p/3881145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!