[NYOJ] [1098] Grade Conversion

Problem Description

Loading...

Problem Analysis

I hesitated about whether to post this one
because I’d already posted the HDU version
but since this slightly modified one tripped me up
and mainly because the reference solution is nicely written
I’ll just post it

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		if(n<60)
			printf("E\n");
		if(n>=60&&n<70)
			printf("D\n");
		if(n>=70&&n<80)
			printf("C\n");
		if(n>=80&&n<90)
			printf("B\n");
		if(n>=90&&n<=100)
			printf("A\n");
	}
	return 0;
}

In the reference solution, a division combined with a switch expresses the conditions cleverly

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
#include<iostream>
using namespace std;
int main() {
	int n,s;
	cin>>n;
	while(n--) {
		cin>>s;
		switch(s/10) {
			case 10:
			case 9:
				cout<<"A"<<endl;
				break;
			case 8:
				cout<<"B"<<endl;
				break;
			case 7:
				cout<<"C"<<endl;
				break;
			case 6:
				cout<<"D"<<endl;
				break;
			default:
				cout<<"E"<<endl;
				break;
		}
	}
}

Problem link: Score Conversion

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...