Google Kickstart 2020回合C:倒计时-代码无法正常运行

时间:2020-05-20 13:46:01

标签: java

当我在Google Kickstart上提交我的代码时,即使示例输入和输出匹配,我也会为第一个测试集获得“错误答案”。我真的不确定为什么Google不接受该代码。

The task是给定一个 N 个正整数数组,找到 K 个倒数,其中一个 K -countdown如果是长度为 K 的连续子数组,并且按顺序包含整数K,K-1,K-2,...,2、1。

输入:

输入的第一行给出测试用例的数量,T。每个测试用例均以包含整数N和K的行开头。第二行包含N个整数。第i个整数是Ai。

输出:

对于每个测试用例,输出包含Case #x的一行:y,其中x是测试用例编号(从1开始),y是她数组中K倒数的数量。

样本输入:

3
12 3
1 2 3 7 9 3 2 1 8 3 2 1
4 2
101 100 99 98
9 6
100 7 6 5 4 3 2 1 100

样本输出:

Case #1: 2
Case #2: 0
Case #3: 1

我的逻辑非常简单:我有一个计数器变量 x ,该变量以 K 开头,并在整数等于 x 时递减。如果找到倒数计数(递减后 x = 0),则答案会增加,并且 x 设置为等于 K 。这是我的代码的症结所在:

    for(int i=0; i<n; i++) {
        arr[i]=sc.nextLong();
        if(arr[i]==x)
            x--;
        else 
            x=k;
        if(x==0) {
            ans++;
            x=k;
        }
    }

这是我的完整代码,以防出现任何小错误:

import java.util.Scanner;
public class Solution {
    static int t;
    static long n,k;
    static long[] arr;
    static Scanner sc=new Scanner(System.in);

    public static void main(String[] args) {
        t=sc.nextInt();
        for(int i=1; i<=t; i++) {
            n=sc.nextLong();
            k=sc.nextLong();
            System.out.println("Case #"+i+": "+solve());
        }
        sc.close();
    }

    public static long solve() {
        long x=k;
        long ans=0;
        arr=new long[(int)n];
        for(int i=0; i<n; i++) {
            arr[i]=sc.nextLong();
            if(arr[i]==x)
                x--;
            else 
                x=k;
            if(x==0) {
                ans++;
                x=k;
            }
        }
        return ans;
    }
}

3 个答案:

答案 0 :(得分:1)

这是我在o(n)中运行的cpp代码

#include<bits/stdc++.h>
using namespace std;
int main(){
int t,m;
cin>>t;
m=t;
while(t--){
    int n,k;
    cin>>n>>k;
    vector <int> v,v2;
    for(int i=0;i<n;i++){
        int tr;
        cin>>tr;
        v.push_back(tr);
    }
    for(int j=0;j<k;j++){
        int tr=k-j;
        v2.push_back(tr);
    }
    int fg=0;
    for(int i=0;i<n;i++){
        if(v[i]==k){
                int count=0;
            for(int j=0;j<k;j++){
                if(v2[j]==v[i+j])
                    count++;
            }
        if(count==k)
            fg++;
            }
        }
        cout<<"Case #"<<m-t<<": "<<fg<<endl;
    }
}

答案 1 :(得分:0)

假设数组中存在倒数一半,且以下数字为 K 。 根据代码,该数字将被忽略,因此,如果随后进行倒数计时,则不会将其计算在内。这是主循环的样子:

for (int i = 0; i < n; i++) {
    arr[i] = sc.nextLong();

    if (arr[i] == x)
        x--;
    else if (arr[i] == k)
        x = k-1;
    else 
        x = k;

    if (x == 0) {
        ans++;
        x = k;
    }
}

答案 2 :(得分:0)

这是我在java中接受的代码:

import java.util.*;

public class Solution {

    public static int kCountDown(int [] nums, int k) {
        int length = nums.length;
        int endCounter = 0;
        int result = 0;
        for (int i=1; i<length; i++) {
            if (nums[i - 1] - nums[i] == 1)
                endCounter += 1;
            else
                endCounter = 0;
            if (nums[i] == 1 && endCounter >= k - 1)
                result += 1;
        }
        return result;
    }

    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        int test = sc.nextInt();
        for (int t=1; t<=test; t++) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            int [] nums = new int [n];
            for (int i=0; i<n; i++) {
                nums[i] = sc.nextInt();
            }
            System.out.println("Case #" + t + ": " + kCountDown(nums, k));
        }
    }
}

复杂度为 O(n),其中 n = 输入数组的长度