Informatics Olympiad 1.3: The control structure of the program (2)

​The first part of C++ language

Chapter III Program Control Structure

The second section switch statement

1049 I'm Not Afraid
#include <iostream>
using namespace std;

int main() {
    int day;
    cin >> day;

    switch (day) {
        case 1:
            cout << "NO" << endl;
            break;
        case 2:
            cout << "YES" << endl;
            break;
        case 3:
            cout << "NO" << endl;
            break;
        case 4:
            cout << "YES" << endl;
            break;
        case 5:
            cout << "NO" << endl;
            break;
        case 6:
            cout << "YES" << endl;
            break;
        case 7:
            cout << "YES" << endl;
            break;
    }

    return 0;
}
1050 Cycling and Walking
#include <iostream>
using namespace std;

int main() {
    int d;
    cin >> d;

    // 1/(1/1.2-1/3.0)=2
    int x = (27+23)*2;

    if (d > x) {
        cout << "Bike" << endl;
    }
    else if (d == x) {
        cout << "All" << endl;
    }
    else {
        cout << "Walk" << endl;
    }

    return 0;
}
1051 piecewise function
#include <cstdio>
using namespace std;

int main() {
    double x, y;
    scanf("%lf", &x);

    if (x>=0 && x<5) {
        y = -x + 2.5;
    }
    else if (x < 10){
        y = 2 - 1.5 * (x-3) * (x-3);
    }
    else if (x < 20) {
        y = x / 2 - 1.5;
    }

    printf("%.3lf\n", y);

    return 0;
}
1052 Calculate postage
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int w, cost;
    char urgent;
    cin >> w >> urgent;

    if (w <= 1000) {
        cost = 8;
    }
    else {
        w -= 1000;
        cost = 8 + ceil(w/500.0) * 4;
    }

    if (urgent == 'y') {
        cost += 5;
    }

    cout << cost << endl;

    return 0;
}
1053 Maximum number output
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int a,b,c;
    cin >> a >> b >> c;

    if (a >= b && a >= c) {
        cout << a << endl;
    }
    else if (b >= a && b >= c) {
        cout << b << endl;
    }
    else {
        cout << c << endl;
    }

    return 0;
}
1054 Triangle Judgment
#include <iostream>
using namespace std;

int main() {
    int a,b,c;
    cin >> a >> b >> c;

    if (a+b>c && b+c>a && c+a>b) {
        cout << "yes" << endl;
    }
    else {
        cout << "no" << endl;
    }

    return 0;
}
1055 Judge Leap Year
#include <iostream>
using namespace std;

int main() {
    int year;
    cin >> year;

    if (year%4==0 && year%100!=0 || year%400==0) {
        cout << 'Y' << endl;
    }
    else {
        cout << 'N' << endl;
    }

    return 0;
}
The relationship between 1056 points and squares
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int x, y;
    cin >> x >> y;

    if (abs(x) <= 1 && abs(y) <= 1) {
        cout << "yes" << endl;
    }
    else {
        cout << "no" << endl;
    }

    return 0;
}
1057 Simple Calculator
#include <iostream>
using namespace std;

int main() {
    int a, b;
    char op;

    cin >> a >> b >> op;

    if (op == '+') {
        cout << a + b << endl;
    }
    else if (op == '-') {
        cout << a - b << endl;
    }
    else if (op == '*') {
        cout << a * b <<endl;
    }
    else if (op == '/') {
        if (b == 0) {
            cout << "Divided by zero!" << endl;
        }
        else {
            cout << a / b << endl;
        }
    }
    else {
        cout << "Invalid operator!" << endl;
    }

    return 0;
}
1058 Find the quadratic equation in one variable
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
    double a, b, c, delta;
    scanf("%lf %lf %lf", &a, &b, &c);

    delta = b*b - 4*a*c;

    if (fabs(delta) < 1e-6) {
        printf("x1=x2=%.5lf\n", -b/(2*a));
    }
    else if (delta < 0){
        printf("No answer!\n");
    }
    else {
        if (a > 0) {
            printf("x1=%.5lf;x2=%.5lf\n", (-b-sqrt(delta))/(2*a), (-b+sqrt(delta))/(2*a) );
        }
        else {
            printf("x1=%.5lf;x2=%.5lf\n", (-b+sqrt(delta))/(2*a), (-b-sqrt(delta))/(2*a) );
        }
    }

    return 0;
}

If your child is in the fourth grade or above, is interested in computer programming and has spare capacity in cultural lessons, please contact customer service (WeChat ID: xiaolan7321) to participate in informatics learning. We are professional informatics competition coaches. We use online small class teaching methods. The goal is to help primary and middle school students who love programming to achieve excellent results in informatics competitions at home and abroad.

Teaching features:

  • Online small class teaching, lay a good code foundation. Avoid the problem that students in large classes are either "can't keep up" or "not enough to eat".

  • Rich teaching experience, familiar with students' knowledge structure and learning ability, and arrange the schedule reasonably.

  • Practice with competitions, and continuously improve students' abilities through examinations and competitions.

Guess you like

Origin blog.csdn.net/davidliule/article/details/106139506