信息学奥赛一本通 1.3:程序的控制结构(2)

​第一部分 C++语言

第三章 程序的控制结构

第二节 switch语句

1049 晶晶赴约会
#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 骑车与走路
#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 分段函数
#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 计算邮资
#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 最大数输出
#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 三角形判断
#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 判断闰年
#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;
}
1056 点和正方形的关系
#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 简单计算器
#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 求一元二次方程
#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;
}

如果您的孩子四年级及以上,对计算机编程感兴趣,且文化课学有余力,欢迎联系客服(微信号:xiaolan7321),参加信息学的学习。我们是专业的信息学竞赛教练,采用线上小班授课的方式,目标是帮助热爱编程的中小学生,在国内外信息学竞赛中取得优秀成绩。

教学特点:

  • 线上小班授课,打好代码基础。避免大班课堂上学生要么“跟不上”,要么“吃不饱”的问题。

  • 教学经验丰富,熟悉学生的知识结构与学习能力,合理安排进度。

  • 以赛代练,通过考级与比赛,不断提高学生能力。

猜你喜欢

转载自blog.csdn.net/davidliule/article/details/106139506
今日推荐