洛谷 P7018 [CERC2013] Bus

本文介绍了CERC2013中的一道数学问题,涉及公交车每站下车人数的动态变化。题目要求根据车站数反推初始乘客数。通过逐站倒推,每次将剩余人数翻倍并加一,最终得出初始人数。文章提供了C++代码示例进行求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[CERC2013] Bus

题面翻译

有一个公交车,初始时车上有 nnn 个人,每停一站,车上会先下去一个人,然后再下去车上剩下的人数的一半的人。已知经过了 kkk 个站之后,车上没人了。现在,给出 kkk,求一开始车上的人数 nnn

1⩽k⩽301\leqslant k\leqslant 301k30

Translated by Eason_AC
2020.11.12

题目描述

A bus with nnn 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 kkk 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 nnn of people in the bus.

输入格式

The first line of input contains the number of test cases TTT. The descriptions of the test cases follow:

The only line of each test case contains the number of stops kkk, 1≤k≤301 \leq k \leq 301k30.

输出格式

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

分析:

因为每次到一个站都会先下1个人,在下下去车上剩下的人数的一半的人,所以我们可以直接倒推~~

代码:

#include<bits/stdc++.h>

using namespace std;

int T;

int k;

int n;

int main()
{
    cin>>T;
    
    while(T--)
    {
        n=0;//将n清0
        
        cin>>k;
        
        for(int i=1;i<=k;i++) 
        {
			n*=2;//将下车后剩下的人数乘2
			
			n++;//再将n+1
        }
        
        cout<<n<<endl;//输出n
    }
    
    return 0;
}

结束啦~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

harmis_yz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值