In: Computer Science
Using C language
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
#include <stdio.h> int main() { int n,t; scanf("%d%d",&n,&t); int arr[n]; int i; for(i=0;i<n;i++){ scanf("%d",&arr[i]); } int j; for(i=0;i<t;i++){ int sum = 0; int tempArr[n]; // Create a temporary array to store the current state for(j=0;j<n;j++){ sum += arr[j]; tempArr[j] = arr[j]; } for(j=0;j<n;j++){ arr[j] = 2*tempArr[(j+1)%n] + 2*tempArr[(j-1+n)%n] - sum; // New element = prev[i+1]+prev[i-1] - sum(all elements excluding the two just added) } } for(j=0;j<n;j++){ printf("%d ",arr[j]); } printf("\n"); }
Explanation:
This code is based on the calculations I did to figure out the formula of getting a round back.
It is basically this.
PrevRoundElement[i] = CurrRoundElement[i+1] + CurrRoundElement[i-1] - SumOfAllCurrRoundElements(excluding (i+1) and (i-1) elements)
If you have doubt you can ask in the comments below