UVA 10940 - Throwing cards away II(规律)

本文深入探讨了信息技术领域的关键概念和技术,包括但不限于前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具、嵌入式硬件、音视频基础等,提供了一个从技术核心到具体实现细节的全面视角。从大数据处理到AI音视频处理,从测试到DevOps,涵盖了广泛的技术领域,旨在帮助读者深入理解并掌握这些技术的精髓。

Problem G: Throwing cards away II

Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck:

Throw away the top card and move the card that is now on the top of the deck to the bottom of the deck.

Your task is to find the last, remaining card.

Each line of input (except the last) contains a positive number n ≤ 500000. The last line contains 0 and this line should not be processed. For each number from input produce one line of output giving the last remaining card. Input will not contain more than 500000 lines.

Sample input

7
19
10
6
0

Output for sample input

6
6
4
4
题意:扑克,先是1-n排序,1在顶,n在底,然后每次抽掉一张,第二张放到底,求最后剩下那张是多少。
思路:先写了个模拟的超时了,然后打出来看规律,出了n=1,ans=1,其他都是按 2,(2,4),(2,4,6,8),(2..2^N)这样排的
代码:
#include <stdio.h>
const int MAXN = 500005;
int ans[MAXN];

void init() {
	ans[1] = 1;
	int n = 1;
	for (int i = 2; i <= 500000;) {
		int save = (1<<n);
		for (int j = i; j < i + save / 2 && j <= 500000; j ++) {
			ans[j] = (j - i + 1) * 2;
		}
		i += save / 2;
		n ++;
	}
}

int main() {
	int n; init();
	while (~scanf("%d", &n) && n) {
		printf("%d\n", ans[n]);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值