• 洛谷 P7018 [CERC2013] Bus


    [CERC2013] Bus

    题面翻译

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

    1 ⩽ k ⩽ 30 1\leqslant k\leqslant 30 1k30

    Translated by Eason_AC
    2020.11.12

    题目描述

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

    输入格式

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

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

    输出格式

    For each test case, output a single line containing a single integer—the initial number of bus passengers.

    样例 #1

    样例输入 #1

    2
    1
    3
    
    • 1
    • 2
    • 3

    样例输出 #1

    1
    7
    
    • 1
    • 2

    提示

    Time limit: 1000 ms, Memory limit: 1048576 kB.

    Central Europe Regional Contest (CERC) 2013

    分析:

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

    代码:

    #include
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    结束啦~~~

  • 相关阅读:
    深入理解注意力机制(上)-起源
    Windows使用小技巧
    电商数仓项目中各层的表
    【Android笔记41】使用Android实现一个简易版本的购物车小案例
    xlnet+bilstm实现菜品正负评价分类
    Svelte生命周期(加整体概述)
    uni-app常用场景速查记录
    JAVA面向对象三大特征
    第24章_瑞萨MCU零基础入门系列教程之内部温度传感器-TSN
    用Cmake快速生成vs工程
  • 原文地址:https://blog.csdn.net/m0_66603329/article/details/126490884