題目鏈接:點擊打開鏈接
原文:
The calculation of GPA
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29814 Accepted Submission(s): 7102
一般大學採用之計分法
A90 - 100 4 點
B80 - 89 3 點
C70 - 79 2 點
D60 - 69 1 點
E0 - 59 0 點
例如:某位學生修習三門課,其課目、學分及成績分別爲:
英文:三學分、92 分;化學:五學分、80 分;數學:二學分、60分,則GPA的算法如下:
科目 學分 分數 點數 分數×點數
英文 3 92 4 12
化學 5 80 3 15
數學 2 60 1 2
合計 10 29
29/10=2.9
2.9即爲某生的GPA
下面有請你寫一個用於計算GPA的程序。
代碼:
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
double[][] score=new double[n][2];
double x=0;
double y=0;
boolean b=true;
for(int i=0;i<n;i++){
score[i][0]=sc.nextDouble();
score[i][1]=sc.nextDouble();
if(score[i][1]>=0){
b=false;
x+=score[i][0];
if(score[i][1]<70&&score[i][1]>=60){
y+=score[i][0];
}
if(score[i][1]<80&&score[i][1]>=70){
y+=score[i][0]*2;
}
if(score[i][1]<90&&score[i][1]>=80){
y+=score[i][0]*3;
}
if(score[i][1]<=100&&score[i][1]>=90){
y+=score[i][0]*4;
}
}
}
if(b){
System.out.println(-1);
}else{
if(y==0){
System.out.println(-1);
}else{
System.out.printf("%.2f",y/x);
System.out.println();
}
}
}
}
}