简体   繁体   English

显示错误 output

[英]Showing wrong output

It is a problem from hackerrank.这是hackerrank的问题。

https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem?h_l=interview&isFullScreen=false&playlist_slugs%5B%5D%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D%5B%5D=warmup https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem?h_l=interview&isFullScreen=false&playlist_slugs%5B%5D%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D% 5B%5D%5B%5D=热身

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int jump=0;
    int a[101];
    a[0]=0;
    for (int i = 1; i < n; i++) {
        cin>> a[i];
    }
    for(int j=1;j<n;j++)
    {
        if(a[j]==1)
        {
            jump++;
        }
        if(a[j]==0)
        {
            jump++;
            if(a[j-1]==0 && a[j-1]==1)
                jump--;
        }
    }
    cout<<jump;
    return 0;
}

Emma is playing a new mobile game that starts with consecutively numbered clouds. Emma 正在玩一个新的手机游戏,该游戏以连续编号的云开始。 Some of the clouds are thunderheads and others are cumulus.有些云是雷雨云,有些是积云。 She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. She must avoid the thunderheads.她可以跳上任何数量等于当前云数加 1 或 2 的积云。她必须避开雷雨云。 Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud.确定 Emma 从起始位置跳到最后一朵云所需的最少跳跃次数。 It is always possible to win the game.总是有可能赢得比赛。

For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided.对于每场比赛,Emma 将获得一组云,如果它们是安全的,则编号为 0,如果必须避免,则编号为 1。 For example, c=[0,1,0,0,0,1,0] indexed from 0...6.例如,c=[0,1,0,0,0,1,0] 从 0...6 索引。 The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5. She could follow the following two paths:0->2->4->6 or 0->2->3->4->6.每个云上的数字是它在列表中的索引,因此她必须避开索引 1 和 5 处的云。她可以遵循以下两条路径:0->2->4->6 或 0->2->3- >4->6。 The first path takes 3 jumps while the second takes 4. So, we have to print the minimum number of jumps needed to win the game.第一条路径需要 3 次跳跃,而第二条路径需要 4 次。因此,我们必须打印赢得比赛所需的最少跳跃次数。

Here's the input/output:这是输入/输出:

7
0 0 1 0 0 1 0

Expected output is: 4预期 output 为:4

My wrong output: 6我错了 output:6

The first obvious problem I see is that the second loop will never end, because j is never incremented.我看到的第一个明显问题是第二个循环永远不会结束,因为j永远不会增加。 Furthermore the condition此外,条件

if(a[j-1]==0 && a[j-1]==1)

makes no sense.没有意义。 a[j-1] can only be either 0 or 1, not both unless you are running this on a quantum computer. a[j-1] 只能是 0 或 1,不能同时是 0 或 1,除非你在量子计算机上运行它。 Fix these issues and work from there.解决这些问题并从那里开始工作。

Loop through every position in the consecutive cloud List advancing your position 2 jumps forward if possible.循环通过连续云列表中的每个 position 推进您的 position 2 如果可能的话向前跳跃。 Otherwise, you just jump once.否则,你只跳一次。 Regardless, you'll count the number of iterations taken until you've reached the end.无论如何,您将计算迭代次数,直到您到达终点。 Also, because each iteration is a jump, you don't need to jump off the last cloud, so go to size() minus one.另外,因为每次迭代都是一次跳跃,所以不需要跳出最后一朵云,所以 go 将 size() 减一。

Time-complexity: O(n) [n = number of clouds]时间复杂度: O(n) [n = 云数]

Space-complexity: O(1)空间复杂度: O(1)

Java 8 Java 8

public static int jumpingOnClouds(List<Integer> c) 
{
    //Track number of jumps
    int jumpCount = 0;
    
    //Keep jumping until you can't anymore
    for(int i = 0; i < c.size()-1; jumpCount++)
    {
        //If possible, long jump (2 clouds forward)
        if(i < c.size() - 2 && c.get(i+2) != 1)
            i+=2;
        else //Otherwise, short jump (1 cloud forward)
            i++;
    }
    
    //Return count of jumps taken
    return jumpCount;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM