基础语法:
字符串相关
字符串函数库:cstring
获取字符串长度:strlen(s)
字符串数组:使用二维数组表示 a[元素数目][最大长度],定义时赋值或循环赋值scanf("%s", strs[i])
字符串赋值:a=b→strcpy(a,b);
算法及数据结构回顾:
题解:
1151
二叉树,LCA,vector与Map应用
参考自:https://blog.csdn.net/qq_40597059/article/details/99699193,利用容器特性和遍历序列相对位置判断
#include<iostream>
#include<map>
#include<vector>
using namespace std;
map<int,int>pos;
vector<int>pre,in;
void lca(int inl,int inr,int preroot,int a,int b)
{
if(inl>inr) return;
int inroot=pos[pre[preroot]],ina=pos[a],inb=pos[b];
if(ina<inroot&&inb<inroot) lca(inl,inroot-1,preroot+1,a,b);
else if(ina>inroot&&inb>inroot) lca(inroot+1,inr,preroot+inroot-inl+1,a,b);
else if((ina<inroot&&inb>inroot)||(ina>inroot&&inb<inroot)) printf("LCA of %d and %d is %d.\n",a,b,in[inroot]);
else if(ina==inroot) printf("%d is an ancestor of %d.\n",a,b);
else printf("%d is an ancestor of %d.\n",b,a);
}
int main()
{
int m,n;
scanf("%d%d",&m,&n);
pre.resize(n+1);
in.resize(n+1);
for(int i=1;i<=n;i++)
{
scanf("%d",&in[i]);
pos[in[i]]=i;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&pre[i]);
}
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(pos[a]==0&&pos[b]==0) printf("ERROR: %d and %d are not found.\n",a,b);
else if(pos[a]==0) printf("ERROR: %d is not found.\n",a);
else if(pos[b]==0) printf("ERROR: %d is not found.\n",b);
else lca(1,n,1,a,b);
}
return 0;
}
1042
基础题,字符串操作
#include<cstdio>
#include<cstring>
int main()
{
char card[55][5]={" ","S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11" , "S12", "S13","H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "H11" , "H12", "H13","C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11" , "C12", "C13","D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11" , "D12", "D13","J1", "J2"};
char temp[55][5];
int order[54];
int count,i,j;
scanf("%d",&count);
for(i=1;i<=54;i++) scanf("%d",&order[i]);
// for(i=0;i<54;i++) printf("%d",order[i]);
for (j=1;j<=count;j++)
{
for(i=1;i<=54;i++)
{
strcpy(temp[order[i]],card[i]);
}
for(i=1;i<=54;i++) strcpy(card[i],temp[i]);
}
for(i=1;i<=54;i++)
{
if(i!=1) printf(" ");
printf("%s",card[i]);
}return 0;
}
1005
基础题
#include<cstdio>
#include<cstring>
int main()
{
char s[100];
int sum=0,j=0,ans[100];
scanf("%s",&s);
for(int i=0;i<strlen(s);i++) sum+=int(s[i])-48;
if(sum==0) printf("zero");
while(sum)
{
ans[j]=sum%10;
sum/=10;
j++;
}
while(j--)
{
if(ans[j]==0) printf("zero");
else if(ans[j]==1) printf("one");
else if(ans[j]==2) printf("two");
else if(ans[j]==3) printf("three");
else if(ans[j]==4) printf("four");
else if(ans[j]==5) printf("five");
else if(ans[j]==6) printf("six");
else if(ans[j]==7) printf("seven");
else if(ans[j]==8) printf("eight");
else if(ans[j]==9) printf("nine");
if(j!=0)
printf(" ");
}
return 0;
}
1001
基础题,数据小,感觉我的处理有些复杂了
#include<cstdio>
#include<cmath>
int main()
{
int a,b,ans,t1,t2,t3;
scanf("%d %d",&a,&b);
ans=a+b;
t1=ans%1000;
ans/=1000;
t2=ans%1000;
ans/=1000;
t3=ans%1000;
if(t3!=0) printf("%d,%03d,%03d",t3,abs(t2),abs(t1));
else if(t2!=0) printf("%d,%03d",t2,abs(t1));
else printf("%d",t1);
return 0;
}