首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Kickstart 2020第C轮:倒计时-代码不起作用

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

Stack Overflow用户
提问于 2020-05-20 13:46:01
回答 3查看 278关注 0票数 0

当我在上提交我的代码时,我得到了第一个测试集的“错误答案”,尽管示例输入和输出匹配。我真的不明白为什么Google不接受这个代码。

任务是,给定一个N正整数数组,求出K-countdowns的个数,其中K-countdown是一个连续子数组,如果它的长度为K,并且按该顺序包含整数K,K-1,K-2,…,2,1。

输入:

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

输出:

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

样本输入:

代码语言:javascript
复制
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

样本输出:

代码语言:javascript
复制
Case #1: 2
Case #2: 0
Case #3: 1

我的逻辑非常简单:我有一个计数器变量x,它从K开始,在整数等于x时递减。如果找到倒计时(递减后的x=0),则答案增加,x设置为等于K。下面是我的代码的关键:

代码语言:javascript
复制
    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;
        }
    }

以下是我的全部代码,以防出现任何微不足道的错误:

代码语言:javascript
复制
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;
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-21 14:52:07

假设数组中有一半的倒计时,下面的数字是K。根据代码,这个数字将被忽略,所以如果有一个后续倒计时,它将不会被计算。下面是主循环应该是什么样子:

代码语言:javascript
复制
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;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-05-21 04:14:36

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

代码语言:javascript
复制
#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
EN

Stack Overflow用户

发布于 2021-02-19 17:17:54

下面是我在java中接受的代码:

代码语言:javascript
复制
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=输入数组的长度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61914731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档