第三章串数组广义表

大于输入年份且四位不同的数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
using namespace std;
int y;
int cnt[4];
int vis[10];
void change(int year){
int n=year;
int i=0;
while(n){
cnt[i++]=n%10;
n/=10;
}
}
bool check(int year){
change(year);
for(int i=0;i<10;i++)
vis[i]=0;
for(int i=0;i<4;i++)
if(vis[cnt[i]]==0)
vis[cnt[i]]=1;
else{
return false;
}
return true;
}
int main(){
cin>>y;
for(int i=y+1;i<10000;i++){
if(check(i)){
cout<<i;
return 0;
}
}
return 0;
}

奇数在前

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
const int N=100010;
int n;
int arr[N];
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i=0,j=n-1;
while(i<j){
while(arr[i]%2==1)i++;
while(arr[j]%2==0)j--;
if(i<j)swap(arr[i],arr[j]);
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}

字符串简单匹配算法

1
2
3
4
5
6
7
8
9
10
11
12
13
int index(Str str,Str substr){
int i=1,j=1;k=i;
while(i<=str.length&&j<=substr.length){
if(str.ch[i]==substr.ch[j]){
i++,j++;
}else{
j=1;
i=++k;
}
}
if(j>substr.length)return k;
return 0;
}

B是否为A的逆序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<cstring>
using namespace std;

int main(){
string a,b;
cin>>a>>b;
int len1=a.size();
int len2=b.size();
if(len1!=len2){
cout<<"不是";
return 0;
}
for(int i=0;i<len1;i++){
if(a[i]!=b[len1-1-i]){
cout<<"不是";
return 0;
}
}
cout<<"是的";
return 0;
}

B是否为A的子串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<cstring>
using namespace std;

int main(){
string a,b;
cin>>a>>b;
for(int i=0;i<a.size()-b.size();i++){
int flag=1;
for(int j=0;j<b.size();j++){
if(a[i+j]!=b[j]){
flag=0;
}
}
if(flag==1){
cout<<"是的";
return 0;
}
}
cout<<"不是的";
return 0;
}

乱码中找数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<cstring>
using namespace std;
const int N=100010;
string str;
int arr[N];
int ArrLen;
int tep[10];
int main(){
cin>>str;
int i=0;
while(i<str.size()){
if(str[i]>='0'&&str[i]<='9'){
int j=0;
while(str[i]>='0'&&str[i]<='9'&&i<str.size()){
tep[j++]=str[i]-'0';
i++;
}
int x=1;
int num=0;
for(int k=j-1;k>=0;k--){
num+=x*tep[k];
x*=10;
}
arr[ArrLen++]=num;
}else{
i++;
}
}
if(ArrLen==0)
cout<<"无";
else{
for(int i=0;i<ArrLen;i++){
cout<<arr[i]<<" ";
}
}
return 0;
}

矩阵处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//矩阵的转置
void fun(int A[][Max],int B[][Max],int n,int m){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
A[i][j]=B[j][i];
}
}
}
//矩阵相加
void fun(int n,int m,int A[][Max],int B[][Max],int C[][Max]){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
C[i][j]=A[i][j]+B[i][j];
}
}
}

//矩阵乘法
void fun(int n,int m,int q,int A[][Max],int B[][Max],int C[][Max]){
for(int i=0;i<n;i++){
for(int j=0;j<q;j++){
C[i][j]=0;
for(int k=0;k<m;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
}