课程

C++ 基础课

当堂练习

printf示例代码

代码和输出一起看,观察到底发生了什么事情。

#include <cstdio>
using namespace std;

int main() {
    printf("我今天走了%d步.\n", 10);
    printf("今天气温%f度.\n", 37.8);
    printf("My name is %s.\n", "Engeeker");
    printf("A的下一个字母是%c.\n", 'B');
    printf("9x9=%d\n", 9*9);
    printf("今天是%d年%d月%d日.", 2024,11,10);
    return 0;
}

scanf示例代码

#include <cstdio>

using namespace std;

int main() {
    int step;
    scanf("I took a %d step today.\n", &step);
    double du;
    scanf("It's %lf degrees today.\n", &du);
    char name[20];
    scanf("My name is %s.\n", &name);
    printf("step:%d\ndu:%lf\nname:%s\n",step, du, name);
}

输入

I took a 100 step today.
It's 37.89 degrees today.
My name is Engeeker.

任务0

补全代码,运行后输出 I have 3 apples. 在小黑框里

#include <cstdio>
using namespace std;
int main() {
    int a = 3;
    printf("I have %d apples.", ___);
    return 0;
}

任务1

使用 scanf/printf 读入一个整数并输出。

#include <____>
using namespace std;


int main() {
    int x;

    scanf(_____,______);
    printf(_____,_____);

    return 0;
}

任务2

使用 scanf/printf 读入两个整数并输出它们的和。

#include <____>
using namespace std;


int main() {
    int a, b;

    scanf(_____,___,___);
    printf(_____,_____);

    return 0;
}

任务3

使用 scanf/printf 读入两个小数并输出它们的和。

#include <____>
using namespace std;

int main() {
    double a, b;

    scanf(_____,___,___);
    printf(_____,_____);

    return 0;
}