zoukankan      html  css  js  c++  java
  • 【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 }
  • 相关阅读:
    Javascript倒计时
    Windows Live Writer的使用
    liunx下查看服务器硬件信息
    Ant+JSDocTookit生成Javascript文档
    文本文件及二进制文件的大小, Unicode字符编码
    读书笔记之:Linux与Unix shell编程指南v1
    读书笔记之:Linux程序设计(第2版)
    JM8.6中的encode_one_macroblock注释
    在fedora中挂载windows分区
    fedora14的yum源总结
  • 原文地址:https://www.cnblogs.com/sunshineatnoon/p/3881145.html
Copyright © 2011-2022 走看看