hdu 1216 (vector || 打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1216

Assistance Required

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1468    Accepted Submission(s): 764

Problem Description
After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty dishes. The contestants would line up in a queue, one behind the other. Each contestant got a number starting with 2 for the first one, 3 for the second one, 4 for the third one, and so on, consecutively.
The first contestant in the queue was asked for his number (which was 2). He was freed from the washing up and could party on, but every second contestant behind him had to go to the kitchen (those with numbers 4, 6, 8, etc). Then the next contestant in the remaining queue had to tell his number. He answered 3 and was freed from assisting, but every third contestant behind him was to help (those with numbers 9, 15, 21, etc). The next in the remaining queue had number 5 and was free, but every fifth contestant behind him was selected (those with numbers 19, 35, 49, etc). The next had number 7 and was free, but every seventh behind him had to assist, and so on.

Let us call the number of a contestant who does not need to assist with washing up a lucky number. Continuing the selection scheme, the lucky numbers are the ordered sequence 2, 3, 5, 7, 11, 13, 17, etc. Find out the lucky numbers to be prepared for the next contest party.
 
Input
The input contains several test cases. Each test case consists of an integer n. You may assume that 1 <= n <= 3000. A zero follows the input for the last test case.
 
Output
For each test case specified by n output on a single line the n-th lucky number.
 
Sample Input
   
   
   
   
1 2 10 20 0
 

Sample Output
   
   
   
   
2 3 29 83


【题意】:

开始有很多人从2开始编号,依次为2,3,4,5,6.。。。

每次删除元素,从第一个开始删, 2,那么每隔2个删除那个元素,,于是删除了 4,6,8,10, 现在数组变成 2,3,5,7,9,11,13,15,17,19,21。。

然后从3开始删除 ,每隔3个删除删除元素 ,,于是删除了9,15,21, 现在变成 2,3,5,7,11,13,17,19...

然后从5开始删除每隔5个删除一个 于是删除了 19,35,49。。

【分析】

最终会剩下一系列数字,根据题目的数据范围,我们只要求到第3000个数就行。

【打表】

首先就想到了数组的模拟做法,经过反复尝试,确定第3000个数是33809.所以数据最大开到这里就行。

时间:140ms

#include <iostream>
#include <cstring>
using namespace std;
const int maxn=34000;
int vis[maxn];
int ans[3030];//保存结果
int main(){
	memset(vis,0,sizeof(vis));
	int cnt=1;
	for(int i=1;i<maxn;i++){
		int tmp=i+1;
		if(!vis[tmp]){
			ans[cnt++]=tmp;
			int idx=0;
			for(int j=tmp+1;j<=maxn;j++){
				if(!vis[j])
					++idx;
				if(idx==tmp)
				{	vis[j]=true;
					idx=0;
				}
			}
		}
	}
	 int t;
	while(cin>>t&&t){
		cout<<ans[t]<<endl;
	
         }
	return 0;
}
写出这个代码之后,我总感觉会超时,一直没敢交,后来用下面的vector方法做出来后,才交了这个代码,发现这个居然更快!

【vector】

用vector的erase操作,就可以删除指定位置的元素了

#include <iostream>
#include <vector>
using namespace std;
vector<int >vt;
int main(){
	 for(int i=2;i<=40000;i++)
		vt.push_back(i);
	for(vector<int >::iterator it=vt.begin();;it!=vt.end();)
	{
		for(vector<int >::iterator it2=it+(*it);it2<vt.end();it2+=(*it))
		{
			it2=vt.erase(it2);//erase 返回的是下一个位置
			it2--;
		}
		it++;
	}
	int t;
	while(cin>>t&&t){
		cout<<vt[t-1]<<endl;
	}  
}
时间:468ms

你可能感兴趣的:(vector,STL,HDU,打表)