完美世界-2015校园招聘-java服务器工程师-成都站

给定一个整数,将该整数分解成多个2的幂次方相加的形式,每次都取最大的可以分解出来的2的幂次方

比如 65 64 1 

      1 1

      2 2

package wanmanshijie;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 
 * @author guoxm
 * @date 2014-12-16
 */
public class PartitionNum {
	public static void main(String args[]){
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		new PartitionNum().partition(num);
	}
	
	public void partition(int num){
		List list = new ArrayList();
		int helper = 1;
		while(helper <= num){
			if((helper&num) != 0){
				list.add(helper);
			}
			helper = helper<<1;
		}
		Collections.reverse(list);
		System.out.println(list);
	}
}

  

posted @ 2014-12-16 22:43  无心流泪  阅读(224)  评论(0编辑  收藏  举报