0%

pta代码暂存

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
40
41
42
43
44
45
46
47
#include<stdio.h>
#include<string.h>
char rightToLeft(char ch){
if(ch==')')return '(';
else if(ch==']')return '[';
else if(ch=='}')return '{';
else return '0';
}
int correspondingFlag(char ch){
if(ch==')'||ch=='(')return 1;
else if(ch=='['||ch==']')return 2;
else if(ch=='{'||ch=='}')return 3;
else return 0;
}
int main(){
char str[55];
char sta[55]={0};
int index[55]={0};
int flag[4]={0};a
int top=0;
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;++i){
if(str[i]=='('||str[i]=='['||str[i]=='{'){//左括号入栈
sta[++top]=str[i];
index[top]=i;
}
else if(str[i]==')'||str[i]==']'||str[i]=='}'){//右括号
if(top==0||sta[top]!=rightToLeft(str[i])){//未匹配到对应左括号
sta[++top]=str[i];
index[top]=i;
}
else{//匹配到对应左括号
top--;//栈顶弹出
}
}
}
for(;top>0;top--){
flag[correspondingFlag(sta[top])]++;
}
if(flag[1]||flag[2]||flag[3]){
for(int i=1;i<4;++i){
while(flag[i]--)printf("%d,",i);
}
}
else printf("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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<stdio.h>
#include<ctype.h>
#include<string.h>
struct Stack{
char element[1000];
int cnt;
}postfix,tem;
void push(struct Stack *sta,char ch){
sta->element[++sta->cnt]=ch;
};
_Bool isempty(struct Stack sta){
return sta.cnt==0;
}
char pop(struct Stack *sta){
return sta->element[sta->cnt--];
};
char top(struct Stack sta){
return sta.element[sta.cnt];
};
_Bool higherthan(char a,char b){
return (a=='*'||a=='/')||(b=='+'||b=='-');
}
double calculate(double a,char op,double b){
if(op=='*')return a*b;
else if(op=='/')return a/b;
else if(op=='+')return a+b;
else if(op=='-')return a-b;
else return 0;
}
int main(){
char str[1000];
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;++i){
if(isdigit(str[i])){
push(&postfix,str[i]);
}
else if(str[i]=='('){
push(&tem,'(');
}
else if(str[i]==')'){
while(isempty(tem)==0&&top(tem)!='('){
push(&postfix,pop(&tem));
}
pop(&tem);
}
else{
while(isempty(tem)==0&&top(tem)!='('&&higherthan(top(tem),str[i])){
push(&postfix,pop(&tem));
}
push(&tem,str[i]);
}
}
while(isempty(tem)==0){
push(&postfix,pop(&tem));
}
double sta[10000];
int top=0;
for(int i=1;i<=postfix.cnt;++i){
if(isdigit(postfix.element[i])){
sta[++top]=postfix.element[i]-'0';
}
else {
double b=sta[top--];
double a=sta[top--];
sta[++top]=calculate(a,postfix.element[i],b);
}
}
printf("%.2f\n",sta[top]);
for(int i=1;i<=postfix.cnt;++i){
printf("%c ",postfix.element[i]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main(){
int num,cnt=0;
int ans[10000]={0};
while(scanf("%d,",&num)!=EOF){
ans[++cnt]=num;
}
for(int i=0;i<cnt;++i){
if(ans[i+1]!=ans[cnt-i]){
printf("No\n");
return 0;
}
}
printf("Yes\n");
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
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#include<malloc.h>
struct Node{
int value;
struct Node* child[2];
};
void insert(struct Node *now,int value){
int pos=now->value<value;
if(now->child[pos]==NULL){
now->child[pos]=(struct Node*)malloc(sizeof(struct Node));
now->child[pos]->value=value;
now->child[pos]->child[0]=now->child[pos]->child[1]=NULL;
}
else{
insert(now->child[pos],value);
}
}
int printByDepth(struct Node* now,int nowDepth,int depth){
if(nowDepth==depth){
printf("%d,",now->value);
return 1;
}
else{
int fla1=0,fla2=0;
if(now->child[0]!=NULL)fla1=printByDepth(now->child[0],nowDepth+1,depth);
if(now->child[1]!=NULL)fla2=printByDepth(now->child[1],nowDepth+1,depth);
return fla1||fla2;
}
}
void destroy(struct Node* now){
if(now->child[0]!=NULL)destroy(now->child[0]);
if(now->child[1]!=NULL)destroy(now->child[1]);
free(now);
}
int main(){
int n,depth;
struct Node* root=(struct Node*)malloc(sizeof(struct Node));
root->child[0]=root->child[1]=NULL;
scanf("%d%d,",&n,&(root->value));
for(int i=1,value;i<n;++i){
scanf("%d,",&value);
insert(root,value);
}
scanf("%d",&depth);
if(!printByDepth(root,1,depth))printf("-1");
destroy(root);
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
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#include<malloc.h>
struct Node{
int value;
struct Node* child[2];
};
void insert(struct Node *now,int value){
int pos=now->value<value;
if(now->child[pos]==NULL){
now->child[pos]=(struct Node*)malloc(sizeof(struct Node));
now->child[pos]->value=value;
now->child[pos]->child[0]=now->child[pos]->child[1]=NULL;
}
else{
insert(now->child[pos],value);
}
}
int printByDepth(struct Node* now,int nowDepth,int depth){
if(nowDepth==depth){
printf("%d,",now->value);
return 1;
}
else{
int fla1=0,fla2=0;
if(now->child[0]!=NULL)fla1=printByDepth(now->child[0],nowDepth+1,depth);
if(now->child[1]!=NULL)fla2=printByDepth(now->child[1],nowDepth+1,depth);
return fla1||fla2;
}
}
void destroy(struct Node* now){
if(now->child[0]!=NULL)destroy(now->child[0]);
if(now->child[1]!=NULL)destroy(now->child[1]);
free(now);
}
int main(){
int n,depth;
struct Node* root=(struct Node*)malloc(sizeof(struct Node));
root->child[0]=root->child[1]=NULL;
scanf("%d%d,",&n,&(root->value));
for(int i=1,value;i<n;++i){
scanf("%d,",&value);
insert(root,value);
}
scanf("%d",&depth);
if(!printByDepth(root,1,depth))printf("-1");
destroy(root);
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
40
#include<stdio.h>
#include<malloc.h>
struct Node{
int value;
struct Node* child[2];
};
void insert(struct Node *now,int value){
int pos=now->value<value;
if(now->child[pos]==NULL){
now->child[pos]=(struct Node*)malloc(sizeof(struct Node));
now->child[pos]->value=value;
now->child[pos]->child[0]=now->child[pos]->child[1]=NULL;
}
else{
insert(now->child[pos],value);
}
}
void destroy(struct Node* now){
if(now->child[0]!=NULL)destroy(now->child[0]);
if(now->child[1]!=NULL)destroy(now->child[1]);
free(now);
}
void preOrderTransverse(struct Node* now){
printf("%d,",now->value);
if(now->child[0]!=NULL)preOrderTransverse(now->child[0]);
if(now->child[1]!=NULL)preOrderTransverse(now->child[1]);
}
int main(){
struct Node* root=(struct Node*)malloc(sizeof(struct Node));
root->child[0]=root->child[1]=NULL;
scanf("%d,",&(root->value));
int value;
while(scanf("%d,",&value)!=EOF){
insert(root,value);
}
preOrderTransverse(root);
destroy(root);
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<stdio.h>
#include<malloc.h>
struct Node{
int value;
int height;
struct Node* child[2];
};
int Height(struct Node* now){
return now==NULL?0:now->height;
}
int max(const int a,const int b){
return a>b?a:b;
}
void freshHeight(struct Node* now){
now->height=1+max(Height(now->child[0]),Height(now->child[1]));
}
void rotate(struct Node* parent,struct Node* now,int pos,int direction){
struct Node *newroot=now->child[!direction];
if(parent!=NULL)parent->child[pos]=newroot;
now->child[!direction]=newroot->child[direction];
newroot->child[direction]=now;
freshHeight(now);
freshHeight(newroot);
if(parent!=NULL)freshHeight(parent);
}
int checkBalance(struct Node* now,int *flag,int *twice){
if(Height(now->child[0])-Height(now->child[1])==2){
*flag=0;
}
else if(Height(now->child[1])-Height(now->child[0])==2){
*flag=1;
}
else return 0;
*twice=Height(now->child[*flag]->child[*flag])<Height(now->child[*flag]->child[!(*flag)]);
return 1;
}
void reBalance(struct Node* parent,struct Node* now,int pos){
int rotateflag,twiceflag,flag;
flag=checkBalance(now,&rotateflag,&twiceflag);
if(flag){
if(twiceflag)rotate(now,now->child[rotateflag],rotateflag,rotateflag);
rotate(parent,now,pos,!rotateflag);
}
}
void insert(struct Node *now,int value){
int pos=now->value<value;
if(now->child[pos]==NULL){
now->child[pos]=(struct Node*)malloc(sizeof(struct Node));
now->child[pos]->value=value;
now->child[pos]->child[0]=now->child[pos]->child[1]=NULL;
now->child[pos]->height=1;
}
else{
insert(now->child[pos],value);
reBalance(now,now->child[pos],pos);
}
freshHeight(now);
}
void destroy(struct Node* now){
if(now->child[0]!=NULL)destroy(now->child[0]);
if(now->child[1]!=NULL)destroy(now->child[1]);
free(now);
}
void preOrderTransverse(struct Node* now){
printf("%d,",now->value);
if(now->child[0]!=NULL)preOrderTransverse(now->child[0]);
if(now->child[1]!=NULL)preOrderTransverse(now->child[1]);
}
int main(){
struct Node* root=(struct Node*)malloc(sizeof(struct Node));
root->child[0]=root->child[1]=NULL;
root->height=1;
scanf("%d,",&(root->value));
int value;
while(scanf("%d,",&value)!=EOF){
insert(root,value);
}
preOrderTransverse(root);
destroy(root);
return 0;
}
//这个代码其实有漏洞,请你理解后找出来