- sstzer 的博客
码力训练
- @ 2024-1-22 12:22:30
五大数据类型
- 整型: short, int, long, long long
- 浮点型: float, double
- 字符型: char
- 字符串型: string
- 布尔型:bool
五大运算符
- 算术运算符
+,-,*,/,% - 关系运算符
>,>=,<,<=,==,!=(等于是两个等号,不能在if()里写一个等号) - 逻辑运算符
&&,||,! - 自增自减运算符
i++,i--,++i,--i - 赋值运算符
=,+=,-=,*=,/=,%=
三大控制结构
- 顺序结构
t=a; a=b; b=t; - 选择结构
- 单选
- 二选一
- 三选一
- 多选一
- 嵌套
- 循环结构
- 一重循环
- 二重循环
- 三重循环
- 多重循环
- while循环
- 函数
- 系统函数
- 自定义函数
- 自定义非递归函数
- 自定义递归函数
c++基本框架
#include<bits/stdc++.h>
using namespace std;
int main()
{
return 0;
}
20秒以内完成
#include<bits/stdc++.h>
using namespace std;
int main()
{
if()
{
}
if()
{
}
else
{
}
if()
{
}
else if()
{
}
else
{
}
if()
{
if()
{
}
}
for()
{
}
for()
{
for()
{
}
}
for()
{
for()
{
for()
{
}
}
}
for()
{
for()
{
for()
{
for()
{
}
}
}
}
while()
{
}
do
{
}while();//不要少分号
continue;//跳出本次循环
break;//跳出整个循环
return 0;
}
求奇数之和,累乘积
#include<bits/stdc++.h>
using namespace std;
int main()
{
int sum=0;
for(int i=1; i<=100; i++)
{
if(i%2==1)
{
sum+=i;
}
}
cout<<sum<<endl;
int fac=1;
for(int i=1;i<=10;i++)
{
fac*=i;
}
cout<<fac<<endl;
return 0;
}
//鸡兔同笼,乘法口诀表
#include<bits/stdc++.h>
using namespace std;
int main()
{
for(int i=1; i<=35; i++) //i只鸡
{
for(int j=1; j<=35; j++) //j只兔
{
if(i*2+j*4==94 && i+j==35)
{
cout<<i<<" "<<j<<endl;
}
}
}
for(int i=1; i<=9; i++)
{
for(int j=1; j<=i; j++)
{
cout<<j<<"*"<<i<<"="<<j*i<<" ";
}
cout<<endl;
}
return 0;
}
//百钱买百鸡,时间模拟
#include<bits/stdc++.h>
using namespace std;
int main()
{
for(int i=0; i<=20; i++)
{
for(int j=0; j<=33; j++)
{
for(int k=0; k<=100; k++)
{
if(i*5+j*3+k/3.0==100 && i+j+k==100)
{
cout<<i<<" "<<j<<" "<<k<<endl;
}
}
}
}
for(int h=0;h<24;h++)
{
for(int m=0;m<60;m++)
{
for(int s=0;s<60;s++)
{
cout<<h<<":"<<m<<":"<<s<<endl;
}
}
}
return 0;
}
//一维数组
#include<bits/stdc++.h>
using namespace std;
int a[100];//方括号中的数必须是常量,不可以是变量,其下标是0到99,其值为0
int main()
{
srand(time(NULL));//设置随机数的种子
for(int i=0;i<100;i++)
{
a[i]=rand()%100;//赋值
}
for(int i=19;i>=0;i--)
{
cout<<a[i]<<" ";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int a[10][10];
int main()
{
srand(time(NULL));
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
a[i][j]=rand()%100;
}
}
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
cout<<endl;
}
return 0;
}
系统函数
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<max(114,514)<<endl;
cout<<min(114,514)<<endl;
cout<<sqrt(64)<<endl;//开根号
cout<<pow(2,10) <<endl;//指数函数
cout<<floor(3.99)<<endl; //向下取整
cout<<ceil(3.01)<<endl; //向上取整
cout<<round(5.98)<<endl;//四舍五入 取整
cout<<sin(3.1415926/2)<<endl;
cout<<cos(3.1415926/2)<<endl;
cout<<tan(3.1415926/4)<<endl;
return 0;
}
用户自定义函数
//自定义函数
#include<bits/stdc++.h>
using namespace std;
int add(int x,int y)//x,y为形参 , 在主函数之前声明实现函数,
{
return x+y;
}
double reduc(double x,double y);//在主函数之前声明函数,在主函数之后实现函数
int main()
{
cout<<add(8,10)<<endl;//8,10为实参
cout<<reduc(8,10)<<endl;//8,10为实参
return 0;
}
double reduc(double x,double y)
{
return x-y;
}
递归函数
//递归函数 实现累加和
#include<bits/stdc++.h>
using namespace std;
int sum(int n)
{
if(n==1)
{
return 1;
}
else
{
return sum(n-1)+n;
}
}
void print1(int n)
{
if(n>10)
{
return;
}
else
{
cout<<n<<" ";
print1(n+1);
}
}
void print2(int n)
{
if(n<1)
{
return;
}
else
{
print2(n-1);
cout<<n<<" ";
}
}
int main()
{
cout<<sum(10)<<endl;
print1(1);
cout<<endl;
print2(10);
return 0;
}
汉诺塔游戏

#include <bits/stdc++.h>
using namespace std;
// 汉诺塔游戏规则:
// - 有 3 根柱子(A、B、C)
// - 一开始所有圆盘从小到大叠在 A 柱上
// - 目标:把所有圆盘搬到 C 柱
// - 规则1:每次只能移动 1 个圆盘
// - 规则2:大的圆盘不能压在小的圆盘上面
// 递归函数:把 n 个圆盘从 a 柱,借助 b 柱,全部搬到 c 柱
void hanoi(int n, char a, char b, char c)
{
if (n < 1) // 递归跳出条件
{
return;
}
else //递归条件
{
// 第 1 步:先把上面的 n-1 个小盘子,从 a 搬到 b(c 先借来当中转站)
hanoi(n - 1, a, c, b);
// 第 2 步:现在 a 柱上只剩最大的那个盘子,直接搬到 c 柱
cout << "把 " << n << " 号圆盘,从 " << a << " 柱搬到 " << c << " 柱" << endl;
// 第 3 步:把刚才放在 b 柱的 n-1 个盘子,再搬到 c 柱(a 借来当中转站)
hanoi(n - 1, b, a, c);
}
}
int main()
{
int n;
cout << "请问要几个圆盘呀?(请输入一个数字,比如 3)";
cin >> n; // 从键盘读入小朋友输入的数字
hanoi(n, 'A', 'B', 'C'); // 开始游戏:从 A 柱搬到 C 柱,B 柱帮忙
return 0; // 程序圆满完成任务,说"再见"
}