Luo Gu training "sequence and branch" solution to a problem of Xinshoucun

P1422 Xiaoyu home electricity

Topic links: https://www.luogu.com.cn/problem/P1422
subject to the effect:
summer, each household's electricity consumption have increased a lot, the corresponding electricity also post more. Xiaoyu home today received a notice electricity. Xiaoyu see written above: According to Min valence requirements [2006] No. 27, and monthly electricity consumption 150 kwh per kWh 0.4463 yuan the following sections performed monthly electricity consumption in some 151 to 400 kilowatt-hours per kWh 0.4663 yuan execution, the monthly electricity consumption 401 kwh and more parts per kWh 0.5663 yuan execution; Xiaoyu want to test themselves, the number of tariff on electricity bills payable notice in the end it is correct. Write a program, known total electricity, according to price regulations, payable calculated electricity should be.
Problem-solving ideas: segmented pricing.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
double a, b;
int main() {
    scanf("%lf", &a);
    if (a <= 150) b = a * 0.4463;
    else if (a <= 400) b = 150 * 0.4463 + (a - 150) * 0.4663;
    else b = 150 * 0.4463 + 250 * 0.4663 + (a - 400) * 0.5663;
    printf("%.1lf\n", b);
    return 0;
}

P1085 unhappy Jinjin

Topic links: https://www.luogu.com.cn/problem/P1085
subject to the effect:
Jinjin the junior high school. Her mother felt Jinjin should study harder, so Jinjin in addition to school, but also for her mother to participate in the registration of subjects refresher course. Also weekly mother would send her to learn reading, dance and piano. But if Jinjin day of classes over eight hours will be unhappy, but also on the longer it will be more unhappy. Jinjin will not assume other things unhappy, and she's not happy not last until the next day. Would you please check Jinjin schedule next week, next week, she will not be unhappy to see; and if so, the most unhappy day.
Problem-solving ideas: loop through it again to find the most unhappy day.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int a[8], b;
int main() {
    for (int i = 1; i <= 7; i ++) {
        cin >> a[i] >> b;
        a[i] += b;
    }
    b = 0;
    for (int i = 1; i <= 7; i ++) if (a[i] > 8 && a[i] > a[b]) b = i;
    cout << b << endl;
    return 0;
}

P1089 Jinjin savings plan

Topic links: https://www.luogu.com.cn/problem/P1089
subject to the effect: savings programs simulation requirements of the subject, will determine strapped for cash in one month, or to determine the final money that can be obtained.
Problem-solving ideas: simulate what this process can solve this problem.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int a, b, c;   // a表示这个月的花费,b表示目前存到妈妈那里的钱(最终计算前不包含利息),c表示手头的钱
int main() {
    for (int i = 1; i <= 12; i ++) {
        cin >> a;
        if (a > c+300) {
            cout << -i << endl;
            return 0;
        }
        c += 300 - a;
        b += c/100 * 100;
        c %= 100;
    }
    cout << c + b/5*6 << endl;
    return 0;
}

P1909 buy pencils

Topic links: https://www.luogu.com.cn/problem/P1909
subject to the effect:
P n teacher needs to go to the store to buy pencils as children participate NOIP gift. She found the store a total of 33 kinds of packaging pencil, pencil in a number of different packaging may be different, prices may vary. To be fair, P teacher decided to only buy the same package of pencils.
Shops are not allowed to open pencil packaging, so P teacher may need to buy more than nn pencils enough for the children to send gifts.
Now P teacher wanted to know, when the number of stores in each package are sufficient, enough to buy a minimum of at least nn pencil how much it costs.

Outline of Solution: For the i-th pencil, assuming it has a box, then we need to buy \ (\ lceil \ frac {n } {a} \ rceil \) box, we use expressions (n+a-1)/ato express n rounded up to a the result of.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int n, a, b, c = INT_MAX;
int main() {
    cin >> n;
    for (int i = 0; i < 3; i ++) {
        cin >> a >> b;
        c = min(c, (n+a-1)/a*b);
    }
    cout << c << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/quanjun/p/11924355.html
Recommended