Tuesday, January 22, 2019

Intervals 1

Write a program that, given two intervals, computes the interval corresponding to their intersection, or tells that it is empty.

Input : Input consists of four integer numbers a1, b1, a2, b2 that represent the intervals [a1,b1] and [a2,b2]. Assume a1≤ b1 and a2≤ b2.

Output : Print “[]” if their intersection is empty, or “[x,y]” if this is their non-empty intersection.

#include <iostream>
using namespace std;

int main () {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    
    if (c>b and c>a) cout << "[" << "]" << endl;
    else if (c < a and d < a) cout << "[]" << endl;
    
    else if (a >= c) {
        if (b >= d) {
            cout << "[" << a << "," << d << "]" << endl;
        }
        else {
            cout << "[" << a << "," << b << "]" << endl;
        }
    }
    else {
        if (b >= d) cout << "[" << c << "," << d << "]" << endl;
        else cout << "[" << c << "," << b << "]" << endl;
    }
}

No comments:

Post a Comment

Modelo OSI

Open System Interconnection , Interconexión de Sistemas Abiertos, es un modelo para estudiar las categorías en que se pueden dividir los pr...