circular list jump

Asked

Viewed 40 times

-1

Good evening guys, I’m in need of help with a job.

Following:

The program must receive 3 input variables

m= total number of persons, p= position of chosen person, n= number of jumps.

the person who is in the position where the jump falls dies, the program must run until only 1 survivor.

at the end should be displayed DEAD and the survivor number if the jump falls in the chosen position and ALIVE if it does not fall.

I made a code but it’s not quite right, the problem is that the program must continue running until there is only one survivor, and every round must be eliminated the numbers that 'die', I’m not able to do this only with counter and vector. I thought of implementing with a circular list but do not know how to do, someone can help?

If need be I am an image for better understanding.

click here to see image

int main (){

int m, p, n,i, morre, score,sobrevive, j=0;

scanf("%d %d %d",&m , &p, &n);

int vet[m];
    for(i=1;i<=m;i++){
        vet[i]=i;
    }
    morre=vet[n+1];
    i=1;
    for(j=0;j<m;j++){
            if(i>m){
                i=i-m;
                morre = vet[i];
            }
            if(morre==p){
                score=1;
                sobrevive=vet[i--]; 
            }
            i=i+n;
            morre = vet[i];     
        }

if(score==1){
    printf("MORTO \n");
}else{
    printf("VIVO");
}

}

  • Perhaps it is best to declare a vector of m positions, which represents whether the person is dead or alive, and initialize it with all alive. Jump, in a circular way, n live and mark as dead. It does not make much sense to initialize the vector with its own index. Stay in a loop while you have more than one live one. Evaluate whether there will always be a solution or can reach an insoluble situation.

  • yes, but I don’t know how to make the vector stop when there’s only one alive, I don’t know what condition to put, and I don’t know how to make him go back to the beginning of the Indian when I get to the last number of the Indian and I don’t know how to make him not count those who are dead and that’s what I need help with

  • You can set a counter by initializing it with the total number of people and each death decreases by 1 until it reaches 1.

  • yes I tried it but it didn’t totally work in some cases test didn’t hit, I ended up implementing another code with list but thanks for the help

1 answer

0

See if this is what you want:

#include <stdio.h>
#define VIVO 0
#define MORTO 1
int main (){
    int m, p, n, i, sobreviventes, pos, j, k, l;
    scanf("%d %d %d",&m , &p, &n);
    int vet[m];
    for(i=0; i<m; i++){
        vet[i] = VIVO;
    }
    sobreviventes = m;
    pos = p - 1;
    while (sobreviventes > 1) {
        j=0;
        k = pos;
        while (j < n) {
            k %= m;
            if (vet[k] == VIVO)
                j++;
            k++;
        }
        vet[--k] = MORTO;
        sobreviventes--;
        pos = k;
        printf("\nSobreviventes: %d\n", sobreviventes);
        for (l=0; l<m; l++)
            printf("\t%d: %d", l, vet[l]);
    }
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.