In: Computer Science
c++. please read this task carefully.
First, try given examples of input to your code, if it works, then send it here.
Create structure Applicants with following fields:
struct applicants{
int id;
string name;
string surname;
int subject1,subject2,subject3,selectedSubject;
string specialCase;
int total; };
SpecialCase (if applicant has "Awardee of Olympiads",
then this field is "true", otherwise "false").
Do not forget that all "Awardees of Olympiads" will gain grants
automatically.
If the total points are same your algorithm have to choose an
applicant with higher SelectedSubject (even with
SpecialCase)
Assuming that you have only K grants to
distribute.
Note: You have to design your program using structures and functions.
Input:
First line contains M(total amount of
applicant) and K(amount of grants that
have to be distributed) (0 < M < 1001) and
(1<=K<=M).
Then N lines are provided in format described above.
Output:
K lines sorted by total points( Total Points = Subject1 + Subject2
+ Subject3 + SelectedSubject ) in format:
ID Name Surname Total_sum_of_points
example of input:
(There can be any inputs, it's just one of an examples)
(No means that they are not "Awardee of Olympiads")
10 5
1 Kimmy Marrow 12 23 4 14 NO
2 Baythen Mcclay 6 6 19 3 NO
3 Christen Stew 11 7 3 5 NO
4 Plein Phillip 15 19 8 4 NO
5 Beist Constant 15 3 17 11 NO
6 Sam Said 7 17 18 1 NO
7 Maclay Istan 5 5 16 17 NO
8 Sara Agit 15 17 24 18 YES
9 Nicolya Kim 24 12 23 23 NO
10 Neit Dimond 22 1 18 7 YES
Output:
9 Nicolya Kim 82
8 Sara Agit 74
1 Kimmy Marrow 53
10 Neit Dimond 48
5 Beist Constant 46
Algorithm :
Step 1 : Take all the applicants data in a vector of
applicants type.
Step 2 : Then , sort that vector using your own
compare function.
Step 3 : In compare func , we compare the total
values if they are not equal , otherwise we compare selectedsubject
values.
Step 4 : Now , print first k values of the
vector.
Output :
Here's Code :
#include<bits/stdc++.h>
using namespace std;
struct applicants{
int id;
string name;
string surname;
int subject1,subject2,subject3,selectedSubject;
string specialCase;
int total;
};
static bool compare(applicants a1,applicants a2){
if(a1.total == a2.total)
return a1.selectedSubject > a2.selectedSubject;
return a1.total > a2.total;
}
int main()
{
vector<applicants> v,ans;
int n,k;
cin>>n>>k;
int id,sub1,sub2,sub3,selsub;
string name ,surname,special;
for(int i=0;i<n;i++)
{
cin>>id>>name>>surname>>sub1>>sub2>>sub3>>selsub>>special;
applicants a1 ;
a1.id = id;
a1.name = name;
a1.surname = surname;
a1.subject1 = sub1;
a1.subject2 = sub2;
a1.subject3 = sub3;
a1.selectedSubject = selsub;
a1.total = sub1 + sub2 + sub3 + selsub;
a1.specialCase = special;
if(special == "YES")
ans.push_back(a1);
else
v.push_back(a1);
}
int z = ans.size();
sort(v.begin(),v.end(),compare);
cout<<"Output : "<<endl;
for(int i=0;i<k-z;i++){
ans.push_back(v[i]);
}
sort(ans.begin(),ans.end(),compare);
for(int i=0;i<ans.size();i++){
cout<<ans[i].id<<" "<<ans[i].name<<" "<<ans[i].surname<<" "<<ans[i].total<<endl;
}
return 0;
}