Program design thinking and practice of CSP-M1 Bu Ti (3/4 / data classes)

Program design thinking and practice of CSP-M1 Bu Ti (3/4 / data classes)

  • Description: The original title has been found, and there are only code. There are three questions, the third question would not do, not submitted, only the first two here.

    Cuckoo East adventure

    problem analysis

    A letter to the ring 26 is formed, the minimum distance of any two letters, either the absolute value of difference ascii, either the value 26 is subtracted, depending on the clockwise or counterclockwise movement. Note the value of two, to take each of the minimum.
#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;
}

Cuckoo East want to eat

problem analysis

Both shopping mode, for the second, the same day if the demand is odd, you must buy a copy, and get a ticket the next day.

Daily demand is initialized to 0, after entering the day's demand, if it is an even number, according to the method a purchase, if it is an odd number, according to purchase a second method, in accordance with the purchase method two remaining, while the next day the demand minus 1 (because obtaining a Zhang coupons). If demand appeared one day less than zero, then the conditions are not met. It is noteworthy that, according to this method, there are remaining tickets if purchased after the last day, the last day of the next day will be negative, also need to determine what additional.

#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;
}

Guess you like

Origin www.cnblogs.com/master-cn/p/12542007.html