蓝桥杯试题算法提高gpac语言

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

蓝桥杯ADV-284 算法提高GPA
算法提高GPA
时间限制:1.0s 内存限制:256.0MB
问题描述
输入A,B两人的学分获取情况,输出两人GPA之差。

输入格式
输入的第一行包含一个整数n表示A的课程数,以下n行每行Si,Ci分别表示第i个课程的学分与A的表现。

GPA=Σ(Si*Ci) / Σ(Si)。

特殊地,如果Ci是'P'或者'N'(对应于通过与不通过),则第i个课程不记入GPA的计算(即当其不存在)。

A读入结束后读入B,B的输入格式与A相同。

保证2人的Σ(Si)非零
输出格式
输出A的GPA - B的GPA的值,保留2位小数(四舍五入)
Tips:当A和B的分数相近时输出0.00。

样例输入
2
1 10
2 N
2
1 10
1 5
样例输出
2.50
数据规模和约定
输入的所有数字均为不超过100的非负整数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int n, Si, Ci;
char str_Ci[5] = { 0 };
double numerator, denominator, gpa_a, gpa_b;
numerator = denominator = 0;
scanf("%d", &n);
while (n--)
{
scanf("%d %s", &Si, str_Ci);
if (strcmp(str_Ci, "P") == 0 || strcmp(str_Ci, "N") == 0) continue;
else
{
Ci = atoi(str_Ci);
numerator += Si * Ci;
denominator += Si;
}
}
gpa_a = numerator / denominator;
numerator = denominator = 0;
scanf("%d", &n);
while (n--)
{
scanf("%d %s", &Si, str_Ci);
if (strcmp(str_Ci, "P") == 0 || strcmp(str_Ci, "N") == 0) continue;
else
{
Ci = atoi(str_Ci);
numerator += Si * Ci;
denominator += Si;
}
}
gpa_b = numerator / denominator;
double diff = gpa_a - gpa_b;
if (fabs(diff) < 0.01)
printf("0.00");
else
printf("%.2lf", diff);
return 0;
}。

相关文档
最新文档