首页 > 其他 > 详细

小刘(第二版)

时间:2014-12-23 13:50:16      阅读:210      评论:0      收藏:0      [点我收藏+]

第三章

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

 

小刘(第二版)

原文:http://www.cnblogs.com/shinecheng/p/4179848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!