If there are K(K > 0) courses, the i-th course has the credit Ci, your score Si, then the result GPA is
GPA = (C1 * S1 + C2 * S2 +……+Ci * Si……) / (C1 + C2 + ……+ Ci……) (1 <= i <= K, Ci != 0)
If there is a 0 <= Si < 60, The GPA is always not existed.
Notice: There is no blank in the Course Name. All the Inputs are lega
2 3 Algorithm 3 97 DataStruct 3 90 softwareProject 4 85 2 Database 4 59 English 4 8
90.10 Sorry!
题目:GPA = (C1 * S1 + C2 * S2 +……+Ci * Si……) / (C1 + C2 + ……+ Ci……) (1 <= i <= K, Ci != 0)用这个公式计算总分
但成绩有小于60的输出Sorry!
并且注意没输出一组数据的结果,后面就空一行,但最后一次不空
还要注意学分和成绩只能用double
作为计算总数的变量别忘记先清零
数组稍开大一点,防止溢出
#include<stdio.h>
struct news{
char nam[32];
double cred;
double sco;
}a[105];
int main()
{
int n,m,i,p;double sum,num;
scanf("%d",&n);
while(n--)
{
sum=num=0.0;p=0;
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
scanf("%s %lf %lf",a[i].nam,&a[i].cred,&a[i].sco);
for(i=0;i<m;i++)
{
if(a[i].sco<60)
p=1;
sum+=a[i].cred*a[i].sco;
num+=a[i].cred;
}
if(p) printf("Sorry!\n");
else{
sum=sum/num;
printf("%.2lf\n",sum);
}
if(n) printf("\n");
}
return 0;
}