Advertisement
Spocoman

02. Exam Preparation

Sep 10th, 2023
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int badGrades;
  8.     cin >> badGrades;
  9.     cin.ignore();
  10.  
  11.     string task, lastProblem;
  12.     getline(cin, task);
  13.  
  14.     int badGradesCounter = 0, tasksCount = 0;
  15.     double gradesTotal = 0, grade;
  16.  
  17.     while (task != "Enough") {
  18.         cin >> grade;
  19.         cin.ignore();
  20.  
  21.         gradesTotal += grade;
  22.         tasksCount++;
  23.  
  24.         if (grade <= 4) {
  25.             badGradesCounter++;
  26.  
  27.             if (badGradesCounter == badGrades) {
  28.                 printf("You need a break, %i poor grades.\n", badGradesCounter);
  29.                 break;
  30.             }
  31.         }
  32.  
  33.         lastProblem = task;
  34.         getline(cin, task);
  35.     }
  36.  
  37.     if (task == "Enough") {
  38.         printf("Average score: %.2f\n", gradesTotal / tasksCount);
  39.         printf("Number of problems: %i\n", tasksCount);
  40.         printf("Last problem: %s\n", lastProblem.c_str());
  41.     }
  42.    
  43.     return 0;
  44. }
  45.  
  46. Решение с тернарен оператор:
  47.  
  48. #include <iostream>
  49. #include <string>
  50.  
  51. using namespace std;
  52.  
  53. int main() {
  54.     int badGrades;
  55.     cin >> badGrades;
  56.     cin.ignore();
  57.  
  58.     string task, lastProblem;
  59.     getline(cin, task);
  60.  
  61.     int badGradesCounter = 0, tasksCount = 0;
  62.     double gradesTotal = 0, grade;
  63.  
  64.     while (task != "Enough" && badGradesCounter != badGrades) {
  65.         cin >> grade;
  66.         cin.ignore();
  67.  
  68.         gradesTotal += grade;
  69.         tasksCount++;
  70.  
  71.         if (grade <= 4) {
  72.             badGradesCounter++;
  73.         }
  74.  
  75.         lastProblem = task;
  76.         getline(cin, task);
  77.     }
  78.  
  79.     task == "Enough" ?
  80.         printf("Average score: %.2f\nNumber of problems: %i\nLast problem: %s\n", gradesTotal / tasksCount, tasksCount, lastProblem.c_str()) :
  81.         printf("You need a break, %i poor grades.\n", badGradesCounter);
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement