pangfengliu / programmingtasks

programming tasks from my courses
67 stars 17 forks source link

Conveyor Belt #349

Open littlehug opened 5 years ago

littlehug commented 5 years ago

Write a function to simulate a circular conveyor belt.

Task Description

You are given N integers to represent a circular conveyor belt of 64N bits in length. Each bit of the integers represents whether there is a packet on the belt or not. In every second, the belt moves to the left (the most significant bit) by one bit, and the leftmost bit of the left most integer is moved to the rightmost bit (the least significant bit ) of the rightmost integer because the belt is circular. Write a function to determine the conveyor belt status after T seconds.

We illustrate the rotation by an example. Let N be 2 and the integer sequence be {2017, -1121}. Initially (T = 0) the conveyor belt is like this. 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 1110 0001 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1011 1001 1111

After 10 seconds (T= 10) the conveyor belt will be like this, and the integer sequence becomes {2066431, -1147904}. 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 1111 1000 0111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1110 1110 0111 1100 0000 0000

After 64 seconds (T = 64) the conveyor belt will be like this, and the integer sequence becomes {-1121, 2017}. 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1011 1001 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 1110 0001

Finally after 100 seconds (T = 100) the conveyor belt is like this, and the integer sequence becomes {-77034533421056, 138675904053247}. 1111 1111 1111 1111 1011 1001 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 1110 0001 1111 1111 1111 1111 1111 1111 1111 1111 1111

You may use the following main function to test your function.

#include<stdio.h>
#include"transmission.h"
#define MAXSIZE 1048576
long long int belt[MAXSIZE];
int main(){
    int N,T;
    scanf("%d%d",&N,&T);
    for(int i=0;i<N;i++)
        scanf("%lld",&belt[i]);
    transmission(belt,N,T);
    for(int i=0;i<N;i++)
        printf("%lld%s",belt[i],i==N-1?"":" ");
    return 0;
}

The prototype of the function you need to implement is as follows.

void transmission(long long int belt[],int N,int T);

The transmission.h is as follow:

void transmission(long long int belt[],int N,int T);

Subtask