2021年度训练联盟热身训练赛第二场 A.Binarize It

Binarize It

题目链接:https://ac.nowcoder.com/acm/contest/12794/A

题目描述

Professor Boolando can only think in binary, or more specifically, in powers of 2. He converts any number you give him to the smallest power of 2 that is equal to or greater than your number. For example, if you give him 5, he converts it to 8; if you give him 100, he converts it to 128; if you give him 512, he converts it to 512.

The Problem:

Given an integer, your program should binarize it.

输入描述:

The first input line contains a positive integer,n, indicating the numberof values to binarize. The values are on the followingninput lines, one per line. Each input will contain an integer between2 and 100,000 (inclusive).

输出描述:

At thebeginning of each testcase, output “Inputvalue:v”wherevis the input value. Then,on the next output line, print the binarized version. Leave a blank line after the output for each test case.

示例1

输入
3
900
16
4000
输出
Input value: 900
1024

Input value: 16
16

Input value: 4000
4096

题目大意:

输入一个数,输出大于等于这个数的最小的2次方的数

代码如下:

#include<iostream>
using namespace std;

int main(){
    
    
	int t;
	scanf("%d",&t);
	while(t--){
    
    
		int n;
		scanf("%d",&n);
		int ans = 1;
		while(ans < n){
    
    
			ans*=2;
		}
		printf("Input value: %d\n",n);
		printf("%d\n",ans);
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45894701/article/details/114847789