zoukankan      html  css  js  c++  java
  • 模板集(更新中)

    源自挑战程序设计竞赛以及其他博客。

    平时常用的头文件。bits不敢滥用。

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <stdio.h>
    #include <cstring>
    #include <string>
    #include <cstdlib>
    #include <queue>
    #include <stack>
    #include <set>
    #include <vector>
    #include <map>
    #include <list>
    //#include <>
    using namespace std;

    1.插入排序

     1 void insertionsort(int* a,int n)
     2 {
     3     int v,j;
     4     for(int i=1;i<n;++i)
     5     {
     6         v=a[i];
     7         j=i-1;
     8         while(j>=0&&a[j]>v)
     9         {
    10             a[j+1]=a[j];
    11             j--;
    12         }
    13         a[j+1]=v;
    14         trace(a,n);
    15     }
    16 }

    2.冒泡排序

    sw代表swap的次数。

     1 int bubblesort(int *a,int n)
     2 {
     3     int sw=0;
     4     bool flag=1;
     5     for(int i=0;flag;i++)
     6     {
     7         flag=0;
     8         for(int j=n-1;j>=i+1;j--)
     9         {
    10             if(a[j]<a[j-1])
    11             {
    12                 swap(a[j],a[j-1]);
    13                 flag=1;
    14                 sw++;
    15             }
    16             //trace(a,n);
    17         }
    18     }
    19     return sw;
    20 }

    。。。

    3.双向链表

    struct Node{
        int key;
        Node *next,*prev;
    };
    
    Node *nil;
    Node *listSearch(int key)
    {
        Node *cur=nil->next;
        while(cur!=nil&&cur->key!=key)
        {
            cur=cur->next;
        }
        return cur;
    }
    
    void init(){
        nil=(Node*) malloc(sizeof(Node));
        nil->next=nil;
        nil->prev=nil;
    }
    
    void printList(){
        Node *cur=nil->next;
        int isf=0;
        while(1){
            if(cur==nil)
                break;
            if(isf++>0)
                printf(" ");
            printf("%d",cur->key);
            cur=cur->next;
        }
        
        printf("
    ");
    }
    
    void deleteNode(Node *t){
        if(t==nil){
            return;
        }
        t->prev->next=t->next;
        t->next->prev=t->prev;
        free(t);
    }
    
    void deleteFirst(){
        deleteNode(nil->next);
    }
    
    void deleteLast(){
        deleteNode(nil->prev);
    }
    
    void deleteKey(int key){
        deleteNode(listSearch(key));
    }
    
    void insert(int key){
        Node *x=(Node *)malloc(sizeof(Node));
        x->key=key;
        x->next=nil->next;
        nil->next->prev=x;
        nil->next=x;
        x->prev=nil;
    }
    int main()
    {
        int key,n,i,size=0,np=0,nd=0;
        char com[21];
        scanf("%d",&n);
        init();
        for(int i=0;i<n;++i){
            scanf("%s%d",com,&key);
            if(com[0]=='i')
            {
                insert(key);
                np++;
                size++;
            }
            else if(com[0]=='d')
            {
                if(strlen(com)>6){
                    if(com[6]=='F')
                        deleteFirst();
                    else if(com[6]=='L');
                        deleteLast();
                }
                else{
                    deleteKey(key);
                    nd++;
                }
                size--;
            }        
        }
        printList();
        return 0;
    }

     4.pair ,make pair 的使用(ALDS1——3——B Queue)

     1 int main()
     2 {
     3     int n,q,t;
     4     string name;
     5     queue<pair<string, int> > Q;
     6     
     7     cin>>n>>q;
     8     
     9     for(int i=0;i<n;i++)
    10     {
    11         cin>>name>>t;
    12         Q.push(make_pair(name,t));
    13     }
    14     
    15     pair<string ,int> u;
    16     int elaps=0,a;
    17     
    18     while(!Q.empty()){
    19         u=Q.front();
    20         Q.pop();
    21         
    22         a=min(u.second,q);
    23         u.second-=a;
    24         
    25         elaps+=a;
    26         if(u.second>0){
    27             Q.push(u);
    28         }
    29         else{
    30             cout<<u.first<<" "<<elaps<<endl;
    31         }
    32     }
    33     return 0;
    34 }

    5.vector的操作

    void print(vector<double> V){
        for(int i=0;i<V.size();i++){
            cout<<V[i]<<" ";
        }
        cout<<endl;
    }
    int main()
    {
        vector<double> V;
        V.push_back(0.1);
        V.push_back(0.2);
        V.push_back(0.3);
        
        V[2]=0.4;
        print(V);
        
        V.insert(V.begin()+2,0.8);
        print(V);
        
        V.erase(V.begin()+1);
        print(V);
        
        V.push_back(0.9);
        print(V);
        return 0;
    }

     

     6.list操作

     1 int main()
     2 {
     3     int q,x;
     4     char com[20];
     5     
     6     list<int> v;
     7     scanf("%d",&q);
     8     
     9     for(int i=0;i<q;++i)
    10     {
    11         scanf("%s",com);
    12         //insert
    13         if(com[0]=='i')
    14         {
    15             scanf("%d",&x);
    16             v.push_front(x);
    17         }
    18         //deleteLast
    19         else if(com[6]=='L')
    20         {
    21             v.pop_back();
    22         }
    23         //deleteFirst
    24         else if(com[6]=='F')
    25         {
    26             v.pop_front();
    27         }
    28         //delete
    29         else if(com[0]=='d')
    30         {
    31             scanf("%d",&x);
    32             for(list<int>::iterator it = v.begin();it!=v.end();it++)
    33             {
    34                 if(*it==x)
    35                 {
    36                     v.erase(it);
    37                     break;
    38                 }
    39             }
    40         }
    41     }
    42     
    43     int i=0;
    44     for(list<int>::iterator it = v.begin();it!=v.end();it++)
    45     {
    46         if(i++)
    47             printf(" ");
    48         printf("%d",*it);
    49     }
    50     
    51     printf("
    ");
    52     return 0;
    53 } 

     6.散列法

    #define M 1046527
    #define NIL (-1)
    #define L 14
    using namespace std;
    
    char H[M][L];
    int getChar(char ch)
    {
        if(ch=='A')
            return 1;
        if(ch=='C')
            return 2;
        if(ch=='G')
            return 3;
        if(ch=='T')
            return 4;
        return 0;
    }
    
    long long getKey(char str[])
    {
        long long sum = 0,p=1,i;
        for(i=0;i<strlen(str);i++)
        {
            sum+=p*(getChar(str[i]));
            p*=5;
        }
        
        return sum;
    }
    
    int h1(int key){
        return key%M;
    }
    int h2(int key){
        return 1+(key%(M-1));
    }
    
    int find(char str[]){
        long long key,i,h;
        key = getKey(str);
        for(i=0;;i++)
        {
            h=(h1(key)+i*h1(key))%M;
            if(strcmp(H[h],str)==0)
                return 1;
            else if(strlen(H[h])==0)
                return 0;
        }
        return 0;
    }
    
    int insert(char str[])
    {
        long long key,i,h;
        key =getKey(str);
        for(i=0;;i++)
        {
            h=(h1(key)+i*h1(key))%M;
            if(strcmp(H[h],str)==0)
                return 1;
            else if(strlen(H[h])==0)
            {
                strcpy(H[h],str);
                return 0;
            }
            
        }
        return 0;
    }
    int main()
    {
        int i,n,h;
        char str[L],com[9];
        
        for(i=0;i<M;i++)
        {
            H[i][0]='';
        }
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s%s",com,str);
            if(com[0]=='i'){
                insert(str);
            }
            else{
                if(find(str)){
                    printf("yes
    ");
                }
                else{
                    printf("no
    ");
                }
            }
        }
        return 0;
    }

    。。

  • 相关阅读:
    SAP OPEN UI5 Step 8: Translatable Texts
    SAP OPEN UI5 Step7 JSON Model
    SAP OPEN UI5 Step6 Modules
    SAP OPEN UI5 Step5 Controllers
    SAP OPEN UI5 Step4 Xml View
    SAP OPEN UI5 Step3 Controls
    SAP OPEN UI5 Step2 Bootstrap
    SAP OPEN UI5 Step1 环境安装和hello world
    2021php最新composer的使用攻略
    Php使用gzdeflate和ZLIB_ENCODING_DEFLATE结果gzinflate报data error
  • 原文地址:https://www.cnblogs.com/greenaway07/p/10546899.html
Copyright © 2011-2022 走看看