Предмет: Информатика, автор: OderaaOrwa

На языке С++! Помогите, пожалуйста.

Студент начал решать задачи данного урока программирования, когда
электронные часы показывали h1 часов и min1 минут, а закончил, когда было h2
часов и min2 минут. Составьте программу, позволяющую определить, сколько
времени студент решал эти задачи. (Будем считать, что задачи решались не
дольше суток).

Ответы

Автор ответа: WellUnknownUser
2

Ответ:

Если не заморачиваться с проверками:

1)

#include <iostream>

using namespace std;

int main()

{

   unsigned short int h1,h2,m1,m2;

   cout << "Enter start time: ";

   cin >> h1 >> m1;

   cout << "Enter end time: ";

   cin >> h2 >> m2;

   unsigned short int SpendedTime = (h2 * 60 + m2) - (h1 * 60 + m1);

   unsigned short int SpendedHours = SpendedTime / 60;

   unsigned short int SpendedMinutes = SpendedTime - SpendedHours * 60;

   cout << "Spended time: ";

   

   if(SpendedHours != 0){

       cout << SpendedHours;

       if(SpendedHours != 1){

           cout << " hours";

       }else{

           cout << " hour";

       }

       cout << " and ";

   }

   

   cout << SpendedMinutes;

   

   if(SpendedMinutes != 1){

       cout << " minutes";

   }else{

       cout << " minute";

   }

   return 0;

}

Если заморачиваться:

2)

#include <iostream>

using namespace std;

int main()

{

   unsigned short int h1,h2,m1,m2;

   bool Passed = false;

   while(Passed != true){

       cout << "Enter start time: ";

       cin >> h1 >> m1;

       cout << "Enter end time: ";

       cin >> h2 >> m2;

       if(h2 >= h1){

           if(h2 > h1){

               Passed = true;

               break;

           }else{

               if(m2 >= m1){

                   Passed = true;

                   break;

               }

           }

       }

       cout << "Error: entered wrong time interval \n";

   }

   unsigned short int SpendedTime = (h2 * 60 + m2) - (h1 * 60 + m1);

   unsigned short int SpendedHours = SpendedTime / 60;

   unsigned short int SpendedMinutes = SpendedTime - SpendedHours * 60;

   cout << "Spended time: ";

   

   if(SpendedHours != 0){

       cout << SpendedHours;

       if(SpendedHours != 1){

           cout << " hours";

       }else{

           cout << " hour";

       }

       cout << " and ";

   }

   

   cout << SpendedMinutes;

   

   if(SpendedMinutes != 1){

       cout << " minutes";

   }else{

       cout << " minute";

   }

   

   return 0;

}

Объяснение:


OderaaOrwa: чекнул первый. сравнил мой и твой ответ, на удивление правильный. спасибо!
Похожие вопросы
Предмет: Английский язык, автор: illab4834
1.
Complete the sentences with the Present Perfect Simple of the verbs in brackets.
I don’t understand. You _____ (not explain) it very well.


а didn't explain
б have not explain
в haven't explained
г hasn't explained

2.
Complete the sentences with the Present Perfect Simple of the verbs in brackets.
Ouch! I think I _____ (sprain) my ankle.


а sprained
б sprain
в have sprained
г has sprained

3.
A: How long / When did you buy your new bike?
B: Two weeks ago / since.


а How long, ago
б How long, since
в When, since
г When, ago

4.
Who won the _____ this afternoon?


а court
б coach
в race

5.
A: How long / When have you had this car?
B: I’ve had it for / since March.


а When, since
б How long, for
в How long, since
г When, for

6.
Complete the sentences with the Present Perfect Simple or Past Simple.
Grandma _____ us last weekend.

а has visited
б visited

7.
I write lyrics / texts for the songs and I also sing.


а lyrics
б texts

8.
I’ve read three books so far / yet.


а yet
б so far

9.
Complete the sentences with the Present Perfect Simple of the verbs in brackets.

___ Lucy and Charlie ___ (visit) the National Museum yet?


а Did.....visited
б Have.....visit
в Has.....visited
г Have.....visited

10.
Complete the sentences with the Present Perfect Simple or Past Simple.
Mrs. Smith _____ at this school for 5 years.


а worked
б has worked

11.
I watched a _____ about killer whales last night.


а soap opera
б wildlife documentary
г sitcom

12.
A: Have you ever / never heard this song before?
B: Yes. I’ve heard it once / since before.


а never, once
б ever, once
в ever, since
г never, since

13.
Complete the sentences with the Present Perfect Simple or Past Simple.
I _____ anything today.


а didn't eat
б haven't eaten

14.
Complete the sentences with the Present Perfect Simple of the verbs in brackets.
_____ Andy ever _____ (have) lessons with Mrs Kennedy?


а Have.....have
б Has.....had
в Have....had
г Has.....have

15.
When the band came on stage, the crowd went wrong / wild.


а wild
б wrong

16.
How many songs are there on this album / performance?


а album
б performance

17.
My favourite band didn’t go on _____ this year.


а gig
б award
в tour

18.
Tom's team has won three championships / spectators so far.


а championships
б spectators

19.
After the concert, I waited to get the singer’s _____.


а autograph
б audience
в fan



20.
Complete the sentences with the Present Perfect Simple of the verbs in brackets.
Paul’s worried because he thinks he _____ (lose) his MP3 player.


а have lose
б has lose
в has lost
г have lost