题目链接:P7018 [CERC2013] Bus - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题面翻译
有一个公交车,初始时车上有 n 个人,每停一站,车上会先下去一个人,然后再下去车上剩下的人数的一半的人。已知经过了 k 个站之后,车上没人了。现在,给出 k,求一开始车上的人数 n。
1 <= k <= 30。
Translated by Eason_AC
2020.11.12
题目描述
A bus with n passengers opens its door at the bus stop. Exactly half of its passengers and an additional half of a passenger get out. On the next stop, again, half of the passengers plus half of a passenger leave the bus. This goes on for k stops in total. Knowing that the bus leaves the last stop empty, and that no one was hurt during the trip, determine the initial number n of people in the bus.
输入格式
The first line of input contains the number of test cases T. The descriptions of the test cases follow:
The only line of each test case contains the number of stops k, 1 <= k <= 30.
输出格式
For each test case, output a single line containing a single integer—the initial number of bus passengers.
样例 #1
样例输入 #1
2
1
3
样例输出 #1
1
7
提示
Time limit: 1000 ms, Memory limit: 1048576 kB.
Central Europe Regional Contest (CERC) 2013
AC code:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int k;
cin>>k;
int sum=0;
while(k--)
{
sum++;
if(k>0)
sum*=2;
}
cout<<sum<<endl;
}
return 0;
}