-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTell the positions.cpp
88 lines (67 loc) · 1.93 KB
/
Tell the positions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
PROBLEM:
In a class there are ‘n’ number of students. They have three different subjects: Data Structures, Algorithm Design & Analysis and Operating Systems. Marks for each subject of all the students are provided to you. You have to tell the position of each student in the class. Print the names of each student according to their position in class. Tie is broken on the basis of their roll numbers. Between two students having same marks, the one with less roll number will have higher rank. The input is provided in order of roll number.
Input Format:
First line will have a single integer ‘n’, denoting the number of students in the class.
Next ‘n’ lines each will have one string denoting the name of student and three space separated integers m1, m2, m3 denoting the marks in three subjects.
Output Format:
Print ‘n’ lines having two values: First, the position of student in the class and second his name.
Constraints:
1 <= n <= 10^5
0 <= m1, m2, m3 <= 100
Sample Input:
3
Mohit 94 85 97
Shubham 93 91 94
Rishabh 95 81 99
Sample Output:
1 Shubham
2 Mohit
3 Rishabh
CODE:
*/
#include <bits/stdc++.h>
using namespace std;
class student{
public:
string name;
int marks;
int roll;
};
bool mycompare(student a, student b){
if (a.marks!=b.marks)
{
return (a.marks>b.marks);
}else{
return(a.roll<b.roll);
}
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int n;
std::cin>>n;
//std::vector<string> name; //ncross1
std::vector<student> stud; //ncross3
for (int i = 0; i < n; ++i)
{
string temp_name;
int sub1;
int sub2;
int sub3;
cin>>temp_name>>sub1>>sub2>>sub3;
int sum = sub1+sub2+sub3;
student temp_sub;
temp_sub.name = temp_name;
temp_sub.marks = sum;
temp_sub. roll = i+1;
stud.push_back(temp_sub);
}
sort(stud.begin(), stud.end(), mycompare);
for (int i = 0; i < n; ++i)
{
cout << i+1<<" "<<stud.at(i).name<<'\n';
}
return 0 ;
}