zoukankan      html  css  js  c++  java
  • Eat Candy(暴力,水)

    Eat Candy

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 8  Solved: 6
    [Submit][Status][Web Board]

    Description

       There is a box with infinite volume. At first there are ncandies in the box. Then every second you will eat some candies, left half of candies (round down) in the box. Then add k candies into the box. How many candies there are in the box after 109+7seconds?

    Input

       There are multiple test cases. In each test case, there are only one line contains two integers n,k(1≤n,k≤109+7)

    Output

     

        For each test case, output the answer in one line.

    Sample Input

    4 5
    2 3

    Sample Output

    9
    5

    HINT



    In the first test case:


    1st second, 4->2, 2+5 = 7


    2nd second, 7->3, 3+5 = 8


    3rd second, 8->4, 4+5 = 9


    4th second, 9->4, 4+5 = 9



    1000000007th            9


    So there are 9 candies in the box after 1000000007 seconds.

    题解:每次n为n的一半加k,问经过1e9+7次后的值;
    代码:
    import java.util.Scanner;
    
    
    public class EatCandy {
        public static void main(String[] args){
            int n, k;
            Scanner cin = new Scanner(System.in);
            while(cin.hasNext()){
                n = cin.nextInt();
                k = cin.nextInt();
                while(n != (n/2 + k)){
                    n = n / 2 + k;
                }
                System.out.println(n);
            }
        }
    }
  • 相关阅读:
    idea从零搭建简单的springboot+Mybatis
    关于监听微服务功能
    final
    mysql数据库分页查询优化
    Mysql中“select ... for update”排他锁(转)
    HashMap(常用)方法个人理解
    5.1 Object类型
    5.3 Date类型
    4.1-4.2 基本类型及引用类型,执行环境及作用域
    20.JSON
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5501493.html
Copyright © 2011-2022 走看看