当前位置: 移动技术网 > IT编程>开发语言>C/C++ > HDU - 4802 - GPA (水题)

HDU - 4802 - GPA (水题)

2018年09月26日  | 移动技术网IT编程  | 我要评论

吴玉相,好易通tc500s对讲机,诺安基金大厦

题意:

计算gpa,输入一个数字和一个字符串,用 数字×字符串对应的数值

思路:

用map对应数值,要注意的是字符串为p或者n的时候,不计入结果

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>

using namespace std;

map<string, double> mp;

int main() {
    mp["a"] = 4.0;
    mp["a-"] = 3.7;
    mp["b+"] = 3.3;
    mp["b"] = 3.0;
    mp["b-"] = 2.7;
    mp["c+"] = 2.3;
    mp["c"] = 2.0;
    mp["c-"] = 1.7;
    mp["d"] = 1.3;
    mp["d-"] = 1.0;
    mp["f"] = 0.0;
    int t;
    while (~scanf("%d", &t)) {
        double x, ans = 0.0, index = 0.0;
        string str;
        for (int i = 1; i <= t; i++) {
            cin >> x >> str;
            if (str == "p" || str == "n") continue;
            ans += x * mp[str];
            index += x;
        }
        if (index == 0) printf("0.00\n");
        else printf("%.2lf\n", ans / index);
    }

    return 0;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网