PROBLEM LINK:
[Practice ] (CodeChef: Practical coding for everyone)
Author: [Siddharth Jha]
(https://www.codechef.com/users/sidjha69)
DIFFICULTY:
SIMPLE
PREREQUISITES:
Sorting
PROBLEM:
In a competition the names, scores and ranks of top 3 players are to be displayed in descending order of marks. No two players can have the same scores.
QUICK EXPLANATION:
Sorting in reverse order based on scores
SOLUTIONS:
Setter's Solution
#include <iostream>
#include <string>
using namespace std;
void swap_string (string& x, string& y) {
string temp;
temp = x;
x = y;
y = temp;
return;
}
void swap_int (int& x, int& y) {
int temp;
temp = x;
x = y;
y = temp;
return;
}
void arrange (int a[], string s[],int n) {
//used bubble sort
for (int i = 0; i<n-1; i++) {
for (int j=0; j<n-i-1;j++) {
if (a[j] < a[j+1]) {
swap_int(a[j], a[j+1]);
swap_string(s[j], s[j+1]);
}
}
}
// print
for (int i = 0; i<n ;i++)
cout << a[i] << " " << s[i] << "\n";
return;
}
int main () {
int n=3;
int a[n];
string s[n];
for (int i = 0; i<n; i++) {
cin >> a[i] >>s[i];
}
arrange (a,s,n);
return 0;
}
Tester's Solution
#include <iostream>
#include<string>
using namespace std;
void arrange(int N1,string player1,int N2,string player2,int N3, string player3) {
if (N1>N2 && N1>N3 && N2>N3)
cout<<N1<<" "<<player1<<"\n"<<N2<<" "<<player2<<"\n"<<N3<<" "<<player3;
else if (N1>N2 && N1>N3 && N3>N2)
cout<<N1<<" "<<player1<<"\n"<<N3<<" "<<player3<<"\n"<<N2<<" "<<player2;
else if (N2>N1 && N2>N3 && N1>N3)
cout<<N2<<" "<<player2<<"\n"<<N1<<" "<<player1<<"\n"<<N3<<" "<<player3;
else if (N2>N1 && N2>N3 && N3>N1)
cout<<N2<<" "<<player2<<"\n"<<N3<<" "<<player3<<"\n"<<N1<<" "<<player1;
else if (N3>N1 && N3>N2 && N2>N1)
cout<<N3<<" "<<player3<<"\n"<<N2<<" "<<player2<<"\n"<<N1<<" "<<player1;
else if (N3>N1 && N3>N2 && N1>N2)
cout<<N3<<" "<<player3<<"\n"<<N1<<" "<<player1<<"\n"<<N2<<" "<<player2;
return;
}
int main() {
int i,N1,N2,N3;
string player1,player2,player3;
cin>>N1>>player1>>N2>>player2>>N3>>player3;
arrange(N1,player1,N2,player2,N3,player3);
return 0;
}
Editorial's Solution
//Search about functions used in it
#include <bits/stdc++.h>
using namespace std;
int main() {
int n=3;
int score;
string name;
map <int,string, greater<int>> m; //greater<int> added so that it sorts in descending order default is ascending order
for (int i=0;i<n;i++) {
cin >> score >> name;
m[score] = name; //m.insert({score,name})
}
for (auto& i:m) {
cout << i.first <<" " << i.second << "\n";
}
}