跳转至

第二章 顺序结构程序设计

第一节 运算符和表达式

2064:【例 2.1】交换值

代码
#include <iostream>

using namespace std;

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

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

    return 0;
}
代码02
#include <iostream>

using namespace std;

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

    swap(a, b);
    cout << a << ' ' << b << '\n';

    return 0;
}
代码03
#include <iostream>

using namespace std;

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

    int t = a;
    a = b;
    b = t;

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

    return 0;
}

2065:【例 2.2】整数的和

代码
#include <iostream>

using namespace std;

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

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

    return 0;
}

2066:【例 2.3】买图书

代码
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    double n, m;
    scanf("%lf%lf", &n, &m);

    printf("%.2lf\n", n - m * 0.8);

    return 0;
}

1006:A+B 问题

代码
#include <iostream>

using namespace std;

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

    cout << a + b;

    return 0;
}

1007:计算 (a+b)×c 的值

代码
#include <iostream>

using namespace std;

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

    cout << (a + b) * c << endl;

    return 0;
}

1008:计算 (a+b)/c 的值

提示

在计算机当中除法 /,是要特殊注意的

如果两边都是整数,得到一个向下取整的整数结果

如果两遍有一个是小数,就会得到一个小数的结果

比如,10 / 3 得到的是 3

比如,10.0 / 3 得到的是 3.33..

代码
#include <iostream>

using namespace std;

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

    cout << (a + b) / c << endl;

    return 0;
}

1009:带余除法

提示

%,模运算,取余操作

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

using namespace std;

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

    cout << a / b << ' ' << a % b << endl;

    return 0;
}

1010:计算分数的浮点数值

提示

在这里,a 和 b 都是整数,a / b 默认会得到一个向下取整的整数结果

但是,我们想要一个小数的结果,我们可以这样,1.0 * a / b

也可以这样,(double)a / b

代码
#include <iostream>
#include <cstdio>

using namespace std;

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

    printf("%.9lf\n", 1.0 * a / b);

    return 0;
}

第二节 常量和变量

2067:【例 2.5】圆

提示

题目读好了吗

样例看好了吗

草稿纸上的公式,推好了吗

代码
#include <iostream>
#include <cstdio>

using namespace std;

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

    printf("%.4lf %.4lf %.4lf\n", 2*r, 2*3.14159*r, 3.14159*r*r);

    return 0;
}

2068:【例 2.6】鸡兔同笼

代码
// 两层循环
#include <iostream>

using namespace std;

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

    for (int i = 0; i <= x; i++)
        for (int j = 0; j <= min(x, y / 4); j++)
            if (i + j == x && 2 * i + 4 * j == y)
                cout << i << ' ' << j << endl;


    return 0;
}
代码02
// 一层循环
// j其实是不用枚举的
#include <iostream>

using namespace std;

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

    for (int i = 0; i <= x; i++) {
        int j = x - i;
        if (2 * i + 4 * j == y) cout << i << ' ' << j << endl;
    }

    return 0;
}
代码03
// 不用循环
// 前提是,你已经推完了式子
#include <iostream>

using namespace std;

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

    int tuzi = (y - x * 2) / 2;
    int ji = x - tuzi;

    cout << ji << ' ' << tuzi << '\n';

    return 0;
}

1011:甲流疫情死亡率

代码
#include <iostream>
#include <cstdio>

using namespace std;

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

    printf("%.3lf%%\n", 1.0 * b / a * 100);

    return 0;
}

1012:计算多项式的值

代码
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    double a,b,c,d,x,f,e,y,z,w;

    cin>> x >> a >> b >> c >> d;

    e=x*x*x;
    y=a*x*x*x;
    z=b*x*x;
    w=c*x;

    f=y+z+w+d;

    printf("%.7lf\n", f);

    return 0;
}

1013:温度表达转化

代码
#include <iostream>
#include <cstdio>

using namespace std;

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

    printf("%.5lf\n", 5 * (F - 32) / 9);

    return 0;
}

1014:与圆相关的计算

代码
#include <iostream>
#include <cstdio>

using namespace std;

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

    printf("%.4lf %.4lf %.4lf\n", 2*r, 2*3.14159*r, 3.14159*r*r);

    return 0;
}
代码02
// 这里输入的部分,用cin,用scanf,是没有区别的,习惯用哪个都可以
// cin, scanf,只有在需要输入非常多次数据的时候,才会有区别,那时候才要根据情况进行区分
// 现在不用学那个
#include <iostream>
#include <cstdio>

using namespace std;

const double PII = 3.14159;

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

    printf("%.4lf %.4lf %.4lf\n", 2 * r, 2 * PII * r, PII * r * r);

    return 0;
}

1015:计算并联电阻的阻值

提示

这个题目,可能最困难的地方,就是对繁分数的阅读

请跟我在视频里模仿,我怎么念,你怎么念,就ok了

代码
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    double r1, r2;
    scanf("%lf%lf", &r1, &r2);

    double r = 1.0 / (1 / r1 + 1 / r2);
    printf("%.2lf\n", r);

    return 0;
}

第三节 标准数据类型

1414:【17NOIP 普及组】成绩

代码
#include <iostream>
#include <cstdio>

using namespace std;

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

    cout << a * 0.2 + b * 0.3 + c * 0.5;

    return 0;
}

1016:整型数据类型存储空间大小

代码
#include <iostream>

using namespace std;

int main() {
    int a;
    short b;

    cout << sizeof a << ' ' << sizeof b << endl;

    return 0;
}
代码02
// sizeof 后面直接接变量类型,要加上括号
// 后面接变量名,则是可加可不加
#include <iostream>

using namespace std;

int main() {
    cout << sizeof(int) << ' ' << sizeof(short) << endl;

    return 0;
}

1017:浮点型数据类型存储空间大小

代码
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    float a;
    double b;

    cout << sizeof(a) << ' ' << sizeof(b);

    return 0;
}

1018:其他数据类型存储空间大小

代码
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    bool a;
    char b;

    cout << sizeof(a) << ' ' << sizeof(b);

    return 0;
}

1019:浮点数向零舍入

提示

这里埋个伏笔,向零舍入,这个东西,有猫腻

特殊就特殊在负数的情况

代码
#include <iostream>

using namespace std;

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

    cout << (int)x;

    return 0;
}

1020:打印 ASCII 码

代码
#include <iostream>

using namespace std;

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

    cout << (int)c;

    return 0;
}

1021:打印字符

代码
#include <iostream>

using namespace std;

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

    cout << (char)x;

    return 0;
}

1022:整型与布尔型的转换

代码
#include <iostream>

using namespace std;

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

    bool b;
    b = a;

    a = b;
    cout << a;

    return 0;
}

1023:Hello,World! 的大小

提示

Hello, World!,这句话,请不要手敲,直接复制粘贴,因为你看不清楚是否有空格,逗号用的是英文的还是中文的

在代码里,我们用双引号括起来,"Hello, World!",表示这是一个字符串(你可以理解为一句话)

你想输出早上好,就可以 cout << "Good morning!";

你想输出六百六十六,就可以 cout << "666";,当然也可以 cout << 666;。前者当成一个字符串,后者当成一个数字

代码
#include <iostream>

using namespace std;

int main() {
    cout << sizeof("Hello, World!");

    return 0;
}

第四节 数据输入输出

2069:【例 2.12】糖果游戏

提示

这个题目,放后面做

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

using namespace std;

int t;

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

    t = a / 3;
    e += t, b += t;
    a = t;

    t = b / 3;
    a += t, c += t;
    b = t;

    t = c / 3;
    b += t, d += t;
    c = t;

    t = d / 3;
    c += t, e += t;
    d = t;

    t = e / 3;
    a += t, d += t;
    e = t;

    printf("%5d%5d%5d%5d%5d\n", a, b, c, d, e);

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

using namespace std;

int a[10];

int main(){
    for (int i = 0; i < 5; i++) cin >> a[i];

    for (int i = 0; i < 5; i++){
        int t = a[i] / 3;
        int l = (i - 1 + 5) % 5, r = (i + 1) % 5;
        a[l] += t, a[r] += t, a[i] = t;
    }

    for (int i = 0; i < 5; i++) printf("%5d", a[i]);
    puts("");

    return 0;
}

1024:保留 3 位小数的浮点数

提示

此题要求我们用单精度浮点数,所以用的是float

一般情况下,表示小数、实数、带小数点的数,用double

正常写程序的情况下,基本没用到过float(在一些节省空间的场景下会用到,受精确度影响)

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

using namespace std;

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

    printf("%.3f\n", a);

    return 0;
}

1025:保留 12 位小数的浮点数

代码
//float 7位,double 16位
#include <bits/stdc++.h>

using namespace std;

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

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

    return 0;
}

1026:空格分隔输出

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

using namespace std;

int main() {
    char a;
    int b;
    float c;
    double d;

    cin >> a >> b >> c >> d;

    //cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
    printf("%c %d %f %lf\n", a, b, c, d);

    return 0;
}

1027:输出浮点数

提示

//%f 以小数形式

//%e 以指数形式

//%g 已上两种,自动选择,宽度较小的格式输出

说实话,我平时只知道用 %lf,这些我还真不知道

代码
//%f 以小数形式
//%e 以指数形式
//%g 已上两种,自动选择,宽度较小的格式输出
#include <bits/stdc++.h>

using namespace std;

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

    printf("%f\n%.5f\n%e\n%g\n", a, a, a, a);

    return 0;
}

1028:字符菱形

提示

写代码的时候,表示一个字符的变量名,一般就用 c

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

using namespace std;

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

    printf("  %c\n", c);
    printf(" %c%c%c\n", c, c, c);
    printf("%c%c%c%c%c\n", c, c, c, c, c);
    printf(" %c%c%c\n", c, c, c);
    printf("  %c\n", c);

    return 0;
}

第五节 顺序结构实例

2070:【例 2.13】数字对调

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

using namespace std;

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

    cout << x % 10 * 100 + (x / 10) % 10 * 10 + x / 100;

    return 0;
}

2071:【例 2.14】平均分

代码
#include <iostream>
#include <cstdio>

using namespace std;

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

    printf("%.4lf\n", 1.0*(a*87+b*85)/(a+b));

    return 0;
}

2072:【例 2.15】歌手大奖赛

提示

这个问题,稍微为难人一点,就是题面的表达有点饶

说白一点,就是一道应用题的题面

多读两遍,用自己的话复述

代码
// 复述题意
// 解题思路
// 先求出最高分
// 求出最低分
// 去掉最高最低,求平均分

#include <bits/stdc++.h>

using namespace std;

int main() {
    double maxn = 9.6 * 6 - 9.4 * 5;
    double minn = 9.6 * 6 - 9.8 * 5;
    double ave = (9.6 * 6 - maxn - minn) / 4;
    printf("%5.2f\n", ave);


    return 0;
}

2073:【例 2.16】三角形面积

提示

海伦公式,要熟记,这在编程里用的比较多

只需要知道怎么用就可以,不用考虑证明

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

using namespace std;

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

    double p = (a + b + c) / 2;
    double s = sqrt(p * (p - a) * (p - b) * (p - c));

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

    return 0;
}

1029:计算浮点数相除的余

提示

这道题目,没有给输出的精度要求,是个坑点

测试点设置的时候,应该是按cout输出设置的

用cout直接输出,就ok了

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

using namespace std;

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

    int k = a / b;
    double r = a - b * k;

    cout << r << endl;

    return 0;
}

1030:计算球的体积

提示

球的体积公式,背一下

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

using namespace std;

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

    int k = a / b;
    double r = a - b * k;

    cout << r << endl;

    return 0;
}

1031:反向输出一个三位数

提示

数字翻转,是一个经典问题

有的题目要求输出前导零,有的题目不要输出前导零

此题,是要输出前导零的

代码
// 这个用位置制思想,拿到个十百位上的数字,依次输出
#include <iostream>

using namespace std;

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

    cout << x % 10 << x / 10 % 10 << x / 100 << '\n';

    return 0;
}
代码02
// 注意看scanf这句话,`%1d` 输入一个宽度的整数
// 请注意scanf、printf的名字叫做,格式化输入输入语句
// 这份代码的写法,在一些特殊场景下,会出现奇效
// 当然也是某一年考试中,引发我学了一下这个写法
// 否则我是不可能去学这种东西的
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    int a, b, c;
    scanf("%1d%1d%1d", &a, &b, &c);
    cout << c << b << a;

    return 0;
}
代码03
// 当你学了string的时候,就是字符串
// 你就可以把这个三位数,当成一个字符串处理
// 方法又多了一样
#include <bits/stdc++.h>

using namespace std;

int main() {
    string s;
    cin >> s;

    reverse(s.begin(), s.end());

    cout << s << endl;

    return 0;
}

1032:大象喝水查

提示

这个问题,首先要知道一下圆柱体体积的公式

然后,就是涉及到一个逻辑,不满一桶的时候,按一桶计算

所以,就是向上取整的问题(向上取整,是一个常见的操作)

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

using namespace std;

int main() {
    int h, r;
    cin >> h >> r;

    cout << (int)(20000 / (3.14 * r * r * h)) + 1 << endl;

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

using namespace std;

int main() {
    int h, r;
    cin >> h >> r;

    cout << ceil(20000 / (3.14 * r * r * h)) << endl;

    return 0;
}
代码03
#include <iostream>

using namespace std;

double PI = 3.14159, v;

int main() {
    int h, r;
    cin >> h >> r;

    v = PI * r * r * h;
    v /= 1000;//得到单位升

    cout << (int)(20 / v) + 1 << '\n';

    return 0;
}

1033:计算线段长度

提示

注意输入的坐标,是实数。要用 double

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

using namespace std;

int main() {
    double xa, ya, xb, yb;
    cin >> xa >> ya >> xb >> yb;

    printf("%.3lf\n", sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb)));

    return 0;
}

1034:计算三角形面积

代码
// 求出三条边的边长
// 利用海伦公式,求三角形面积
#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main() {
    double xa, ya, xb, yb, xc, yc;
    scanf("%lf%lf%lf%lf%lf%lf", &xa, &ya, &xb, &yb, &xc, &yc);

    double a = sqrt((xa - xb)*(xa - xb) + (ya - yb)*(ya - yb));
    double b = sqrt((xb - xc)*(xb - xc) + (yb - yc)*(yb - yc));
    double c = sqrt((xa - xc)*(xa - xc) + (ya - yc)*(ya - yc));

    double p = (a + b + c) / 2;
    double s = sqrt(p * (p - a) * (p - b) * (p - c));

    printf("%.2lf", s);

    return 0;
}

1035:等差数列末项计算

提示

等差数列的通项公式、求和公式,要记住的

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

using namespace std;

int main() {
    int a1, a2, n;
    cin >> a1 >> a2 >> n;

    int d = a2 - a1;
    int an = a1 + (n - 1) * d;

    cout << an << endl;

    return 0;
}

1036:A×B 问题

提示

这个问题中,给出了两个数的取值范围 1 <= A, B <= 50000

两个数都可能取到 50000, 两个50000相乘,就爆int了

int的取值范围,我们复习一下 [-2147483648, 2147483647]

可能爆int,就要换成long long,但不是都用long long是好的,该用的时候用

代码
#include <iostream>

using namespace std;

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

    cout << (long long)a * b;

    return 0;
}
代码02
// 这份代码中,1ll 表示的是 long long 类型的 1
// 这样相乘,不改变数值大小,但完成了变量类型的强制转换
#include <iostream>

using namespace std;

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

    cout << 1ll * a * b;

    return 0;
}

1037:计算 2 的幂

提示

首先,我们要了解一下,什么是指数

比如,\(2^3\) 表示 3 个 2 相乘,就是 8

我们写代码,可以用循环,可以用pow函数,还可以用位运算

在这里,和大家提示,pow这个函数我们很少使用,因为其执行效率低下,返回的是double,有精度问题

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

using namespace std;

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

    int res = 1;
    while (n--) {
        res *= 2;
    }

    cout << res << endl;

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

using namespace std;

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

    int ret = pow(2, n);
    cout << ret << '\n';

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

using namespace std;

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

    cout << (1 << n) << '\n';

    return 0;
}

1038:苹果和虫子

提示

这个问题和《大象喝水查》类似,都涉及到向上取整的问题

此题,没有破坏掉完整的一个苹果,这个苹果也算坏的

提示02

整数除以整数,向上取整,有几个方法

y除以x向上取整,(y + x - 1) / x,这是一个小公式,记住

代码
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
    int n,y,x;
    scanf("%d%d%d",&n,&x,&y);

    int ret;
    if (y % x == 0) ret = n - y / x;
    else ret = n - y / x - 1;

    if (ret < 0) ret = 0;

    cout << ret << '\n';

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

using namespace std;

int main() {
    int n, y, x, a;
    scanf("%d%d%d", &n, &x, &y);

    int ret = n - (y + x - 1) / x;

    if (ret < 0) ret = 0;
    cout << ret;

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

using namespace std;

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

    int res = max(0, n - (y + x - 1) / x);
    cout << res << endl;

    return 0;
}