C++程式:給所有人發放獎金後,在不超過100分的條件下,最多有多少學生及格


在這個問題中,我們得到一個大小為n的陣列stu[],表示班級裡學生的成績。每個學生的最高分是100分,學生需要50分才能透過考試。我們的任務是編寫一個程式來查詢在給所有人發放獎金並且不超過100分的情況下,最多有多少學生及格。

問題描述 − 我們需要給學生髮放獎金分數以使其及格,但獎金分數將發放給所有學生。我們的任務是透過發放獎金分數來最大化及格的學生人數,但任何學生在發放獎金後不得超過100分。然後返回及格的學生的最大人數。

讓我們舉個例子來理解這個問題:

輸入

stu[] = {45, 32, 78, 10, 53, 67}

輸出

5

解釋

All students passed are :
45 + 22 = 67
32 + 22 = 54
78 + 22 = 100
53 + 22 = 75
67 + 22 = 89

解決方案方法

為了解決這個問題,我們需要給學生分數,但需要考慮的一件事是任何學生的最高分都不應超過100分。因此,可以給予的最大獎金是

Student with max marks(M) + bonus = 100
Bonus = 100 − M

然後我們將此獎金新增到學生的當前分數中。如果超過50分,學生就及格了。結果將是所有此類學生的計數。

演算法

初始化

passCount = 0;

步驟1

Find the student with maximum marks, maxMarks.

步驟2

Calculate bonus that can be given to all students, bonus = 100 − maxMarks.

步驟3

Loop for i −> 0 to n−1

步驟3.1

if(stu[i] + bonus >= 50), passCount++.

步驟4

return passCount.

示例

演示我們解決方案工作的程式:

 線上演示

#include<iostream>
using namespace std;
int calcPassCount(int stu[], int n) {
   int maxMarks = stu[0];
   for(int i = 1; i < n; i++){
      if(stu[i] > maxMarks)
      maxMarks = stu[i];
   }
   int bonusMarks = 100 − maxMarks;
   int passCount = 0;
   for(int i=0; i<n; i++) {
      if(stu[i] + bonusMarks >= 50)
      passCount ++;
   }
   return passCount;
}
int main() {
   int stu[] = {45, 32, 78, 10, 53, 67};
   int n = sizeof(stu)/sizeof(stu[0]);
   cout<<"The Maximum students to pass after giving bonus to everybody is "<<calcPassCount(stu, n);
   return 0;
}

輸出

The Maximum students to pass after giving bonus to everybody is 5

更新於:2020年12月9日

瀏覽量:111

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.