跳过一个数字,然后将下一个数字替换为0,直到只有一个元素保留在数组中. [英] Skip one number and replacenext number with 0 until only one element remains in the array.

查看:137
本文介绍了跳过一个数字,然后将下一个数字替换为0,直到只有一个元素保留在数组中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设将1到2000的数字写到圆棒上,如
所示 下图.然后从1开始,跳过一个数字,并在
中删除一个数字 顺时针方向.编写一个C程序,计算圆上的最后一个剩余数.
在您的程序中,构造一个包含1到2000之间的元素的数组,并执行以下操作:Skip
一个数字,然后删除下一个数字,直到数组中仅剩一个元素为止.确实是
无法从数组中删除元素.因此,不要从
中删除数字 数组,将其替换为零,并在跳过和删除操作期间跳过数组中的零.当
到达数组的末尾,返回到数组的开头.重复此顺序,直到出现
仍然只是一个与零不同的数字.

Suppose that numbers from 1 to 2000 are written onto a circle‐shape stick as shown in the
figure below. Then starting from 1, one number is skipped and one number is deleted in the
clockwise direction. Write a C program which computes the last remaining number on the circle.
In your program construct an array with elements from 1 to 2000 and do the following operation: Skip
one number and delete next number until only one element remains in the array. Indeed, it is
impossible to delete an element from an array. Therefore, instead of deleting a number from the
array, replace it with zero and skip zeros in the array during the skip and delete operations. When the
end of the array is reached go back to the beginning of the array. Repeat this sequence until there
remains just one number different from zero.

推荐答案

好,我了解您的作业分配.您已经对此做了什么?我想您需要帮助,但是,您忘了解释您遇到的问题以及问题所在.:confused:
Ok, I understand your homework assignment. What have you already done on it? I presume that you want some help, however, you forgot to explain where you were having problems and what they were.:confused:


我有点困惑...作业没有没提任何有关获取用户输入的信息.

但是,我看到您正在执行的操作是让用户选择要增加的数字,然后将阵列初始化为该数字.

然后,您从0开始一次浏览该列表,这实际上并没有初始化或没有被代码使用.

然后您去写所有仍然存在的数字.

然后,您只需检查数组中的最后一个数字是否为0,这实际上并没有告诉您任何信息.如果x以偶数开头,那么它将是.如果x为奇数,则不会.

然后,从3开始,如果x = 0,则跳过第四个数字;如果不是x,则从第一个数字开始,跳过第四个数字.

然后,检查x-1 = 0,这是谁,现在知道它是否可以,如果是,则每8位跳过一次……什么?

因此,说真的,您距离分配要求您做什么或如何做的路还很遥远.您是否真的经历过了,看看它能给您带来什么?

首先,根据您给我们提供的信息,您无需要求x ...只需设置数字为1-2000的数组即可.您已经正确地编写了该代码...只是不要停在x处,不要停在2000年.

现在,有很多方法可以解决这个问题.您可以从int开始,用于跟踪不为0的数组元素的数量.然后,可以使用do while直到int =1.当int = 1时,然后遍历数组,找出它是哪个数字.

因此,do while并不简单.您可能会想要有一个boolean变量,该变量告诉您​​是否跳过下一个数字.从数组末尾到开头时,这很有用,因为您可能并不总是跳过第一个可用数字.首先将其设置为true,因为您要做的第一件事就是跳过.

因此,然后,在do while中,首先重置要用来计算数组中还有多少个元素的int.然后,从1开始并遍历每个元素(i++,否i+=4i+=8).首先,检查数字是否为0.如果不是,请检查boolean并查看是否应该跳过该数字.如果不应该跳过它,请将其更改为0并切换boolean告诉您跳过下一个.如果应该跳过它,请切换boolean并增加您使用的int,以跟踪数组中有多少个元素.

抱歉,这有点令人困惑...我不确定如果不为您做该怎么形容.
I''m a little confused...the assignment doesn''t mention anything about getting user input.

But, what I see that you''re doing is letting the user pick which number to go up to, then you initialize the array up to that number.

Then, you go through the list once starting at 0, which isn''t actually initialized or meant to be used by your code.

Then you go and write all of the numbers out that are still there.

Then, you just check if the last number in the array = 0, which really doesn''t tell you anything. If x started as even, then it will be. If x was odd, it won''t be.

Then, you start at three and skip every fourth number if x = 0 and if it doesn''t, you start at one and skip every fourth number.

Then, you check if x-1 = 0, which, who knows by now if it will, and if it is, you skip every 8th...what?

So, seriously, you''re way off on what the assignment is asking you to do or how to do it. Did you actually go through it and see what it gives you?

First, by what you gave us, you don''t need to ask for x...just set up your array with numbers 1-2000. You''ve got that written correctly...just don''t stop at x, stop at 2000.

Now there are a lot of ways that you could approach this. You could start with an int that you could use to keep track of the number of array elements that are not 0. Then, you could use a do while until that int = 1. When that int = 1, then, go through the array and find out which number it is.

So, the do while isn''t simple. You''re probably going to want to have a boolean variable that tells you whether or not to skip the next number. This is useful when going from the end of the array to the beginning, because you may not always skip the first available number. Set it to true to begin with, because the first thing you do is skip.

So, then, in the do while, first, reset your int that you''re using to count how many elements are still in the array. Then, start at 1 and iterate through each element (i++, no i+=4 or i+=8). First, check if the number is 0. If it is not, check the boolean and see if it''s supposed to be skipped. If it''s not supposed to be skipped, change it to 0 and switch the boolean to tell you to skip the next one. If it is supposed to be skipped, switch the boolean and increment your int that your using to keep track of how many elements are in the array.

Sorry if it''s a little confusing...I wasn''t sure how to describe it without doing it for you.


我尝试这样做,但遇到一个我无法解决的问题结束循环:(我发送了代码,但我认为这很糟糕,我必须在星期六之前执行,请帮帮我

i tried to do it but i face a problem i cant end the loops :( i send the code but i think its very bad i have to do it until saturday please help me

#include<stdio.h>

main()
{
    int x,i;
    int a[2001];
    char c;
    do {
        printf("Enter an integer <1 to 2000> ");
        scanf("%d",&x);
        for(i=1;i<=x;i++)
        {
            a[i]=i;
            printf("%d",a[i]);
        }
        printf("\n");
        for(i=0;i<=x;i+=2)
        {
            a[i]=0;
        }
        printf("\n");
        for(i=1;i<=x;i++)
        {
            printf("%d",a[i]);
        }
        printf("\n");

        if(a[x]==0)
        {
            for(i=3;i<=x;i+=4)
            {
                a[i]=0;
            }

        }
        else
        {
            for(i=1;i<=x;i+=4)
            {
                a[i]=0;
            }
        }
        if(a[x-1]==0)
        {
            for(i=5;i<=x;i+=8)
            {
                a[i]=0;
            }
        } 



        for(i=1;i<=x;i++)
        {
            printf("%d",a[i]);
        }
        //printf("%d",a[1]);
        printf("Again? <y for ''YES'', or anything for ''NO''>\n\n");
        scanf("%c",&c);
        scanf("%c",&c);
    } while(c==''y'');
}






这篇关于跳过一个数字,然后将下一个数字替换为0,直到只有一个元素保留在数组中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆