首页 > 其他 > 详细

数列问题 , 经典的链表操作

时间:2014-03-05 15:38:23      阅读:408      评论:0      收藏:0      [点我收藏+]

 

bubuko.com,布布扣

 

 

 

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 //提示:自动阅卷结束唯一标识,请勿删除或增加。

  

数列问题 , 经典的链表操作,布布扣,bubuko.com

数列问题 , 经典的链表操作

原文:http://www.cnblogs.com/liyangtianmen/p/3581129.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!