算法竞赛入门经典第三章3-2 Molar mass UVA - 1586

习题3-2
https://vjudge.net/problem/UVA-1586
用3-1的输入模式,即一个字符一个字符读遇到\n结束,会wa
用字符串处理确是对的,很是奇怪

#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main() {
    int n;
    cin >> n;
    getchar();
    while (n--) {
        char e;
        int cnt = 0;
        double sum = 0.0, per = 0.0;
        string s;
        cin >> s;
        for(int i=0;i<s.size();++i){
            e = s[i];
            if (isalpha(e)) {
                if (!cnt) cnt = 1;
                sum += cnt*per;
                cnt = 0;
                switch (e)
                {
                case 'C':per = 12.01;
                    break;
                case 'H':per = 1.008;
                    break;
                case 'O':per = 16.00;
                    break;
                default:per = 14.01;
                    break;
                }
            }
            else if(isdigit(e))
                cnt = cnt * 10 + e - '0';
        }
        if (!cnt) cnt = 1;
        sum += cnt*per;
        printf("%.3lf\n", sum);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值