Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Google Kickstart 2020第C轮:倒计时-代码不起作用_Java - Fatal编程技术网

Java Google Kickstart 2020第C轮:倒计时-代码不起作用

Java Google Kickstart 2020第C轮:倒计时-代码不起作用,java,Java,当我在GoogleKickstart上提交代码时,第一个测试集的答案是错误的,即使示例输入和输出匹配。我真的不知道谷歌为什么不接受这个代码 给定一个N个正整数的数组,求K-倒计时的个数,其中K-倒计时是一个连续的子数组,如果它的长度为K,并且包含按该顺序排列的整数K,K-1,K-2,…,2,1 输入: 输入的第一行给出了测试用例的数量,然后是T.T测试用例。每个测试用例从一行开始,该行包含整数N和K。第二行包含N个整数。第i个整数是Ai 输出: 对于每个测试用例,输出包含用例x:y的一行,其中x

当我在GoogleKickstart上提交代码时,第一个测试集的答案是错误的,即使示例输入和输出匹配。我真的不知道谷歌为什么不接受这个代码

给定一个N个正整数的数组,求K-倒计时的个数,其中K-倒计时是一个连续的子数组,如果它的长度为K,并且包含按该顺序排列的整数K,K-1,K-2,…,2,1

输入:

输入的第一行给出了测试用例的数量,然后是T.T测试用例。每个测试用例从一行开始,该行包含整数N和K。第二行包含N个整数。第i个整数是Ai

输出:

对于每个测试用例,输出包含用例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;
    }
}

这是我的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;
    }
}

假设数组中有一半的倒计时,下面的数字是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;
    }
}

以下是我接受的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));
        }
    }
}

复杂性取决于其中n=输入数组的长度,这不是一个有用的答案,特别是考虑到这是一个与java相关的问题。