用于%s,不能连续读入
char input[100];
fgets(input,sizeof(input),stdin);读入一串字符,包括空格换行
-4d左对齐四个字符
"%02d" 进行格式化输出,当 x=7 时,输出结果为 "07";
当 x=12 时,输出结果为 "12";
当 x=123 时,输出结果为 "123"。
用数组储存个数
int f[105]={0};//初始化全部为0
s[f[x]]++;
比较字符串(重要)
strcmp(str1,“A”) 看str1第一位是不是A,
= return 0
< <
> >
>
翻转
reverse(a,a+n)//n为数组长度
复制
strcpy(destination, source) 将 source 中的内容复制到 destination 中
快排
sort(a,a+n)只有数据,比较函数可不写,默认从小到大
struct rich{
char s[60];
double num;
};
rich a[n];
bool cmp(rich n,rich m){//比较函数从大到小
return n.num>m.num; return strcmp(n.s,m.s;)<0;字符串只能用strcmp比较大小
}
sort(a,a+n,cmp);
二分查找(返回的是地址,所以-a,返回数组下标)
// 升序数组中:
upper_bound(a.begin(), a.end(), x)-a; // 查找第一个 > x的元素
lower_bound(a.begin(), a.end(), x)-a; // 查找第一个 >= x的元素
void binary_search(数组首地址,结束地址,要查找的数)
返回值为bool类型,找到了返回true。
空格抵消换行符
scanf(" %d",&x);
数学运算
pow(a,b) a的b次方
sqrt()开平方
超大整形存储
unsigned long long sum=0;
printf("%llu",sum);
获取字符串长度
char a[100];
strlen(a)//只能用于字符型数组
绝对值 fabs(a)
memset(a,0,sizeof(a));//初始化
最大公约数
while(m!=0){//辗转相除
r=n%m;//求余数
n=m;//大的变小的
m=r;//m就是最大公约数,小的变余数
}
最小公倍数
最大公约数*最小公倍数=m*n
翻转数
while(a>0){
x=x*10+a%10;
a/=10;
}
map键值对
map<char,int> mp={ {'h',4},{'g',3} };//键为char,值为int的map
map<string,string> m;
m["张三"]="吃汉堡";
cout<<"张三: "<<m["张三"];
stack st;
if(st.empty())//如果是空那么执行下面代码
.empty() 判断栈是否为空,空则返回true
.pop() 移除栈顶元素
.push(啥啥啥) 在栈顶增加元素
.size() 返回栈中元素数目
.top() 返回栈顶元素
空间一条线
if(x1*y2==x2*y1&&x1*x2>=0&&y2*y1>=0) return 1;//成比例,在一个象限符号相同
约瑟夫环
f(n,m) = (f(n−1,m) + m) % n。
分书
int x;
if(n>m){
if(m==13) m=12;
x=n/m;
if(n%m==13&&m==14) x+=2;
else if(n%m!=0) x+=1;
printf("%d\r\n",x);
}
else{
if(n==13) printf("2\r\n");
else if(n==0) printf("0\r\n");
else printf("1\r\n");
}
经典矩阵
4个中的最小值
printf(" %d",min(min(i+1,n-i),min(j+1,n-j)));
求最长下降子列
for(int i=0;i<n;i++){
dp[i]=1;//初始化,默认拦截一颗
for(int j=0;j<i;j++){
if(a[j]>a[i])//更新dp
dp[i]=max(dp[j]+1,dp[i]);//j在i前面
}
sum=max(sum,dp[i]);//更新最大值
}
朴素匹配
while(i<lens&&j<lena){//朴素匹配
if(s[t][i]==a[j]){
i++;j++;
}
else{//若起始点是1
i=i-j+1;//i=i-j+2
j=0;//j=1
}
}
中缀转前缀
从后往前扫描,从后往前写
中缀转后缀
后缀计算结果
希尔排序
一趟的
int d=n/2;
for(i=0;i<d;i++){
if(a[i]>a[i+d]){
int t=a[i];
a[i]=a[i+d];
a[i+d]=t;
}
}
堆判断 flag 0小根堆
for(i=1;i<=n/2;i++)
{
if(a[i]>a[i*2])
{
flag=0;
break;
}
if(a[i]>a[i*2+1]&&2*i+1<=n)
{
flag=0;
break;
}
}
树
int main(){//主函数
node *l;
l=creat();
printf("%d",sum(l));
return 0;}
typedef struct node{//结构体
struct node *lchild,*rchild;
char data;
}node;
node *creat(){//创建树
char ch;
scanf("%c",&ch);
node *l;
if(ch=='#') l=NULL;
else{
l=(node*)malloc(sizeof(node));
l->data=ch;
l->lchild=creat();
l->rchild=creat();
}
return l;
}
深度
int sum(node *&l){
int lchild,rchild;
if(l==NULL) return 0;
else{
lchild=sum(l->lchild);
rchild=sum(l->rchild);
return lchild>rchild?lchild+1:rchild+1;
}
宽度
int sum(node *&l,int t){
int lchild,rchild;
if(l==NULL) return 0;
else{
a[t]++;
if(k<a[t]) k=a[t];
sum(l->lchild,t+1);
sum(l->rchild,t+1);
}
return k;
}
叶结点个数
int sum(node *l)
{
if(l!=NULL)
{
if(l->lchild==NULL&&l->rchild==NULL)//先序遍历
k++;
sum(l->lchild);
sum(l->rchild);
}
return k;
}
度为2的结点
int out(treenode *l)
{
if(l!=NULL)
{
if(l->lchild!=NULL&&l->rchild!=NULL)
k++;
out(l->lchild);
out(l->rchild);
}
return k;
}
空恋与
int out(node *&l)
{
if(l!=NULL)
{
if(l->lchild==NULL)
k++;
if(l->rchild==NULL)
k++;
out(l->lchild);
out(l->rchild);
}
return k;
}
统计利用二叉树存储的森林中树的棵数
int out(node *l)
{
if(l!=NULL)
{
k++;
out(l->rchild);
}
return k;
}
交换二叉树孩子结点
void jiaohuan(node *&l)
{
if(l!=NULL)
{
node *p;
p=(node *)malloc(sizeof(node));
p=l->lchild;
l->lchild=l->rchild;
l->rchild=p;
jiaohuan(l->lchild);
jiaohuan(l->rchild);
}
}
void print1(node *l)//中序输出
{
if(l!=NULL)
{
print1(l->lchild);
printf("%c",l->data);
print1(l->rchild);
}
}
void print2(node *l)//先序输出
{
if(l!=NULL)
{
printf("%c",l->data);
print2(l->lchild);
print2(l->rchild);
}
}
int main()
{
node *l=creat();
jiaohuan(l);
print1(l);
cout<<endl;
print2(l);
}
平衡二叉树
void creat(node *&l){
char ch;
scanf("%c",&ch);
if(ch=='#') l=NULL;
else{
l=(node*)malloc(sizeof(node));
l->lchild=NULL;//输入的先序
l->rchild=NULL;
l->data=ch;
creat(l->lchild);
creat(l->rchild);
}
}
int i=0,a[1000];
void balance(node *l,int k){
if(l==NULL) {
a[i++]=k;return;}//存叶子结点的层数
else{
balance(l->lchild,k+1);
balance(l->rchild,k+1);
}
}
int main(){
node *l;
creat(l);
balance(l,1);
sort(a,a+i);//排序
if(a[i-1]-a[0]<2) printf("yes!");
else printf("no!");
return 0;}
978: 输出利用先序遍历创建的二叉树的中序遍历序列
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node *lchild;
struct node *rchild;
char data;
}node;
node *creat()
{
node *l;
char ch;
scanf("%c",&ch);
if(ch=='#')
{
l=NULL;
}
else
{
l=(node*)malloc(sizeof(node));
l->data=ch;
l->lchild=creat();
l->rchild=creat();
}
return l;
}
void out(node *&l)
{
if(l!=NULL)
{
out(l->lchild);
printf("%c",l->data);
out(l->rchild);
}
}
int main()
{
node *l;
l=creat();
out(l);
}
979: 输出利用先序遍历创建的二叉树的后序遍历序列
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node *lchild;
struct node *rchild;
char data;
}node;
node *creat()
{
node *l;
char ch;
scanf("%c",&ch);
if(ch=='#')
{
l=NULL;
}
else
{
l=(node*)malloc(sizeof(node));
l->data=ch;
l->lchild=creat();
l->rchild=creat();
}
return l;
}
void out(node *&l)
{
if(l!=NULL)
{
out(l->lchild);
out(l->rchild);
printf("%c",l->data);
}
}
int main()
{
node *l;
l=creat();
out(l);
}
984: 利用二叉树中序及先序遍历确定该二叉树的后序序列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}node;
node *creat(char *xian,char *zhong,int n)
{
char *p;
int k;
node *l;
l=(node*)malloc(sizeof(node));
if(n<=0)
return NULL;
l->data=*(xian);
for(p=zhong;p<zhong+n;p++)
{
if(*p==*(xian))
break;
}
k=p-zhong;
l->lchild=creat(xian+1,zhong,k);
l->rchild=creat(xian+k+1,p+1,n-k-1);
return l;
}
void print(node *l)
{
if(l!=NULL)
{
print(l->lchild);
print(l->rchild);
printf("%c",l->data);
}
}
int main()
{
char zhong[100];
char xian[100];
scanf("%s",zhong);
scanf("%s",xian);
int n=strlen(xian);
node *l;l=creat(xian,zhong,n);
print(l);
}
983: 利用二叉树中序及后序遍历确定该二叉树的先序序列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}node;
node *creat(char *hou,char *zhong,int n)
{
node *l;
char *p;
int k;
if(n<=0)
return NULL;
l=(node*)malloc(sizeof(node));
l->data=*(hou+n-1);
for(p=zhong;p<zhong+n;p++)
{
if(*p==*(hou+n-1))
break;
}
k=p-zhong;
l->lchild=creat(hou,zhong,k);
l->rchild=creat(hou+k,p+1,n-k-1);
return l;
}
void print(node*l)
{
if(l!=NULL)
{
printf("%c",l->data);
print(l->lchild);
print(l->rchild);
}
}
int main()
{
char zhong[100];
char hou[100];
scanf("%s",zhong);
scanf("%s",hou);
int n=strlen(hou);
node *l=creat(hou,zhong,n);
print(l);
}
980: 输出利用先序遍历创建的二叉树的层次遍历序列
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char data;
struct node *left;
struct node *right;
}node;
node* creat()
{
char ch;
node *l;
scanf("%c",&ch);
if(ch=='#')
l=NULL;
else
{
l=(node*)malloc(sizeof(node));
l->data=ch;
l->left=creat();
l->right=creat();
}
return l;
}
node *data[100];
int tou=0,wei=0;
void print(node *l)
{
if(l==NULL)
return ;
data[wei]=l;
wei++;
while(tou!=wei)
{
printf("%c",data[tou]->data);
if(data[tou]->left!=NULL)
{
data[wei]=data[tou]->left;
wei++;
}
if(data[tou]->right!=NULL)
{
data[wei]=data[tou]->right;
wei++;
}
tou++;
}
}
int main()
{
node *l;
l=creat();
print(l);
}