CMRGPACA-Editorial

PROBLEM LINK:

Practice
Setter: Charan Narukulla
Tester: Abhilash Movva

DIFFICULTY:

Cake-Walk

PREREQUISITES:

Math

PROBLEM:

There are N subjects. respective subject grades and credits were given to you. Credits for each subject are based on credits for that subject and grade obtained.(check CMRTC GPA CALCULATOR | CodeChef)
Credit points (CP) = grade point (GP) x credits for that subject(CS).

GPA= Σ CP/ Σ CS

EXPLANATION:

Perform char/string matching to get grade and credits.
First calculate CP for every subject by using formula mentioned. Calculate total credits CS and final GPA= Σ CP/ Σ CS.

Setter's Solution

#include <bits/stdc++.h>
using namespace std;

int calc(string grade){

    if(grade == "o" || grade == "O" ) return 10;
    if(grade == "a+" || grade == "A+") return 9;
    if(grade == "a" || grade == "A") return 8;
    if(grade == "b+" || grade == "B+") return 7;
    if(grade == "b" || grade == "B") return 6;
    if(grade == "c" || grade == "C") return 5;
    if(grade == "f" || grade == "F") return 0;

}

int main(){
int t;
cin >> t;
while(t–){
int numOfSubjects,i; cin >> numOfSubjects;
float cp=0, gp, cs, cpSum=0, csSum=0;
string g;
// char g[2];
int pass=1;
for(i=0; i<numOfSubjects; i++) {
cin >> g >> cs;
gp = calc(g);
if(gp==0){
pass=0;

        }
        cp = gp * cs;
        cpSum += cp;
        csSum += cs;
    }
    if(pass)
    cout << fixed << setprecision(2) << cpSum / csSum << endl;
    else
    std::cout << "FAIL" << std::endl;
}
return 0;

}