【CodeChef】Enormous Input Test

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 }
复制代码

 

posted @   SunshineAtNoon  阅读(264)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示