跳转至

第三章 程序的控制结构

第一节 if选择结构

2051:【例 3.1】偶数

代码
#include <bits/stdc++.h>

using namespace std;

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

    if (a % 2 == 0) cout << "yes";

    return 0;
}

2052:【例 3.2】范围判断

代码
#include <bits/stdc++.h>

using namespace std;

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

    if (n > 1 && n < 100) cout << "yes";

    return 0;
}

2053:【例 3.3】三个数

提示

这个题目,看起来很简单。

实际写代码的时候,会发现,很不好写。

代码
// 这是把所有情况列出来
#include <bits/stdc++.h>

using namespace std;

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

    if (a >= b && b >= c) cout << a << ' ' << b << ' ' << c << '\n';
    else if (a >= c && c >= b) cout << a << ' ' << c << ' ' << b << '\n';
    else if (b >= a && a >= c) cout << b << ' ' << a << ' ' << c << '\n';
    else if (b >= c && c >= a) cout << b << ' ' << c << ' ' << a << '\n';
    else if (c >= a && a >= b) cout << c << ' ' << a << ' ' << b << '\n';
    else if (c >= b && b >= a) cout << c << ' ' << b << ' ' << a << '\n';

    return 0;
}
代码02
// 这其实是做了一个排序
#include <bits/stdc++.h>

using namespace std;

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

    if (a < b) swap(a, b);
    if (a < c) swap(a, c);
    if (b < c) swap(b, c);

    cout << a << ' ' << b << ' ' << c << '\n';

    return 0;
}
代码03
// 用总和减去最大、最小,就是中间的那个数
#include <bits/stdc++.h>

using namespace std;

int maxn = -1, minn = 2e9;

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

    maxn = max(max(a, b), c);
    minn = min(min(a, b), c);

    int zhong = a + b + c - maxn - minn;

    cout << maxn << ' ' << zhong << ' ' << minn << '\n';

    return 0;
}

2054:【例 3.4】适合晨练

代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    if (n >= 25 && n <= 30) cout << "ok!";
    else cout << "no!";

    return 0;
}

2055:【例 3.5】收费

代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    double x;
    cin >> x;

    if (x <= 20) printf("%.2lf\n", x * 1.68);
    else printf("%.2lf\n", x * 1.98);

    return 0;
}

2056:【例 3.7】最大的数

提示

题面当中没有说明,输入的三个数是什么类型的

但是看样例,你会发现,是小数,所以要用double

代码
#include <bits/stdc++.h>

using namespace std;

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

    if (x < y) swap(x, y);
    if (x < z) swap(x, z);

    cout << x << '\n';

    return 0;
}

1039:判断数的正负

代码
#include <bits/stdc++.h>

using namespace std;

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

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

    return 0;
}

1040:输出绝对值

提示

正数的绝对值是其本身,负数的绝对值是其相反数

所以,这里可以用if语句进行判断,分情况输出

对于int,取绝对值的函数是 abs()

对于double,取绝对值的函数是 fabs()

代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    double a;
    cin >> a;

    if (a < 0) a = -a;

    printf("%.2lf\n", a);

    return 0;
}
代码02
#include <bits/stdc++.h>

using namespace std;

int main() {
    double a;
    cin >> a;

    printf("%.2lf\n", fabs(a));

    return 0;
}

1041:奇偶数判断

提示

奇数 odd

偶数 even

代码
#include <bits/stdc++.h>

using namespace std;

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

    if (n % 2 == 1) cout << "odd";
    else cout << "even";

    return 0;
}
代码02
// n % 2 的结果,只能是0,或者1
// 所以,非零就是1,对应的就是奇数
#include <bits/stdc++.h>

using namespace std;

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

    if (n % 2) cout << "odd";
    else cout << "even";

    return 0;
}
代码03
// &,是位运算,按位与
// & 1 的结果如果非零,可以用来判断是奇数
#include <bits/stdc++.h>

using namespace std;

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

    if (n & 1) cout << "odd";
    else cout << "even";

    return 0;
}

1042:奇偶 ASCII 值判断

代码
#include <bits/stdc++.h>

using namespace std;

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

    int x = c;

    if (x % 2 == 1) cout << "YES";
    else cout << "NO";

    return 0;
}

1043:整数大小比较

代码
#include <iostream>

using namespace std;

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

    if(n > m) cout << '>';
    else if(n == m) cout << '=';
    else cout << '<';

    return 0;
}

1044:判断是否为两位数

代码
#include <bits/stdc++.h>

using namespace std;

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

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

    return 0;
}

1045:收集瓶盖赢大奖

提示

如果你拥有10个印有“幸运”、或20个印有“鼓励”的瓶盖

这个逻辑是或者的关系,||

代码
#include <bits/stdc++.h>

using namespace std;

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

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

    return 0;
}

1046:判断一个数能否同时被 3 和 5 整除

代码
// puts("") 就是输出一个字符串
// 效果和cout一样,但后面自带一个换行
#include <bits/stdc++.h>

using namespace std;

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

    if (n % 3 == 0 && n % 5 == 0) puts("YES");
    else puts("NO");

    return 0;
}

1047:判断能否被 3,5,7 整除

提示

这个题目,也是看起来简单,写起来难受的题目

当然,当你知道了用代码这样表达的逻辑,后面就好了

代码
// 先来一个最笨的写法
// 把所有情况列举一遍
#include<bits/stdc++.h>

using namespace std;

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

    if(n%3==0&&n%5==0&&n%7==0) cout << 3 << ' ' << 5 << ' ' << 7;
    else if(n%3==0&&n%5==0) cout << 3 << ' ' << 5;
    else if(n%3==0&&n%7==0) cout << 3 << ' ' << 7;
    else if(n%5==0&&n%7==0) cout << 5 << ' ' << 7;
    else if(n%3==0) cout << 3;
    else if(n%5==0) cout << 5;
    else if(n%7==0) cout << 7;
    else cout << 'n';

    return 0;
}
代码02
// 这个写法,用flag来控制,3、5、7是否有一个可以整除
// flag 像一个开关一样
#include <bits/stdc++.h>

using namespace std;

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

    bool flag = false;
    if (n % 3 == 0) {
        cout << "3 ";
        flag = true;
    }
    if (n % 5 == 0) {
        cout << "5 ";
        flag = true;
    }
    if (n % 7 == 0) {
        cout << "7 ";
        flag = true;
    }

    if (!flag) cout << 'n';

    return 0;
}
代码03
// 我们利用逻辑运算符
// (a%3== 0 || a%5 == 0 || a%7 == 0) 这句话是有一个成立就成立
// !(a%3== 0 || a%5 == 0 || a%7 == 0) 前面加一个!,就变成了一个都不成立的时候成立
// 绕一点
#include<iostream>

using namespace std;

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

    if(a%3==0) cout<<"3"<<" ";
    if(a%5==0) cout<<"5"<<" ";
    if(a%7==0) cout<<"7"<<" ";

    if(!(a%3== 0 || a%5 == 0 || a%7 == 0))cout<<"n";

    return 0;
}

1048:有一门课不及格的学生

代码
#include <bits/stdc++.h>

using namespace std;

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

    if ((a < 60 && b >= 60) || (a >= 60 && b < 60)) cout << 1;
    else cout << 0;

    return 0;
}

第二节 switch语句

2057:【例 3.9】星期几

代码
#include <iostream>

using namespace std;

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

    switch (n){
        case 1: cout << "Monday" << '\n'; break;
        case 2: cout << "Tuesday" << '\n'; break;
        case 3: cout << "Wednesday" << '\n'; break;
        case 4: cout << "Thursday" << '\n'; break;
        case 5: cout << "Friday" << '\n'; break;
        case 6: cout << "Saturday" << '\n'; break;
        case 7: cout << "Sunday" << '\n'; break;

        default: cout << "input error!" << '\n';
    }

    return 0;
}

2058:【例 3.10】简单计算器

提示

输出的时候,直接用cout即可。

代码
#include <bits/stdc++.h>

using namespace std;

double a, b;
char c;

int main() {
    cin >> a >> b >> c;
    if (c == '+') cout << a + b << '\n';
    else if (c == '-') cout << a - b << '\n';
    else if (c == '*') cout << a * b << '\n';
    else if (c == '/') {
        if (b == 0) cout << "Divided by zero!" << '\n';
        else cout << a / b << '\n';
    } else cout << "Invalid operator!" << '\n';

    return 0;
}

2059:【例 3.11】买笔

提示

这个题目,我觉得用循环,枚举所有的情况

从大到小枚举,就可以了

网上题解有用数学方法的,那个我没看懂

代码
#include<bits/stdc++.h>

using namespace std;

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

    for (int i = x / 4; i >= 0; i--)
        for (int j = x / 5; j >= 0; j--)
            for (int k = x / 6; k >= 0; k--) {
                //cout << k << ' ' << j << ' ' << i << '\n';
                if (i * 4 + j * 5 + k * 6 == x) {
                    cout << k << ' ' << j << ' ' << i << '\n';
                    return 0;
                }
            }

    return 0;
}

1049:晶晶赴约会

代码
#include <bits/stdc++.h>

using namespace std;

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

    if (n == 1 || n == 3 || n == 5) puts("NO");
    else puts("YES");

    return 0;
}

1050:骑车与走路

提示

应用题题面

代码
//走路快,还是骑车快
#include <iostream>

using namespace std;

int main() {
    int dist; // distance
    cin >> dist;

    double walk = dist / 1.2;
    double bike = 27 + 23 + dist / 3.0;

    if (walk < bike) cout << "Walk";
    else if (bike < walk) cout << "Bike";
    else cout << "All";

    return 0;
}

1051:分段函数

提示

分段函数,这个很能小朋友们看不懂

其实就是当什么情况的时候,怎么计算,就是这个东西

在编程里,就是if

代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    double x;
    cin >> x;

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

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

    return 0;
}

1052:计算邮资

提示

这个题目,是有点难度的

首先难在读题上,题读不懂

然后难在怎么用代码表达

虽然现在知道了咋回事,但是代码不会表达

我们来捋一下思路

先是8元打底,然后看是否超过1000,超过的部分,每500克4元,向上取整

然后看是否加急

代码
#include <bits/stdc++.h>

using namespace std;

int n, cost;
char y;

int main() {
    cin >> n >> y;
    cost = 8;
    if (n > 1000) cost += (n - 1000 + 500 -1) / 500 * 4;
    if (y == 'y') cost += 5;
    cout << cost << '\n';

    return 0;
}

1053:最大数输出

代码
// -0x3f3f3f3f,是一个极小的数,你先这样理解
// 学一段时间后,再解释这是一个什么东西
// 这个数字是十六进制数,在后面写代码中经常用
#include <bits/stdc++.h>

using namespace std;

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

    int maxn = -0x3f3f3f3f;
    maxn = max(max(a, b), c);

    cout << maxn << endl;

    return 0;
}
代码02
// max还可以这样使用
// 但这和编译器的版本是有关的
// 换句话说,低版本的C++编译器,是不识别这种写法的
#include <bits/stdc++.h>

using namespace std;

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

    cout << max({a, b, c});

    return 0;
}

1054:三角形判断

提示

两边之和大于第三边,两边之差小于第三边

代码
#include <bits/stdc++.h>

using namespace std;

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

    if (a + b > c && b + c > a && a + c > b) puts("yes");
    else puts("no");

    return 0;
}

1055:判断闰年

提示

四年一闰,百年不闰,四百年再闰

代码
#include <bits/stdc++.h>

using namespace std;

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

    if ((n % 4 == 0 && n % 100) || (n % 400 == 0)) cout << 'Y' << '\n';
    else cout << 'N' << '\n';

    return 0;
}

1056:点和正方形的关系

提示

这个题目,在第一次接触的时候,是不好做的

因为不知道怎么用代码表达,点在正方形的里面

其实,我们就是用坐标的范围,进行的判断

代码
#include <bits/stdc++.h>

using namespace std;

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

    if (x >= -1 && x <= 1 && y >= -1 && y <= 1) puts("yes");
    else puts("no");

    return 0;
}

1057:简单计算器

提示

这道题目,在当前做的题目当中,算得上题面很长的了

逻辑也复杂一点

所以,是个难题

首先要正确的读入数据,两个int,一个char

然后用if判断,字符c是什么,然后对应的进行计算

代码
// 注意观察这份代码换行的使用
// 如果if下面只有一句话,可以不用大括号,也可以提上来写
// 输出的话,一定要复制粘贴,千万不要自己敲,你看不清那些大小写和空格的
#include <iostream>

using namespace std;

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

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

    return 0;
}

1058:求一元二次方程

提示

这个题目放在后面做

提示02

一元二次方程的求根公式,是要背的,背一下

此题,当有两个不同的实根时,还需要进行一下比较,题目要求较小的在前面

所以,对读题也是有一定考察

代码
#include <bits/stdc++.h>

using namespace std;

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

    double delta = b * b - 4 * a * c;
    if (delta < 0) cout << "No answer!" << '\n';
    else if (delta == 0) {
        printf("x1=x2=%.5lf\n", -b / (2 * a));
    } else {
        double x1 = (-b - sqrt(delta)) / (2 * a);
        double x2 = (-b + sqrt(delta)) / (2 * a);
        if (x1 > x2) swap(x1, x2);
        printf("x1=%.5lf;x2=%.5lf\n", x1, x2);
    }

    return 0;
}