约瑟夫环

bool checknum(int n)
{
	if(0 == (n % 7))
		return 1;
	while(n > 0)
	{
		if(7 == (n % 10))
			return 1;
		n /= 10;
	}

	return 0;
}
#if 0
int main()
{
	int i; 
	bool flag = false;
	int j = 0;
	int pos = 0;
	int k = 0;
	int a[Len] = {0};
	for(i = 0; i < Len; i++)
		a[i] = i + 1;


	while(k < Len)
	{
		if(a[pos])
		{
			j++;
			if(checknum(j))
			{
				cout << a[pos] << endl;
 				if(k == (Len - 1))
 				{
 					cout << a[pos] << endl;
 				}
				a[pos] = 0;
				k++;
			}
		}
		pos++;
		pos = pos % Len;
	}

	cout << "Res: " << j << endl;
	return 0;
}
#endif

#if 0
int main()
{
	vector<int> vec;
	int n = 1;


	for(int i = 0; i < Len; i++)
		vec.push_back(i + 1);

	int sizeVec = Len;

	vector<int>::iterator iterBeg = vec.begin();
	vector<int>::iterator iterEnd;
	while(sizeVec >= 2)
	{
		iterEnd = vec.end();
		if(iterBeg >= iterEnd)
		  iterBeg = vec.begin();

		if(checknum(n))
		{
			//cout << *iterBeg << endl;
			iterBeg = vec.erase(iterBeg);
			sizeVec--;
			n++;
			continue;
		}

		iterBeg++;
		n++;
	}
	cout << *iterBeg << endl;
	return 0;
}

#endif

struct num
{
	int id;
	struct num *next;
};
#if 1
int main()
{
	struct num* head = (struct num*)malloc(sizeof(struct num));
	head->id = 0;
	struct num* cur = head;

	for(int i = 1; i < Len; ++i)
	{
		cur->next = (struct num *)malloc(sizeof(struct num));
		cur = cur->next;
		cur->id = i;
	}

	cur->next = head;
	struct num* pre = head;
	cur = head->next;
	int a = 2;
	while(pre != cur)
	{
		if(checknum(a))
		{
			cout << cur->id << endl;
			pre->next = cur->next;
			free(cur);
		}
		else
		{
			pre = cur;
		}
		cur = pre->next;
		++a;
		
	}

	int pos = cur->id;
	free(cur);
	cout << "Res: " << pos << endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值