In: Computer Science
Using C language, and describe the algorithm design
n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two numbers, replace the number on his/her whiteboard with the average finally. The game terminates when any one has to write a non-integer number on his/her whiteboard.
Given the number of people, the number of passed rounds and the integer numbers on their whiteboards at this moment, you are asked the integer numbers on their whiteboards at the very beginning. The number of rounds increases by 1 when changing the number on the whiteboard and new numbers are all integers.
Input the number of people n and the number of rounds t on the first line. On the next line are numbers on their whiteboards after t rounds.
Output the integer numbers on their whiteboards at the very beginning.
3 2
3 1 2
6 -2 2
/* C program to play a cicrcular game*/
/* include necessary header files */
#include <stdio.h>
#include <stdlib.h>
/* Driver method */
int main(){
/* declare variable(s) */
int n, t, i, j, sum;
/* Take input(s) */
scanf("%d%d",&n,&t);
/* declare array of size n*/
int end[100] = {0};
/* declare temporary array of size n*/
int temp[100] ={0};
/* take input of array */
for(i=0; i<n ; i++)
scanf("%d",&end[i]);
// calculate the values in each step
// in backward manner
i = 1;
while(i <= t){
sum = 0;
for(j = 0; j<n ;j++){
/* double the values */
end[j] = 2 * end[j];
sum += end[j];
}
// divide by 2
sum = sum/2;
/* calculate the value in previous step */
for(j = 0; j<n ;j++){
if(j - 1<0)
temp[j] = sum - (end[(j+1)%n]+end[n - 1]);
else
temp[j] = sum - (end[(j+1)%n]+end[(j-1)%n]);
}
/* copy data from temp array to end array*/
for(j = 0; j<n ;j++)
end[j] = temp[j];
// increment value
i = i + 1;
}
/* display array */
for(j = 0; j<n ;j++)
printf("%d ",end[j]);
return 0;
}
___________________________________________________________________
___________________________________________________________________
3 2
3 1 2
6 -2 2
___________________________________________________________________
Note: If you have queries or confusion regarding this
question, please leave a comment. I would be happy to help you. If
you find it to be useful, please upvote.