然后将下一个数字替换为0

然后将下一个数字替换为0

本文介绍了跳过一个数字,然后将下一个数字替换为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.

推荐答案



#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,直到只有一个元素保留在数组中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:18