Tuesday, January 22, 2019

Add One Second

Write a program that adds one second to a clock time, given its hours, minutes and seconds.

Input : Input consists of three natural numbers h, m and s that represent a clock time, that is, such that h<24, m<60 and s<60.

Output : Print the new clock time defined by h, m and s plus one second in the format “HH:MM:SS”.

#include <iostream>
using namespace std;

int main () { 
    int h, m, s;
    cin >> h >> m >> s;
    
    if (s==59) { m = m+1, s=0;
    }
    else s = s+1;
    
    if (m==60) { m=0, h=h+1;
    }
    
    if (h==24) h=0;
    
    if (h<10) cout << "0" << h << ":";
    else cout << h << ":";
    
    if (m<10) cout << "0" << m << ":";
    else cout << m << ":";
    
    if (s<10) cout << "0" << s << endl;
    else cout << s << endl;
    
    
}

How many seconds are they?

Write a program that converts to seconds a given amount of years, days, hours, minutes and seconds.

Input : The input consists of five natural numbers that represent the years, days, hours, minutes and seconds, respectively.

Output : Write the total number of seconds represented by the input.

Observation : You may assume all the years have 365 days.

#include <iostream>
using namespace std;

int main () { 
    int a, d, h, m, s;
    cin >> a >> d >> h >> m >> s;
    a = a*365*24*3600;
    d= d*86400;
    h=h*3600;
    m=m*60;
    
    int segons;
    segons= a+d+h+m+s;
    cout << segons << endl;
}

Integer division and remainder of an integer number by a natural number

Write a program that reads an integer number a and a natural number b, with b > 0, and prints the integer division d and the remainder r of a divided by b.

Remember that, by definition, d i r must be the only integer numbers such that 0 ≤ r < b and d · b + r = a.

Input : Input consists of a and b, with b > 0.

Output : Print a line with the integer division and the remainder of a divided by b, separated by a space.

#include <iostream>
using namespace std;

int main () {
    int a, b;
    cin >> a >> b;
    
    int d, r;
    if (a%b < 0) {
        r= (a%b) +b ;
        d= a/b -1 ;
        cout << d << " " << r << endl;
    }
    
    else {
        d=a/b;
        r=a-b*d;
        cout << d << " " << r << endl;
    }
    
}

Intervals 3

Write a program that, given two intervals, tells if one is inside the other, and 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 the intervals are equal, ‘1’ if the first is inside the second (but they are not equal), ‘2’ if the second is inside the first (but they are not equal), or ‘?’ otherwise. Also, print “[]” if the 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;
    
   // Print ‘=’ if the intervals are equal, 
   //‘1’ if the first is inside the second (but they are not equal), 
   //‘2’ if the second is inside the first (but they are not equal), 
    // or ‘?’ otherwise.
    
    if (a == c and b == d) cout << "= , "<< "[" << a << "," << b << "]" << endl;
    else {
        
        if ((a >= c and b < d) or (a > c and b <= d)) cout << "1 , "<< "[" << a << "," << b << "]" << endl;
        
        else {
            
            if ((a <= c and b > d) or (a < c and b >= d)) cout << "2 , "<< "[" << c << "," << d << "]" << endl;
            
            else {
                if (a == d) cout << "? , "<< "[" << a << "," << d << "]" << endl;
                else {
                    
                    if (b == c) cout << "? , "<< "[" << b << "," << c << "]" << endl;
                    
                    else {
                        
                        if (c>=a and c<=b and d>=b)  cout << '?' << ' ' << ',' << ' ' << '[' << c << ',' << b << ']' << endl;
                        
                        else {
                            
                            if (a>=c and a<=d and d<=b)  cout << '?' << ' ' << ',' << ' ' << '[' << a << ',' << d << ']' << endl;
                            
                            else {
                                
                                if (d>=a and d<=b and c<=a)  cout << '?' << ' ' << ',' << ' ' << '[' << a << ',' << d << ']' << endl;
                                
                                else  cout << "? , "<< "[]" << endl;
                            }
                        }
                    }
                }
            }
        }
    }
}

Intervals 2

Write a program that, given two intervals, tells if one is inside the other.

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 the intervals are equal, ‘1’ if the first is inside the second (but they are not equal), ‘2’ if the second is inside the first (but they are not equal), or ‘?’ otherwise.

#include <iostream>
using namespace std;

int main () {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    
   // Print ‘=’ if the intervals are equal, 
   //‘1’ if the first is inside the second (but they are not equal), 
   //‘2’ if the second is inside the first (but they are not equal), 
    // or ‘?’ otherwise.
    
    if (a == c and b == d) cout << "=" << endl;
    else {
        
        if ((a >= c and b < d) or (a > c and b <= d)) cout << "1" << endl;
        
        else {
            
            if ((a <= c and b > d) or (a < c and b >= d))cout << "2" << endl;
            
            else 
                            cout << "?" << endl;
        }
    }
}

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;
    }
}

Classification of characters

Write a program that reads a letter, and that tells if it is an uppercase letter or a lowercase letter, and that also tells if it is a vowel or a consonant. Here, assume that the vowels are ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’, and their corresponding uppercase letters.

Input : Input consists of a letter.

Output : Tell if the letter is uppercase or lowercase, and also tell if it is a vowel or a consonant.

#include <iostream> 
using namespace std;

int main (){
    char c;
    cin >> c;
    if (c >= 'a' and c <= 'z' )cout << "lowercase" << endl; 
    else cout << "uppercase" << endl; 
    
    if ((c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u') or (c == 'A' or c == 'E' or c == 'I' or c == 'O' or c == 'U')) cout << "vowel" << endl;
    else cout << "consonant" << endl;
}

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...