一本通第三章 程序的控制结构

第一节 if选择结构

1039 判断数正负
#include <iostream>
using namespace std;

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

    if (n > 0) {
        cout << "positive" << endl;
    }
    else if (n == 0) {
        cout << "zero" << endl;
    }
    else {
        cout << "negative" << endl;
    }

    return 0;
}
1040 输出绝对值
#include <cstdio>
using namespace std;

int main() {
    float a;
    scanf("%f",&a);

    if (a < 0) {
        printf("%.2f\n", -1 * a);
    }
    else {
        printf("%.2f\n", a);
    }

    return 0;
}
1041 奇偶数判断
#include <iostream>
using namespace std;

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

    if (n%2 == 0) {
        cout << "even" << endl;
    }
    else {
        cout << "odd" << endl;
    }

    return 0;
}
1042 奇偶ASCII值判断
#include <iostream>
using namespace std;

int main() {
    char c;
    cin >> c;

    if (c%2 == 1) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }

    return 0;
}
1043 整数大小比较
#include <iostream>
using namespace std;

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

    if (x > y) {
        cout << '>' << endl;
    }
    else if (x == y) {
        cout << '='<< endl;
    }
    else {
        cout << '<' << endl;
    }

    return 0;
}
1044 判断是否为两位数
#include <iostream>
using namespace std;

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

    if (n >=10 && n <= 99) {
        cout << 1 << endl;
    }
    else {
        cout << 0 << endl;
    }

    return 0;
}
1045 收集瓶盖赢大奖
#include <iostream>
using namespace std;

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

    if (a>=10 || b >=20) {
        cout << 1 << endl;
    }
    else {
        cout << 0 << endl;
    }

    return 0;
}
1046 判断一个数能否同时被3和5整除
#include <iostream>
using namespace std;

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

    if (n%3 == 0 && n%5 ==0) {
        cout << "YES" << endl;
}
    else {
        cout << "NO" << endl;
    }

    return 0;
}
1047 判断能否被3,5,7整除
#include <iostream>
using namespace std;

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

    if (n%3!=0 && n%5!=0 && n%7!=0) {
        cout << 'n' << endl;
    }
    else {
        if (n%3 == 0) {
            cout << "3 ";
        }
        if (n%5 == 0) {
            cout << "5 ";
        }
        if (n%7 == 0) {
            cout << "7 ";
        }
    }

    return 0;
}
1048 有一门课不及格的学生
#include <iostream>
using namespace std;

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

    if ((a>=60) == (b>=60)) {
        cout << 0 << endl;
    }
    else {
        cout << 1 << endl;
    }

    return 0;
}

第二节 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值