历届试题 幸运数

 

 

#include<iostream>
#include<algorithm>

using namespace std;

const int N = 100000+5;

int a[N];

void init()
{
	for(int i=0; i<N; i++){
		a[i] = 2*i+1;
	}
	
}

	int l, r;
void dfs(int start, int end, int cnt){
	
	int k = start;
	int t = a[start];
 
	for(int i = start; i < end; ++i)
		if((i+1) % t != 0)//因为数组a下标是从0开始的
		{
			a[k] = a[i]; //只更新 0-end之间的数 
			k++;
		}
		
	if(t < end){
		if(t > l)
			cnt++;
		dfs(start+1, end, cnt);  //递归查找
	}
	else{
		cout << cnt << endl; 
		return;
		}             //返回数组

}
int main()
{
	init();//初始化必须直接为奇数,不然在dfs中,cnt会不正确
		
	cin >> l >> r;
	
	dfs(0, r, 0);//因为数组下标从0开始,故start为0 
	
	
	return 0;
 } 

//本来想用STL的set打个表判断解下....然后 ,,,可能由于不熟陷入了死循环,一直不能输入,,等以后有机会了改正吧

#include<iostream>
#include<string.h>
#include<set>

using namespace std;

const int N = 1000+5;

//typedef pair<int, bool> PII;
//typedef make_pair(int a, bool b) Mk_PII(a, b);

set<int> a;
int PreSum[N];

void init()
{
	for(int i=1; i<N; i++){
		a.insert(i);
	}
}

void copy(set<int> &b, set<int> a){
	b.clear();
	for(set<int>::iterator it = a.begin(); it != a.end(); it++)
		b.insert(*it);
}

void settable()
{
	PreSum[0] = 0;
	
	for(set<int>::iterator it1 = a.begin(); it1 != a.end(); it1++){
		int tmp = *it1, cnt = tmp+1;
		if(tmp == 1){
			continue;
		}
	//	cout << tmp << endl;
		
			set<int> b;
			copy(b, a);
			for(set<int>::iterator it = a.begin(); it != a.end(); ++it){
							//		cout << *it << endl;

//				cout <<cnt << endl;
				if(cnt%tmp == 0){
			//		cout << *it << endl;

					b.erase(*it);
				}
				cnt ++;
			}
			copy(a, b);
			
	}
	
	for(set<int>::iterator it = a.begin(); it != a.end(); it++){
			PreSum[*it] = PreSum[*(--it)]+1;
			cout << PreSum[*it] << endl;
			it++;
	}
} 

 
int main()
{
	
	init();

	settable();//提前打表 
	
	//int n;
	
	//cin >> n;
	
	//for(int i=0; i<n; i++)
	{
		int l, r;
		
		cin >> l >>r;
		
		cout << PreSum[r] - PreSum[l] << endl; 
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值