小刘(第二版)

第三章

3.1 uva1585 uvalive3354

题意:错题0分,对题得分为目前位置连续正确数。求总分。

思路:模拟

代码:

#include <cstdio>
#include <cstring>

char str[10000];
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%s", str);
        int res = 0;
        int now = 0;
        for (int i = 0; str[i]; i++) {
            if (str[i] == 'O') now++;
            else now = 0;
            res += now;
        }
        printf("%d\n", res);
    }
    return 0;
}
View Code

 3.2 uva1586 uvalive3900

题意:告诉你四种原子的摩尔质量,现告诉你分子式,求分子的摩尔质量(话说是这么说的么?高中化学忘完了。。)

思路:模拟。最后写成了按字符处理的状态机写法。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>

double num[5] = {12.01,1.008,16.00,14.01, 0};
char dict[1000];
char str[10000];

#define LOG 

int main() {
    int t;
    scanf("%d\n", &t);
    dict['C'] = 0;
    dict['H'] = 1;
    dict['O'] = 2;
    dict['N'] = 3;
    while (t--) {
        scanf("%s", str);
        int pre = 4;
        int now = 0;
        double res = 0;
        for (int i = 0; str[i]; i++) {
            if ('A' <= str[i] && str[i] <= 'Z') {
                res += num[pre]*(now==0?1:now); // default 1
                LOG("pre = %d, now = %d\n", pre, now);

                now = 0;
                pre = dict[str[i]];
            }
            if ('0' <= str[i] && str[i] <= '9') {
                now = now * 10 + str[i] - '0';
            }
        }
        res += num[pre]*(now==0?1:now); // default 1

        printf("%.3lf\n", res);
    }
    return 0;
}
View Code

 

posted on 2014-12-23 12:18  ShineCheng  阅读(148)  评论(0编辑  收藏  举报

导航