Известны год, номер месяца и день рождения каждого из двух человек. Определить, кто из них старше.
Ответы
Ответ:
Объяснение:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
long d1, d2;
int m1, y1;
int m2, y2;
cin >> d1;
cin >> m1;
cin>> y1;
if(m1>12 || d1>31){
cout<<"Некорректная дата. Повторно запустите программу";
return 0;
}
cin >> d2;
cin>> m2;
cin>> y2;
if(m2>12 || d2>31){
cout<<"Некорректная дата. Повторно запустите программу";
return 0;
}
d1 = d1 + (m1 * 30) + (y1 * 365);
d2 = d2 + (m2 * 30) + (y2 * 365);
if(d2 > d1){
cout << "Старше первый";}
else if (d2 < d1){
cout << "Старше второй";}
else if (d2 == d1){
cout<<"Они равны! ";
}
return 0;
}
Объяснение:
#include <iostream>
using namespace std;
int main() {
int year1, month1, day1; // birth date of person 1
int year2, month2, day2; // birth date of person 2
// read birth dates of both people
cout << "Enter birth year, month, and day of person 1: ";
cin >> year1 >> month1 >> day1;
cout << "Enter birth year, month, and day of person 2: ";
cin >> year2 >> month2 >> day2;
// compare birth years
if (year1 > year2) {
cout << "Person 1 is older.\n";
} else if (year2 > year1) {
cout << "Person 2 is older.\n";
} else { // birth years are equal, compare months
if (month1 > month2) {
cout << "Person 1 is older.\n";
} else if (month2 > month1) {
cout << "Person 2 is older.\n";
} else { // birth months are equal, compare days
if (day1 > day2) {
cout << "Person 1 is older.\n";
} else if (day2 > day1) {
cout << "Person 2 is older.\n";
} else { // birth days are equal
cout << "Both people have the same birth date.\n";
}
}
}
return 0;
}