• 小刘(第二版)


    第三章

    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
    ", 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
    ", &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
    ", 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
    ", res);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    24、面向对象(内置方法)
    23、面向对象(包装)
    22、面向对象(反射)
    21、面向对象(封装)
    20、面向对象(多态)
    19、面向对象(继承)
    18、面向对象(静态属性、类方法、静态方法)
    LeetCode 3. Longest Substring Without Repeating Characters
    LeetCode 2.Add Two Numbers
    LeetCode 1. Two Sum
  • 原文地址:https://www.cnblogs.com/shinecheng/p/4179848.html
Copyright © 2020-2023  润新知