hdu 1216简单的链表模拟

为了学习STL,全用STL实现的,代码省了不少。

/*

 * hdu1216/win.cpp

 * Created on: 2012-10-24

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

vector<int> result;

int current;

inline int myfun() {

    return current++;

}

void init() {

    int n = 35000;

    list<int> ml;

    current = 2;

    ml.resize(n);

    generate_n(ml.begin(), n, myfun);

    list<int>::iterator p, q;

    while(ml.size() > 1) {

        p = ml.begin();

        int t = *p;

        for(int i = 0, len = ml.size(); i < len; i++, p++) {

            if(i % t == 0) {

                q = p;

                q++;

                ml.erase(p);

                p = q;

                i++;

            }

        }

        result.push_back(t);

    }

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    init();

    int n;

    while(scanf("%d", &n) == 1 && n > 0) {

        printf("%d\n", result[n - 1]);

    }

    return 0;

}

你可能感兴趣的:(HDU)