zoukankan      html  css  js  c++  java
  • 数列问题 , 经典的链表操作

    int Node_Size ;
    
    struct Node{
           int x ;
           Node *next ;
    };
    
    Node * make_list(int n){
         int i  , j ;
         stack<int> stk ;
         Node *before , *root ;
         root = before = (Node *)malloc(sizeof(Node)) ;
         before->x = 1 ;
         before->next = NULL ;
         Node_Size = 1 ;
         for(i = 2 ; i <= n ; i++){
             j = i ;
             while(j){
                 stk.push(j % 10) ;
                 j /= 10 ;
             }
             while(!stk.empty()){
                  Node_Size++ ;
                  Node *now = (Node *)malloc(sizeof(Node)) ;
                  now->x = stk.top() ;
                  now->next = NULL ;
                  before->next = now ;
                  before = now ;
                  stk.pop() ;
             }
         }
         return root ;
    }
    
    void out(Node *root){
         Node * p ;
         p = root ;
         do{
            printf("%d->",p->x) ;
            p = p->next ;
         }while(p != NULL) ;
         puts("") ;
    }
    
    Node * delete_even(Node *List){
        Node *root  , * now , *before;
        root = before = List ;
        now = before->next ;
        while(before->next != NULL  && now->next != NULL){
            before->next = now->next ;
            before = now->next ;
            free(now) ;
            now = before->next ;
            Node_Size-- ;
        }
        if(now != NULL && now->next == NULL){
            before->next = NULL ;
            Node_Size-- ;
        }
        return  root ;
    }
    
    Node * delete_odd(Node *List){
        Node *root  , * now , *before;
        List = List->next ;
        root =  List ;
        Node_Size-- ;
        root = delete_even(root) ;
        return  root ;
    }
    
    int gao(int n){
        Node_Size = 0 ;
        Node * root = make_list(n) ;
        int k = 1 ;
        while(Node_Size > 1){
           // out(root) ;
           // cout<<Node_Size<<endl ;
            root = k? delete_even(root) : delete_odd(root) ;
            k ^= 1 ;
        }
        int ans = root->x ;
        free(root) ;
        return ans ;
    }
    
    class Test {
    public:
        static int remain (int   n)
        {
            return gao(n);
        }
    };
    //start 提示:自动阅卷起始唯一标识,请勿删除或增加。
    int main()
    {   
        cout<<Test::remain(0)<<endl;   
    } 
    //end //提示:自动阅卷结束唯一标识,请勿删除或增加。
    

      

  • 相关阅读:
    逆向笔记——PE文件相对虚拟地址(RVA)转文件偏移地址(FOA)
    逆向笔记——在PE任意一个节中添加代码
    FFT的物理意义
    Hilbert-Huang Transform: matlab 希尔伯特-黄变换: matlab实现
    交叉验证 Cross validation
    AAL template: ROI to brain lobe
    Types of intraclass correlation coefficience (ICC)
    统计:P值 & α值
    Notes: sensitivity & specificity
    Meet Github
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3581129.html
Copyright © 2011-2022 走看看