Given the positions of N cars on a straight line, we have to list the valid moves of each car along 2 directions (i.e. right and left).
For example, a line with 3 cars is shown below:
0 1
3 4
5 6
Given the coordinate of car 1 is 3 4, it means car 1 occupies the position of 3 to 4 on the line.
The valid moves of car 1 is 1 0, which means it can move left 1 position but can’t move right
HINTS:
function.h
#define FUNCTION_H #define LINE_SIZE 100 #define MAX_CARS 100 #define SPACE '.' char line[LINE_SIZE]; int cars[MAX_CARS][2]; void list_moves(int); #endif
main.c
#include <stdio.h>
#include "function.h"
int main(void)
{
int i,j,k;
int num_cars;
scanf("%d",&num_cars);
//reset line
for (i=0; i<LINE_SIZE; i++) {
line[i] = SPACE;
}
//read positions of cars and put them on the line
for (i=0; i<num_cars; i++) {
scanf("%d%d", &cars[i][0], &cars[i][1]);
for (j=cars[i][0]; j<=cars[i][1]; j++) {
line[j] = i+'0';
}
}
list_moves(num_cars);
return 0;
}
The first line contains a number N (1<=N<=100), which means the total number of cars.
The following N lines contain the positions of all the N cars, where each line consists of 2 numbers: the first number is the position of the car’s head, and the second number is the position of the car’s tail.
Print out the valid moves of the cars in each line.
Each line consists of 2 numbers, the valid moves toward left and right.
All of the numbers in the same line are separated by a space, and there is a '\n' at the end of each line.