Advertisement
Spocoman

08. Graduation

Sep 10th, 2023 (edited)
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     string name;
  8.     cin >> name;
  9.  
  10.     int failedExams = 0;
  11.     int completedClass = 0;
  12.     double sumGrades = 0, grade;
  13.  
  14.     while (completedClass < 12 && failedExams < 2) {
  15.         cin >> grade;
  16.  
  17.         if (grade >= 4) {
  18.             sumGrades += grade;
  19.             completedClass++;    
  20.         }
  21.         else {
  22.             failedExams++;    
  23.         }
  24.     }
  25.  
  26.     if (completedClass == 12) {
  27.         printf("%s graduated. Average grade: %.2f\n", name.c_str(), sumGrades / 12);
  28.     }
  29.     else {
  30.         printf("%s has been excluded at %i grade", name.c_str(), completedClass + 1);
  31.     }
  32.     return 0;
  33. }
  34.  
  35. Решение с тернарен оператор:
  36.  
  37. #include <iostream>
  38. #include <string>
  39.  
  40. using namespace std;
  41.  
  42. int main() {
  43.     string name;
  44.     cin >> name;
  45.  
  46.     int failedExams = 0;
  47.     int completedClass = 0;
  48.     double sumGrades = 0, grade;
  49.  
  50.     while (completedClass < 12 && failedExams < 2) {
  51.         cin >> grade;
  52.  
  53.         sumGrades += grade >= 4 ? grade : 0;
  54.         grade >= 4 ? completedClass++ : failedExams++;
  55.     }
  56.  
  57.     completedClass == 12 ?
  58.         printf("%s graduated. Average grade: %.2f\n", name.c_str(), sumGrades / 12) :
  59.         printf("%s has been excluded at %i grade", name.c_str(), completedClass + 1);
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement