- 大纲要求
- 顺序结构程序设计 第五节 数据输入输出
- 1982:【19CSPJ普及组】数字游戏">例题,1982:【19CSPJ普及组】数字游戏
- 计算三角形面积">例题,计算三角形面积
- 等差数列末项计算">例题,等差数列末项计算
- 计算2的幂">例题,计算2的幂
- 苹果和虫子">例题,苹果和虫子
- 控制结构程序设计 if选择结构
- 收集瓶盖赢大奖">例题,收集瓶盖赢大奖
- 判断一个数能否同时被3和5整除">例题,判断一个数能否同时被3和5整除
- 判断能否被3,5,7整除">例题,判断能否被3,5,7整除
- 判断闰年">例题,判断闰年
- 求一元二次方程">例题,求一元二次方程
- 控制结构程序设计 switch语句
- 循环结构程序设计 for语句
- 循环结构程序设计 while语句
- 循环结构程序设计 循环嵌套
大纲要求
•【2】cin 语句,scanf 语句,cout语句,printf语句,赋值语句,复合语句
•【2】if语句,switch语句,多层条件语句
•【2】for语句,while语句,do while语句
•【3】多层循环语句
顺序结构程序设计 第五节 数据输入输出
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
cout << x;
return 0;
}
#include <cstdio>
using namespace std;
int main()
{
int x;
scanf("%d", &x);
printf("%d\n", x);
printf("%10d\n", x); // 占10个字符宽度,靠右对齐
printf("%-10d\n", x); // 占10个字符宽度,靠左对齐
double y;
scanf("%lf", &y);
printf("%lf\n", y);
printf("%.2lf\n", y); //小数点后保留2位
return 0;
}
// 如果输出是两个int,中间有一个空格
int a = 1, b = 2;
printf("%d %d", a, b);
// 如果输出是两个int,中间有一个加号
int a = 1, b = 2;
printf("%d+%d", a, b);
// 如果只是输出一句话,可以这样
printf("Hello world\n");
// 在双引号里面,是所见即所得
// %d 称之为,format specifier, 格式化符号
// % 后面跟什么,是不同变量类型,跟不同的东西
// 常见的,%d int,%lf double,%c 字符,%s 字符串
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char c;
//cin >> c;
//cout << c << '\n';
// a b c A B C * - 1 0
scanf("%c", &c);
printf("%c\n", c);
return 0;
}
// 学习一下字符数组、一维字符数组、二维字符数组
// string类
#include <bits/stdc++.h>
using namespace std;
char c;
string s1;
char s2[110];
char s3[110][110];
int main(){
// 以字符为例
cin >> c;
cout << c << '\n';
// 读入string类
cin >> s1;
cout << s1 << '\n';
// 读入字符数组
scanf("%s", s2);
printf("%s\n", s2);
// 读入二维字符数组
int n;
cin >> n;
for (int i = 0; i < n; i++) scanf("%s", s3[i]);
for (int i = 0; i < n; i++) printf("%s\n", s3[i]);
return 0;
}
// 占用字符宽度
// int类型 占多少个字符宽度
cin >> n;
printf("%8d %8d\n", n, n);
printf("%-8d %-8d\n", n, n);
// 保留小数点的问题
// double 类型保留小数点后几位
printf("%.20lf\n", 1.0 / 3);
// 一般就用printf解决保留小数点几位的问题,不用setprecision,主要是记不住
double x = 3.1415926;
cout << x << '\n';
cout << setprecision(5) << x << '\n';
cout << setprecision(4) << x << '\n';
cout << setprecision(3) << x << '\n';
cout << setprecision(2) << x << '\n'; // 3.1
cout << setprecision(1) << x << '\n'; // 3
// setprecision 不计算小数点
// #include <iomanip>
// 与 setw 字段宽度不同的是
// setprecision 的精度设置将保持有效,直到更改为其他值为止
// 流操作符 setw 可用于建立指定宽度的打印区域
// 空格“填充”在前面,所以它被认为是右对齐的
// 左对齐也能搞,麻烦
// 用一回,写一回
int y;
cin >> y;
cout << setw(8) << y << setw(8) << y << '\n';
注意看,这个参数是 point to already allocated objects,指向了一个已经存在的对象
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
scanf("%d", &x); // &取地址符号
printf("%d\n", x);
return 0;
}
/*
输入:123
输出:123
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
scanf("%d", x);
printf("%d\n", x);
return 0;
}
// 编译报错
std03.cpp:7:14: warning: format specifies type 'int *' but the argument has type 'int'
[-Wformat]
scanf("%d", x);
~~ ^
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
int *px = &x;
scanf("%d", px);
printf("%d\n", x);
return 0;
}
/*
输入:123
输出:123
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
int *px = &x;
scanf("%d", *px); //如果这样写
printf("%d\n", x);
return 0;
}
//编译报错
std03.cpp:8:14: warning: format specifies type 'int *' but the argument has type 'int'
[-Wformat]
scanf("%d", *px);
#include <bits/stdc++.h>
using namespace std;
int a[10];
int main(){
for (int i = 0; i < 5; i++)
scanf("%d", a + i);
//scanf("%d", &a[i]); //这两句话作用是一样的
return 0;
}
//读入字符串
#include <bits/stdc++.h>
using namespace std;
char s[10];
int main(){
scanf("%s", s); //这个时候,写的是数组名称,数组名本身就是一个指针
return 0;
}
// 在读入数据的时候,充分利用格式化的优点
// 输入数据1980-12-1
#include <bits/stdc++.h>
using namespace std;
int year, month, day;
int main(){
scanf("%d-%d-%d", &year, &month, &day);
printf("%d %d %d\n", year, month, day);
return 0;
}
// 上图,解释了,scanf里面有空格不影响的原因
// 还有scanf里面有具体符号时候,匹配的问题
// 还有具体限制读入一个字符宽度的数字
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
scanf("%1d%1d%1d", &a, &b, &c);
printf("%d %d %d\n", a, b, c);
return 0;
}
/*
123
*/
例题,1982:【19CSPJ普及组】数字游戏
// 这个就可以利用scanf的性质处理
例题,计算三角形面积
//海伦公式
p = (a+b+c) / 2;
s = sqrt(p * (p-a) * (p-b) * (p-c));
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
double x1, y1, x2, y2, x3, y3, a, b, c, p, s;
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
a = sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
b = sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1));
c = sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3));
p = (a+b+c)/2;
s = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2lf",s);
return 0;
}
例题,等差数列末项计算
//等差数列求和公式
#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;
}
例题,计算2的幂
//幂次,方 的叫法
#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;
}
例题,苹果和虫子
//还是题意,剩余多少完整的苹果
//也要注意苹果很少,时间很长的情况,会是什么结果【*】
控制结构程序设计 if选择结构
// 分类讨论,不重不漏【*】
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
if (x > 0) cout << '+' << '\n';
if (x > 0) cout << '+' << '\n';
else cout << "-" << '\n';
if (x > 0) cout << '+' << '\n';
else if (x == 0) cout << '0' << '\n';
else cout << '-' << '\n';
if (x > 0) cout << '+';
else{
if (x == 0) cout << '0';
else cout << '-';
}
//if嵌套,套一层而已
if (x >= 0){
if (x == 0) cout << '0' << '\n';
else cout << '+' << '\n';
}
return 0;
}
例题,收集瓶盖赢大奖
#include <bits/stdc++.h>
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;
}
例题,判断一个数能否同时被3和5整除
#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;
}
例题,判断能否被3,5,7整除
//
例题,判断闰年
// 百度百科,闰年
// 普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。
// 世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。
// 能被4整除,不能被100整除的,是闰年
// 能被400整除的,是闰年
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
if ((n % 4 == 0 && n % 100) || (n % 400 == 0)) puts("Y");
else puts("N");
return 0;
}
例题,求一元二次方程
// 本题对小学阶段有点麻烦,需要学习几下基本知识
// 重点是阅读一下,我是如何用代码表达意思的
#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);
printf("x1=%.5lf;x2=%.5lf\n", min(x1, x2), max(x1, x2));
}
return 0;
}
控制结构程序设计 switch语句
我从来不用switch..,你们用不用随意吧
// 还是认真的给一个示例
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
switch(n){
case 1: cout << 1 << '\n'; break;
case 2: cout << 2 << '\n'; break;
default: cout << "不是1,也不是2" << '\n';
}
return 0;
}
循环结构程序设计 for语句
// i从0变化到n-1,一共循环n次
for (int i = 0; i < n; i++){
}
for (int i = 1; i <= n; i++){
//...
}
// 格式
for (控制变量初始化表达式;条件表达式;增量表达式){
}
for (int i = 2; i <= n; i += 2){
}
// 循环嵌套,表达清楚循环的过程
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++){
//...
}
例题,求整数的和与均值
#include <bits/stdc++.h>
using namespace std;
int sum;
double ave;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
sum += x;
}
ave = sum * 1.0 / n;
printf("%d %.5lf\n", sum, ave);
return 0;
}
例题,最高的分数【打擂台】
//介绍”打擂台“
#include <bits/stdc++.h>
using namespace std;
int ans = -1;
int main()
{
int n;
cin >> n;
while (n--)
{
int x;
cin >> x;
ans = max(ans, x);
}
cout << ans << endl;
return 0;
}
例题,奇数求和
//判断奇数
//模2不为0
for (int i = m; i <= n; i++)
if (i % 2) sum += i;
例题,乘方计算
//这个幂次的数学概念,如果不清楚,视频问我
long long res = 1ll;
while (n--)
{
res *= a;
}
//1ll,1后面有两个l(小写L),表示的是long long类型
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, n;
cin >> a >> n;
long long res = 1ll;
while (n--)
{
res *= a;
}
printf("%lld\n", res);
return 0;
}
例题,求小数的某一位
// 对小数的理解
// 不能使用double
// 为什么呢?因为,题目求小数点后10000位。double才15位
// 这个时候,要模拟一下竖式除法
例题, 幂的末尾
// a 的 b 次幂,这肯定是一个很大数,爆int,爆longlong
// 好在题目只要求保留末三位
// 最后在输出的时候,如果不足三位,要在前面补零
// 这个需要考虑一下如何编程实现
循环结构程序设计 while语句
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
// 循环n次,这个写法,非常实用
while (n--){
//...
}
return 0;
}
while (true){
if (x == 100) break; //跳出循环
if (x % 2 == 1) continue; //本次循环到此为止,执行下一次循环
x += 1;
}
while (1){
x++;
}
for (int i = 0; i < n; i++){
int j = i;
while (j < n) j++;
// ...
}
/*
do while很少用
do{
// ...
}while (true);
*/
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int x = 100;
while (x < 100){
x++;
}
cout << x << '\n';
int y = 100;
do{
y++;
}while (y < 100);
cout << y << '\n';
return 0;
}
例题,球弹跳高度的计算
// 这个画图理解一下过程
// 务必画图,建议录制讲解视频
例题,角谷猜想
// 花点时间理解题意
// 建议录制讲解视频
循环结构程序设计 循环嵌套
例题,求阶乘的和
// 有两重循环,或者一重循环的写法
例题,金币
// 这个在循环上,有点小困难
// 困难的点,在如何控制每天取的币数,去多少天,一共取了多少天,怎么结束循环
例题,第n小的质数【判断质数】
// 介绍试除法判断质数模板
// 函数 模块化编程
// 判断质数 数论,三个中的其中之一
#include<bits/stdc++.h>
using namespace std;
bool is_prime(int x){
if (x < 2) return false;
for (int i = 2; i <= x / i; i++)
if (x % i == 0) return false;
return true;
}
int main()
{
int n;
cin >> n;
int cnt = 0;
for (int i = 2; ; i++){
if (is_prime(i)) cnt++;
if (cnt == n){
cout << i << '\n';
break;
}
}
return 0;
}