CodeChef : 时间限制 超出以下 java 代码

  • 本文关键字:java 代码 时间 CodeChef java
  • 更新时间 :
  • 英文 :


这是问题的链接: - http://www.codechef.com/problems/INTEST/

以下是代码: -

import java.util.*;
import java.io.*;
class INTEST {
    public static void main(String...s) {
        String str = "";
        try {
            str = new BufferedReader(new InputStreamReader(System. in )).readLine();
        } catch (Exception e) {
            System.out.println(e);
        }
        String[] ar = str.split(" ");
        int n = Integer.parseInt(ar[0]);
        int k = Integer.parseInt(ar[1]);
        int count = 0;
        if (k <= 10000000) {
            int[] t = new int[n];
            for (int i = 0; i <= n - 1; i++) {
                try {
                    t[i] = Integer.parseInt(new BufferedReader(new InputStreamReader(System. in )).readLine());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (t[i] <= 1000000000) {
                    if (t[i] % k == 0) count++;
                } else break;
            }
        }
        System.out.println(count);
    }
}

我从扫描仪更改为缓冲阅读器来读取数据,但它无助于减少时间。

任何帮助我如何减少时间。谢谢。

问题 :

此问题的目的是验证用于读取输入数据的方法是否足够快,以处理带有巨大输入/输出警告的问题。您应该能够在运行时每秒至少处理 2.5MB 的输入数据。

输入

输入以两个正整数 n k (n, k<=10^7) 开头。接下来的 n 行输入包含一个正整数 ti,每行不大于 10^9。

输出

写入单个整数进行输出,表示有多少个整数 ti 可以被 k 整除。例

输入:7 3

1

51

966369

7

9

999996

11

输出:

对象初始化成本很高(即使用new)。 您应该尽可能避免这种情况)。 在这种情况下,您可以创建一次Scanner对象并重用它。

例如

class INTEST {
    public static void main(String...s) {
        String str = "";
         Scanner input=new Scanner(System.in);
        try {
            str = input.readLine();
        } catch (Exception e) {
            System.out.println(e);
        }
        String[] ar = str.split(" ");
        int n = Integer.parseInt(ar[0]);
        int k = Integer.parseInt(ar[1]);
        int count = 0;
        if (k <= 10000000) {
            int[] t = new int[n];
            for (int i = 0; i <= n - 1; i++) {
                try {
                    t[i] = Integer.parseInt(input.readLine());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (t[i] <= 1000000000) {
                    if (t[i] % k == 0) count++;
                } else break;
            }
        }
        System.out.println(count);
    }
}

注意:还可以对代码进行更多优化。例如,使用 nextInt 而不是 nextLine 然后转换为 integer 。此外,您始终可以假设输入,无需一直检查值。

这是

成功运行的最终优化解决方案:)

 import java.util.*;
 import java.text.*;
 import java.io.*;
class INTEST{
public static void main(String... s) {
String str="";
try{
str = new BufferedReader(new InputStreamReader(System.in)).readLine();
}catch(Exception e){System.out.println(e);}
String[] ar = str.split(" ");
int n = Integer.parseInt(ar[0]);
int k = Integer.parseInt(ar[1]);
int count = 0;
if(k<=10000000){
int t=0;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<=n-1;i++){
try{
t  = Integer.parseInt(bf.readLine()) ;
}catch(Exception e){e.printStackTrace();}
 if(t<=1000000000)  
{
if(t%k==0)
count++;
} 
else break;
}}
System.out.println(count);
}} 

最新更新