第四天!!

第三天完全鸽了...

今天A了希尔和逆波兰

代码存一下

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<vector>
 6 using namespace std;
 7 long long cnt;
 8 int l;
 9 int A[1000000];
10 int n;
11 vector<int> G;
12 void insertsort(int A[],int n,int g){
13     for(int i=g;i<n;i++){
14         int v=A[i];
15         int j=i-g;
16         while(j>=0&&A[j]>v){
17             A[j+g]=A[j];
18             j-=g;
19             cnt++;
20         }
21         A[j+g]=v;
22     }
23 }
24 void shell(int A[],int n){
25     for(int h=1; ; ){
26         if(h>n){
27             break;
28         }
29         G.push_back(h);
30         h=3*h+1;
31     }
32     for(int i=G.size()-1;i>=0;i--){
33         insertsort(A,n,G[i]);
34     }
35 }
36 int main(){
37     scanf("%d",&n);
38     for(int i=0;i<n;i++){
39         scanf("%d",&A[i]);
40     }
41     cnt=0;
42     shell(A,n);
43     printf("%d\n",G.size());
44     for(int i=G.size()-1;i>=0;i--){
45         printf("%d",G[i]);
46         if(i){
47             printf(" ");
48         }
49     }
50     printf("\n%d\n",cnt);
51     for(int i=0;i<n;i++){
52         printf("%d\n",A[i]);
53     }
54     return 0;
55 }

希尔排序以上....感觉其实无用

 1 #include<iostream>
 2 #include<stack>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 using namespace std;
 7 stack<int> S;
 8 int a,b,x;
 9 char s[1000500];
10 int main(){
11     while(cin>>s){
12         if(s[0]=='+'){
13             a=S.top();
14             S.pop() ;
15             b=S.top() ;
16             S.pop() ;
17             S.push(a+b);  
18         }
19         else 
20         if(s[0]=='-'){
21             a=S.top();
22             S.pop() ;
23             b=S.top() ;
24             S.pop() ;
25             S.push(b-a);  
26         }
27         else
28         if(s[0]=='*'){
29             a=S.top();
30             S.pop() ;
31             b=S.top() ;
32             S.pop() ;
33             S.push(a*b);  
34         }
35         else{
36             S.push(atoi(s)); 
37         }
38     }
39     printf("%d\n",S.top());
40     return 0;
41 }

这是逆波兰栈的运用

明天写个queue好了,如果可以再试试链表

就酱

猜你喜欢

转载自www.cnblogs.com/accidia/p/9427128.html