哈理工OJ 1971 Power of Two(拆分整数为2的次幂的和)
来源:互联网 发布:免流服务器ip改域名 编辑:程序博客网 时间:2024/05/20 10:14
题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1971
Power of Two
Time Limit: 2000 MS Memory Limit: 65536 K
Total Submit: 197(83 users) Total Accepted: 92(77 users) Rating: Special Judge: No
Description
You have been given a number, you should decomposite it into sum of power of two in descending order. For example:
240 = 128 + 64 + 32 + 16
and
115 = 64 + 32 + 16 + 2 + 1
Input
The first line contains an integer T(T ≤ 50), indicating the number of test cases. Each test case contains one number n(1 ≤ n ≤ 2^31-1) the number we are going to decomposite.
Output
For each test case, output “Case #i: ” followed by one line the conresponding result.
Sample Input
2
240
115
Sample Output
Case #1: 240 = 128 + 64 + 32 + 16
Case #2: 115 = 64 + 32 + 16 + 2 + 1
Source
“科林明伦杯”哈尔滨理工大学第三届ACM程序设计团队赛
Author
xiaodao
【思路分析】先把2的次幂都求出来,然后直接扫一遍就OK了。
【AC代码】
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<queue>using namespace std;#define ll long longll a[33];void init(){ for(ll i=0;i<=32;i++) { a[i]=(ll)pow(2,i); }}ll re[35];int main(){ init(); int t,iCase=0; scanf("%d",&t); while(t--) { ll n,cnt=0; scanf("%lld",&n); ll x=n; for(ll i=32;i>=0;i--) { if(n>=a[i]) { n-=a[i]; re[cnt++]=a[i]; } } printf("Case #%d: %lld = %lld",++iCase,x,re[0]); for(ll i=1;i<cnt;i++) { printf(" + %lld",re[i]); } printf("\n"); } return 0;}
- 哈理工OJ 1971 Power of Two(拆分整数为2的次幂的和)
- leetcode231-Power of Two(判断一个整数n是否为2的方幂)
- [LeetCode-231] Power of Two(判断一个数是不是2的若干次幂)
- LeetCode 231. Power of Two(2的N次幂)
- [LeetCode] 231. Power of Two(判断整数是否是2的幂)
- [LeetCode]231. Power of Two-判断一个整数是否是2的幂次方
- LeetCode OJ 之 Power of Two (2的n次方)
- Power of Two 判断是否为2的次方
- LeetCode 231 Power of Two(2的幂)
- leetcode解题之231# Power of Two&326. Power of Three Java版 (判断是否为2,或者3 的幂)
- leetcode 231. Power of Two 判断是否为2的幂 Java
- 231. Power of Two--判断一个数是否为2的幂
- Power of Two 二的幂
- Power of Two | Leetcode 二的幂
- 哈理工 OJ Fire Maze(2次bfs)
- 判断一个整数是否为2的整数次幂
- Power of Two 判断一个数是不是2的幂
- Power of Two ----- 判断一个数是不是2的幂
- 简单的模拟——笨小猴
- (OK) (solved) How restore /cust partition - 华为全网通 honor 5x - KIW-AL10 - B228
- 集合的详解
- 排队接水-洛谷 1223
- Android学习总结(6)——Android日常开发总结的技术经验60条
- 哈理工OJ 1971 Power of Two(拆分整数为2的次幂的和)
- 一个简单的线程例子
- Spring定时任务的几种实现
- 构建类似Office 2007框架的WPF应用程序(3)
- 关联规则挖掘算法-FP-Growth
- mybatis批量查询传集合问题
- Scientific.Toolworks.Understand.v4.0.872.Incl.Keygen-AMPED
- 玩转计划任务命令:schtasks
- nagios分组出图代码实现讲解[1]