Assistance Required HDU - 1216 (模拟+链表)

题目
在这里插入图片描述

复习一些, 对于常用删除和插入操作的数据结构要用list, 对于遍历和查找更多的数据结构要用集合, 这道题很显然要用list

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <list>

using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 50000;

int n, ans[3010];
list<int> num(maxn);
void init()
{
    //初始化号码2...n
    int i = 2, j = 1;
    list<int>::iterator it;
    for(it = num.begin(); it != num.end(); it++)
        *it = i++;

    it = num.begin();
    while (it != num.end()){
        i = num.front();
        ans[j++] = i;
        num.pop_front();

        int k = 1;
        for(it = num.begin(); it != num.end(); it++, k++){
            if(k % i == 0)
                num.erase(it--);
        }
        it = num.begin();
    }
}
int main()
{
    init();
    while(cin >> n && n!=0)
        cout << ans[n] << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/84992326