M

Sample Input

4
10 1
7 1
10 2
7 2

Sample Output

5
4
3
2

Hint

For the third sample test case, when BaoBao dies for the first time, the money he carries will be reduced from 10 to 5; When he dies for the second time, the money he carries will be reduced from 5 to 3.

代码  ps:除以2,向上取整,k的取值为0或1时退出循环,否则超时

#include<iostream>
using namespace std;
int main(){
    int t;
    int n,k;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&k);
        while(k--){
            if(n%2==0) n=n/2;
            else n=n/2+1;
            if(n==1||n==0) break;
        }
        printf("%d
",n);
    }
}

 

原文地址:https://www.cnblogs.com/-happy-/p/12518106.html