F-Playoff题解

 

目录

题目描述:

题目分析:

Ac代码:



原题链接

题目描述:

Consider a playoff tournament where 2^n athletes compete. The athletes are numbered from 1 to 2^n.

The tournament is held in n stages. In each stage, the athletes are split into pairs in such a way that each athlete belongs exactly to one pair. In each pair, the athletes compete against each other, and exactly one of them wins. The winner of each pair advances to the next stage, the athlete who was defeated gets eliminated from the tournament.

The pairs are formed as follows:

  • in the first stage, athlete 11 competes against athlete 2; 3 competes against 4; 5 competes against 6, and so on;
  • in the second stage, the winner of the match "1–2" competes against the winner of the match "3–4"; the winner of the match "5–6" competes against the winner of the match "7–8", and so on;
  • the next stages are held according to the same rules.

When athletes x and y compete, the winner is decided as follows:

  • if x+y is odd, the athlete with the lower index wins (i. e. if x<y, then x wins, otherwise y wins);
  • if x+y is even, the athlete with the higher index wins.

The following picture describes the way the tournament with n=3 goes.

 

Your task is the following one: given the integer n, determine the index of the athlete who wins the tournament.

Input

The first line contains one integer t (1≤t≤30) — the number of test cases.

Each test case consists of one line containing one integer n (1≤n≤30).

Output

For each test case, print one integer — the index of the winner of the tournament.

Sample 1

InputcopyOutputcopy
2
3
1
7
1

Note

The case n=3 is shown in the picture from the statement.

If n=1, then there's only one match between athletes 1 and 2. Since 1+2=3 is an odd number, the athlete with the lower index wins. So, the athlete 1is the winner.

题意就是相邻的两个运动员要相互比试设两位运动员为x,y

两种情况:

1.x+y为奇数,那么谁小谁就赢了进入到下一场比赛

2.x+y为偶数,结果与奇数时相反

题目分析:

看着题目挺复杂,其实是个水题,通过画图就可发现规律:

1.当只有一个运动员时那肯定就是他自己赢了

2.当大于1时由于是2^n那么一定是偶数,那么就符合以下规律

 

所以除了第一种只有一个运动员的情况外,无论n是多少,最终结果都是所有数中的最大的奇数,也就是2^n-1;

Ac代码:

#include<bits/stdc++.h>
using namespace std;
void op(int t)
{
	int all=pow(2, t);
	if (all == 1)
		printf("1\n");
	else
		printf("%d\n",all-1);
}
int main()
{
	int n;
	scanf("%d", &n);
	while (n--)
	{
		int t;
		scanf("%d", &t);
		op(t);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值