Write a program to generate a boolean OR of two digital signals. A signal has two states, a high state and a low state. It is given as an array of positive integers in ascending order. Each pair of integer indicates the range of the signal in a high state. The array size will be even, so that the signal starts with a low state and ends with a low state.
Consider two signals named A and B described as follows:
A = 3 4 5 9 10 11
and
B = 1 2 6 12 13 14
Their boolean OR will be a signal A OR B = 1 2 3 4 5 12 13 14
This is illustrated in the figure below ...
Consider two signals named A and B described as follows:
A = 3 4 5 9 10 11
and
B = 1 2 6 12 13 14
Their boolean OR will be a signal A OR B = 1 2 3 4 5 12 13 14
This is illustrated in the figure below ...
Input Specification:
The input will describe two signals, one on each line. Every line will begin with an integer H, which specifies the number of high states of the signal. This is followed by 2*H positive integers describing the signal, as shown above. Assume that state change will never occur at same time (the integers can be thought of as a timeline) in the two given signals.
Output Specification:
The output signal should be the boolean OR of the two input signals, described in exactly the same manner as the input.
Sample Input and Output:
Input:
3 3 4 5 9 10 11
3 1 2 6 12 13 14
Output:
4 1 2 3 4 5 12 13 14
Input:
5 2 5 6 10 20 23 38 45 51 52
5 1 4 7 9 14 19 21 24 30 31
Output:
7 1 5 6 10 14 19 20 24 30 31 38 45 51 52
Input:
1 1 1000
1 999 10000
Output:
1 1 10000
3 3 4 5 9 10 11
3 1 2 6 12 13 14
Output:
4 1 2 3 4 5 12 13 14
Input:
5 2 5 6 10 20 23 38 45 51 52
5 1 4 7 9 14 19 21 24 30 31
Output:
7 1 5 6 10 14 19 20 24 30 31 38 45 51 52
Input:
1 1 1000
1 999 10000
Output:
1 1 10000
Solutions:
Using C language ....
#include <stdio.h> #include <stdlib.h> int main() { int i,j; //Input array for storing the input data int out[20000]; //Output array for calculating boolean or int output[10000]; //Temproary variables int width,l,h; int k,flag,flag2; //Clearing the array for storing input for(i=0;i<20000;i++) out[i]=0; //Reading first signal scanf("%d",&width); for(i=0;i<width;i++) { scanf("%d %d",&l,&h); for(;l<h;l++) { out[l]=1; } } //Reading second signal and adding it to first scanf("%d",&width); for(i=0;i<width;i++) { scanf("%d %d",&l,&h); for(;l<h;l++) { out[l]=1; } } //Calculating output in output array width=0; k=0; for(i=0;i<20000;i++) { if(out[i]==1 && flag==0) { output[k]=i; flag=1; flag2=1; k++; width++; } else if(out[i]==0) { flag=0; } if(flag==0 && flag2==1) { output[k]=i; flag2=0; k++; } } //Printing output printf("%d",width); for(i=0;i<k;i++) { printf(" %d",output[i]); } printf("\n"); return 0; }
No comments:
Post a Comment