Posted on 2008-08-15 15:52
歲月流逝 閱讀(766)
評論(5) 編輯 收藏 引用
題目描述
The calculation of GPA
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2613 Accepted Submission(s): 598
Problem Description
每學期的期末,大家都會忙于計算自己的平均成績,這個成績對于評獎學金是直接有關的。國外大學都是計算GPA(grade point average) 又稱GPR(grade point ratio),即成績點數與學分的加權平均值來代表一個學生的成績的。那么如何來計算GPA呢?
一般大學采用之計分法
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的程序。
Input
包含多組數據,每組數據的第一行有一個數N,接下來N行每行表示一門成績。每行有兩個實型的數 s,p,s表示這門課的學分,p表示該學生的成績(百分制)。如果p=-1則說明該學生這門課缺考,是不應該計算在內的。
Output
對每組數據輸出一行,表示該學生的GPA,保留兩位小數。如果GPA不存在,輸出-1。
Sample Input
Sample Output
鏈接
http://acm.hdu.edu.cn/showproblem.php?pid=1202我的代碼
//杭電1202
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float suma=0,nowa,nowb,sum=0,point;
int n;
int flag=0;
while(cin>>n)
{
for(int i=0;i<n;++i)
{
cin>>nowa>>nowb;
if(nowb==-1)
{
flag++;
continue;
}
if(nowb!=-1)
{
suma+=nowa;
if(nowb>=90&&nowb<=100)
point=4;
if(nowb>=80&&nowb<90)
point=3;
if(nowb>=70&&nowb<=79)
point=2;
if(nowb>=60&&nowb<=69)
point=1;
if(nowb>=0&&nowb<=59)
point=0;
sum+=point*nowa;
}
}
if(flag!=n)
{cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
cout<<sum/suma<<endl;
}
else
cout<<-1<<endl;
}
return 0;
}
別人AC的代碼
#include<stdio.h>
int main()
{
int i,sub;
float score,allgpa,credit,allcredit;
while(scanf("%d",&sub)!=EOF)
{
allgpa=0;
allcredit=0;
for(i=0;i<sub;i++)
{
scanf("%f%f",&credit,&score);
if(score!=-1)
{
allcredit+=credit;
if(score==100)
allgpa+=credit*4;
else
if(score>59)
allgpa+=credit*((int)score/10-5);
}
}
if(allcredit==0)
printf("-1\n");
else printf("%.2f\n",allgpa/allcredit);
}
return 0;
}
我怎么也找不出錯誤呀