-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.
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.
– anonimo
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
– Mazze
You can set a counter by initializing it with the total number of people and each death decreases by 1 until it reaches 1.
– anonimo
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
– Mazze