进行更改以使给定代码更有效

时间:2014-09-19 16:15:14

标签: java

https://www.hackerrank.com/contests/csindia/challenges/pin-problem-1

针对上述问题的以下解决方案未提交,“超时终止”消息正在弹出。要成功提交,应在代码下进行哪些更改?请帮帮我。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Solution
{
    public static void main(String args[])
    {
        try{
            BufferedReader br = 
                new BufferedReader(new InputStreamReader(System.in));
            int testcase = Integer.parseInt(br.readLine());
            if (testcase > 0 && testcase <= 100000)
            {
                int outarr[] = new int[testcase];
                for (int i = 0; i < testcase; i++)
                {
                    String str = br.readLine();
                    StringTokenizer st = new StringTokenizer(str," ");
                    int n = Integer.parseInt(st.nextToken());
                    int m = Integer.parseInt(st.nextToken());
                    if (n > 0 && n <= 10000 && m > 0 && m <= 10)
                    {
                        int maincounter = 0;
                        str = br.readLine();
                        String s[];
                        s = str.split(" ");
                        if (s.length == m)
                        {
                            for (int k = 1; k <= n; k++)
                            {
                                int counter = 0;
                                for (int l = 0; l < s.length; l++)
                                {
                                    if (k % (Integer.parseInt(s[l])) == 0)
                                    counter++;
                                }
                                if (counter == s.length)
                                    maincounter++;
                            }
                            outarr[i] = maincounter;
                        }
                        else
                        {
                            System.out.println("Enter the specified values of m not more than that");
                            testcase--;
                        }   
                    }
                    else
                    {
                        System.out.println("Enter value of n in between 1 to 10^4 and value of m in between 0 to 10");
                        testcase--;
                    }
                }
                for (int i = 0; i < testcase-1; i++)
                {
                    System.out.println(+outarr[i]);
                }
                System.out.print(+outarr[testcase-1]);
            }
            else
            {
                System.out.println("Enter the test value in between 1 to 10^5");
            }
        }
        catch(Exception ae)
        {
            System.out.println("Exception caught");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你的代码中有很多不必要的检查会减慢它们的速度,但老实说还不足以让它没有完成。 Hackerrank告诉您输入的界限是什么,因此您无需为此目的进行验证。

我已经完成了代码,并为您添加了可以改进的评论:

import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer;

类解决方案 {     public static void main(String args [])     {         尝试{             BufferedReader br =                 new BufferedReader(new InputStreamReader(System.in));             int testcase = Integer.parseInt(br.readLine());

        // This check is unnecessary, hackerrank told you testcase would be in these
        // bounds.
        if (testcase > 0 && testcase <= 100000)
        {
            int outarr[] = new int[testcase];
            for (int i = 0; i < testcase; i++)
            {
                String str = br.readLine();
                StringTokenizer st = new StringTokenizer(str," ");
                int n = Integer.parseInt(st.nextToken());
                int m = Integer.parseInt(st.nextToken());

                // This check is unnecessary, hackerrank told you m and n would be in these
                // bounds.
                if (n > 0 && n <= 10000 && m > 0 && m <= 10)
                {
                    int maincounter = 0;
                    str = br.readLine();
                    String s[];
                    s = str.split(" ");

                    // This check is unnecessary, hackerrank told you the next line
                    // would have m numbers
                    if (s.length == m)
                    {
                        for (int k = 1; k <= n; k++)
                        {
                            int counter = 0;
                            for (int l = 0; l < s.length; l++)
                            {
                                // Here, instead of checking every number to see
                                // if it is divisible, you can check and see if 
                                // it isn't. If it isn't, then you shouldn't bother
                                // checking the rest of the possible numbers because
                                // you know that k isn't a possible pin number.
                                // Add a flag that indicates if the pin is still valid,
                                // if it isn't, move on to the next pin.
                                if (k % (Integer.parseInt(s[l])) == 0)
                                counter++;
                            }
                            if (counter == s.length)
                                maincounter++;
                        }
                        outarr[i] = maincounter;
                    }
                    else
                    {
                        System.out.println("Enter the specified values of m not more than that");
                        testcase--;
                    }   
                }
                else
                {
                    System.out.println("Enter value of n in between 1 to 10^4 and value of m in between 0 to 10");
                    testcase--;
                }
            }

            // Why the -1? You can just do i < testcase and print it all here
            // instead of on multiple lines.
            for (int i = 0; i < testcase-1; i++)
            {

                System.out.println(+outarr[i]);
            }
            System.out.print(+outarr[testcase-1]);
        }
        else
        {
            System.out.println("Enter the test value in between 1 to 10^5");
        }
    }
    catch(Exception ae)
    {
        System.out.println("Exception caught");
    }
}

更改后的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Solution
{
    public static void main(String args[])
    {
    try{
        BufferedReader br = 
            new BufferedReader(new InputStreamReader(System.in));
        int testcase = Integer.parseInt(br.readLine());
        int outarr[] = new int[testcase];
        for (int i = 0; i < testcase; i++)
        {
            String str = br.readLine();
            StringTokenizer st = new StringTokenizer(str," ");
            int n = Integer.parseInt(st.nextToken());
            int m = Integer.parseInt(st.nextToken());
            int maincounter = 0;
            str = br.readLine();
            String[] s = str.split(" ");
            for (int k = 1; k <= n; k++)
            {
                boolean stillValid = true;
                for (int l = 0; l < m && stillValid; l++)
                {
                    if (k % (Integer.parseInt(s[l])) != 0)
                    {
                        stillValid = false;
                    }
                }
                if (stillValid)
                {
                    maincounter++;
                }
            }
            outarr[i] = maincounter;
        }
        for (int i = 0; i < testcase; i++)
        {
            System.out.println(outarr[i]);
        }
    }
    catch(Exception ae)
    {
        System.out.println("Exception caught " + ae.getMessage());
    }
}