• 程序设计思维与实践 CSP-M1 补题 (3/4/数据班)


    程序设计思维与实践 CSP-M1 补题 (3/4/数据班)

    • 说明:原题已经找不到了,这里只有代码。一共有三道题,第三题不会做,未提交,这里只有前两个。

    咕咕东的奇遇

    问题分析

    对于一个26个字母形成的环来说,任意两个字母的最小距离,要么是ascii的差的绝对值,要么是26减去这个值,这取决于顺时针还是逆时针移动。记下这两个值,每次取最小的即可。

    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    
    int main()
    {
        string str;
        cin >> str;
        int len = str.length();
        int step = 0;
        int temp = 0;
        int temp1 = 0;
        int temp2 = 0;
        char ori = 'a';
        for (int i = 0; i < len; ++i)
        {
            temp1 = abs(str[i] - ori);
            temp2 = 26 - temp1;
            temp = temp1 < temp2 ? temp1 : temp2;
            step += temp;
            ori = str[i];
        }
        cout << step << endl;
        return 0;
    }
    
    

    咕咕东想吃饭

    问题分析

    两种购物方式,对于第二种,如果当天需求是奇数,则必须买一份,同时第二天获得一张券。

    每天需求初始化为0,输入当天的需求后,如果是偶数,按方法一购买,如果是奇数,按方法二购买一份,其余按照方法二购买,同时下一天的需求减去1(因为获得一张券)。如果出现某天的需求小于零,则不能满足条件。值得注意的是,按照这种方法,如果最后一天购买后有剩余的券,最后一天的下一天也会是负数,也需要额外判断一下。

    #include <iostream>
    using namespace std;
    
    int day[100005] = {};
    
    int main()
    {
        int n;
        cin >> n;
        bool ans = 1;
        for (int i = 0; i < n; ++i)
        {
            int temp;
            cin >> temp;
            day[i] += temp;
            if (day[i] < 0)
                ans = 0;
            if (day[i] % 2 == 1)
            {
                day[i + 1]--;
            }
        }
        if (day[n] == -1)
            ans = 0;
        if (ans == 0)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
        return 0;
    }
    
  • 相关阅读:
    ACM——Points on Cycle
    ACM——A Simple Problem with Integers(线段树的精华版)
    HDU2524——矩形A+B
    ACM——Hero(类似贪心算法)
    用N个三角形最多可以把平面分成几个区域——acm
    ACM——敌兵布阵(经典的线段树)
    ACM——I Hate It(线段树的进化版)
    ACM——今年暑假不AC
    ACM题目Who's in the Middle
    内部排序算法
  • 原文地址:https://www.cnblogs.com/master-cn/p/12542007.html
Copyright © 2020-2023  润新知