列表

详情


NP47. 牛牛的绩点

描述

牛牛在门头沟大学学习,一学年过去了,需要根据他的成绩计算他的平均绩点,假如绩点与等级的对应关系如下表所示。请根据输入的等级和学分数,计算牛牛的均绩(每门课学分乘上单门课绩点,求和后对学分求均值)。
A
4.0
B
3.0
C
2.0
D
1.0
F
0




输入描述

连续输入一行等级一行学分,遇到等级为False则结束输入。

输出描述

均绩保留两位小数。

示例1

输入:

A
3
B
4
C
2
False

输出:

3.11

原站题解

Python 解法, 执行用时: 14ms, 内存消耗: 3096KB, 提交时间: 2022-06-23

dict = {"A":0,"B":0,"C":0,"D":0,"F":0}
dict_jd = {"A":4.0,"B":3.0,"C":2.0,"D":1.0,"F":0}
sum = 0.0
num = 0
while True:
    a = raw_input()
    if a != "False":
        b = input()
        dict[a] += b
    else:
        break
for i in dict.keys():
    num += dict[i]
    sum += dict[i] * dict_jd[i]
result = sum/num
print '%.2f'%result

Python 解法, 执行用时: 14ms, 内存消耗: 3128KB, 提交时间: 2022-07-31

dict = {"A":0,"B":0,"C":0,"D":0,"F":0}
dict_jd = {"A":4.0,"B":3.0,"C":2.0,"D":1.0,"F":0}
sum = 0.0
num = 0
while True:
    a = raw_input()
    if a != "False":
        b = input()
        dict[a] += b
    else:
        break
for i in dict.keys():
    num += dict[i]
    sum += dict[i] * dict_jd[i]
result = sum/num
print '%.2f'%result






Python 解法, 执行用时: 15ms, 内存消耗: 3048KB, 提交时间: 2022-07-08

sumScore = 0
sumAll = 0
while 1:
    name = raw_input()
    if name == 'False':
        print('%.2f'%(sumAll/sumScore))
        break
    score = float(input())
    if name == 'A':
        sumAll += score * 4
    elif name == 'B':
        sumAll += score * 3
    elif name == 'C':
        sumAll += score * 2
    elif name == 'D':
        sumAll += score * 1
    else:
        pass
    sumScore += score

Python 解法, 执行用时: 16ms, 内存消耗: 3080KB, 提交时间: 2022-07-04

dict1 = {'A':4,'B':3,'C':2,'D':1,'F':0}
dict2 = {'A':0,'B':0,'C':0,'D':0,'F':0}
num1 = 0
num2 = 0
while True :
    a = raw_input()
    if a == 'False' :
        break
    b = input()
    num1 += dict1[a] * b
    num2 += b 
s = float(num1)
ss = float(num2)
print '%.2f'%(s/ss)   

Python 3 解法, 执行用时: 38ms, 内存消耗: 4540KB, 提交时间: 2022-07-30

grade_dict = {'A':4.0,'B':3.0,'C':2.0,'D':1.0,'F':0,}
grade_list =[]
scores_list = []
while True:
    grade = input()
    if grade =='False':
        break
    else:    
        scores = int(input())
        grade_list.append(grade_dict[grade] * scores)
        scores_list.append(scores)
        
avg_score = sum(grade_list)/sum(scores_list)
print('%.2f'%avg_score)

上一题