Timed Out和NZEC对CodeChef问题NUKES的看法。

问题描述 投票:0回答:1

在尝试 "核反应堆 "的问题时,我在电脑上得到了结果,但在CodeChef中,有一个0.2秒的时间限制,在提交答案时,我得到了一个TLE(超过时间限制)的错误,在一个测试中,我得到了错误的答案,我不知道是什么原因造成的。

链接 :https:/www.codechef.comproblemsNUKES

我的代码 。

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

public class Main{
    public static void main(String[] args) {
        FastReader fr = new FastReader();

        // I/P
        int a = fr.nextInt();
        int n = fr.nextInt();
        int k = fr.nextInt();

        // ARRAY TO STORE RESULT
        int react[] = new int[k];

        // ARRAY OF ZERO AND ONES
        int temp_one[]=new int[k];
        int temp_zero[]=new int[k];

        for (int i = 0; i < k; i++){
            react[i] = 0;
            temp_zero[i]=react[i];
            temp_one[i]=1;
        }

        while (a != 0) {    // TO REPEAT TILL ALL (A) ARE USED
            int j = 0;

            while(react[j]>=n){     // CHECK(value in K>=A)
                react[j] = 0;
                j++;
            }
            react[j]++;

            if(Arrays.equals(react,temp_one)){ // CHECK(all K are filled)
                react=temp_zero;
            }
            a--;
        }
        for(int i=0;i<k;i++){
            System.out.print(react[i]+" ");
        }
    }
    //////////////////// FAST IO //////////////////////
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

    }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

CodeChef给出的结果

java
1个回答
0
投票

这段代码应该适用于你的算法。

int[] ret = new int[k];
for(int i = (int)(Math.log(a)/Math.log(n+1)); i >= 0; i--) {
    int val = (int)(a/Math.pow(n+1,i));
    a -= val*Math.pow(n+1,i);
    if (i < k) ret[i] = val;
}

ret 是你的返回数组.它基本上找到了a的基数n+1表示,这就是他们希望你得到的答案。如果你想了解更多关于为什么它能工作的信息,请随时提问!

© www.soinside.com 2019 - 2024. All rights reserved.