全新笔记
类型
整型 int
字符 char
浮点型 double
字符串 string
万能头
#include<bits/stdc++.h>
头文件
using namespace std;
int main( ){
}
域宽
setw( );
打断
一次
continue;
全部
break;
输入输出
#include<iostream>
输入
cin>> ;
特殊输入
cin.getline(数组名,长度);
输入(不用按Enter)
getch()
输出
cout<< ;
输出并换行
puts(数组名);
循环
for
for (初始化;条件;调整){
循环体
}
while
while (条件){
语句体
}
判断
单分支
if (条件){
代码
}
双分支
if (条件){
代码
}else{
代码
}
多分支
if (条件){
代码
}else if(条件){
代码
}else{
代码
}
数组
一维
定义
类型 数组名[长度]{定义};
取出
数组名[下标];//从零开始!!!
二维
定义
类型 数组名[行][列]{
{定义,定义,定义,...},
{定义,定义,定义,...},
{定义,定义,定义,...},
...
};
取出
数组名[行][列];//从零开始!!!
系统
#include<windows.h>
system("");
擦除 cls
等待按下任意键pause
格式化
类型
整型 -md
字符 c
浮点型保留几位小数f
输入
scanf("%类型",&变量);
输出
printf("%.类型",变量);
随机数
#include<cstdlib>
srand(time(0));
整数
rand()%最大减最小+最小
按位
按位异或:相同得0,不同得1;符号:^
按位与:同时为1,才为1,否则为0;符号:&
按位或:有1为1,否则为0;符号:|
按位非:1变0;0变1;符号:~
左移/右移:<< >>
ASCII码
0: 48
a: 97
A: 65
A与a相差: 32
课堂案例
1-翻牌游戏
#include <bits/stdc++.h>
#include<ctime>
#include<cstdlib>
using namespace std;
int arr[5][5];//卡牌数组
int num[5][5];//数字数组
int flag[9];//统计次数
int x,y,ix,iy,ia,ib,a,s;//三个数组的xy
int main(){
//1-显示规则
cout<<"虫洞卡牌\n规则如下\n";
cout<<"总共16张牌,由数字1-8组成,每个数字只出现两次\n";
cout<<"每一轮只允许翻两张牌\n";
cout<<"祝你好运(^_^)\n";
srand(time(0));
//2-显示卡牌
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
while (true){
int temp =rand()%8+1;
if(flag[temp]<2){
num[i][j]=temp;
break;
}else{
continue;
}
}
}
}
//3-第一回合显示
//4-开始翻译
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
cout<<"X ";
}cout<<endl;
}
system("pause");
system("cls");
while (true){
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(arr[i][j]>0)cout<<num[i][j]<<" ";
else cout<<"X ";
}cout<<"\n";
}
cout<<"第一次翻牌,行列用空格隔开:";
cin>>ix>>iy;
system("cls");
arr[ix][iy]++;//记录
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(arr[i][j]>0)cout<<num[i][j]<<" ";
else cout<<"X ";
}cout<<"\n";
}
cout<<"第二次翻牌,行列用空格隔开:";
cin>>ia>>ib;
system("cls");
arr[ia][ib]++;
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(arr[i][j]>0)cout<<num[i][j]<<" ";
else cout<<"X ";
}cout<<"\n";
}
s++;
if(num[ia][ib]==num[ix][iy]){
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(arr[i][j]>0) a++;
if(a==16)break;
}
}
cout<<"翻牌成功";
a++;
}
else {
cout<<"翻牌失败";
arr[ix][iy]--;
arr[ia][ib]--;
}
system("pause");
system("cls");
}cout<<"恭喜成功"<<"\n"<<"您一共用了"<<s<<"步";
return 0;
}
2-华容道
#include <bits/stdc++.h>
#include<ctime>
#include<windows.h>
#include<conio.h>
#include<iomanip>
using namespace std;
//1-设置数字
int cnt;
int a,b,c;//几乘几
int arr[100][100];//存放数字的二维数组
int flag[8];//统计每个数字出现的次数
int main(){
//生成随机数
srand(time(0));//随机数种子
cin>>a;//输入几乘几
//3-a行a列数字排列显示
for(int i=1;i<=a;i++){
for(int j=1;j<=a;j++){
if(i==a&&j==a) continue;//最后一个不放数据
while (true){
int temp =rand()%(a*a)+1;//生成1-8的随机数
if(flag[temp])continue;//判断这个数字是否存在
arr[i][j]=temp;//不存在:显示数字
flag[temp]=1;//做标记
break;
}
}
}
//4-显示棋盘
for(int i=1;i<=a;i++){
for(int j=1;j<=a;j++){
if(arr[i][j]==0) cout<<" ";
else {
cout<<setw(3)<<arr[i][j];
}
}
cout<<"\n";
}
//5-开始准备移动
int x=a,y=a;
while (1){
//6-判断是否获胜
for(int i=1;i<=a;i++){
for(int j=1;j<=a;j++){
if(arr[i][j]!=++b)continue;
if(b==a*a-b) break;
}
c=getch();//输入获取-cin>>c;
//做判断
if(c=='w'&&x<a) swap(arr[x][y],arr[x+1][y]),x++; //数值交换
else if(c=='s'&&x>1) swap(arr[x][y],arr[x-1][y]),x--;
else if(c=='a'&&y<a) swap(arr[x][y],arr[x][y+1]),y++;
else if(c=='d'&&y>1) swap(arr[x][y],arr[x][y-1]),y--;
else continue;//其他按键不参与
cnt++;//步数++
//清屏
system("cls");
//重新显示棋盘
for(int i=1;i<=a;i++){
for(int j=1;j<=a;j++){
if(arr[i][j]==0) cout<<" ";
else cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
}
}
cout<<"恭喜你,终于通关"<<endl;
cout<<"总共花了"<<cnt<<"步";
}
///////////////////////
工具箱
#include<ctime>
#include<cstring>
#include<iomanip>
#include<conio.h>
常用
字符位数
strlen( );
比较字符串大小
strcmp(数组名1,数组名2);