题目:
华老师的n个学生参加了一次模拟测验,考出来得得分数很糟糕,但是华老师可以将成绩修改为【0,100】中的任意成绩,所以他想知道,如果要使得所有人成绩的平均分不少于X分,至少改动几个人的分数
输入:
第一行一个数T,共T组数据(T<=10)
接下来对于每组数据:
第一行两个整数n和X(1<=n<=1000,0<=X<=100)
第二行n个整数,第i个数Ai表示第i个同学的成绩
输出:
共T行,每行一个整数,代表最少的人数
样例输入:
2
5 60
59 20 30 90 100
5 60
59 20 10 10 100
样例输出:
1
2
思路:
先对成绩排序,每次修改最小的成绩,看是否满足条件,如果不满足修改第二小的,依次循环。
实现:
1 #include <numeric> 2 #include <bits/stdc++.h> 3 using namespace std; 4 int MAXSCORE = 100; 5 6 int process(vector<int>& scoreVector,int n,int X) { 7 int result = 0; 8 int totalScore = n * X; 9 int sum = accumulate(scoreVector.begin(),scoreVector.end(),0); 10 if(totalScore <= sum) { 11 return 0; 12 } 13 14 sort(scoreVector.begin(),scoreVector.end()); 15 for (int i = 0; i < scoreVector.size(); ++i) { 16 int currentScore = scoreVector.at(i); 17 int diffTotal = totalScore-sum; 18 int maxChange = MAXSCORE-currentScore; 19 20 if (maxChange >= diffTotal) { 21 result++; 22 break; 23 }else { 24 result++; 25 sum+=maxChange; 26 } 27 } 28 29 return result; 30 } 31 32 33 int main() 34 { 35 int T,n,X; 36 cin >> T; 37 vector<int> scoreVector; 38 vector<int> result; 39 while (T>0) { 40 cin >> n >> X; 41 int score = 0; 42 scoreVector.clear(); 43 int temp = n; 44 while(temp>0) { 45 scanf("%d",&score); 46 scoreVector.push_back(score); 47 temp--; 48 } 49 // process 50 //cout << process(scoreVector,n,X) << endl; 51 result.push_back(process(scoreVector,n,X)); 52 T--; 53 } 54 55 for (int i = 0; i < result.size(); ++i) { 56 cout << result.at(i) << endl; 57 } 58 59 }