首页 > 编程 > Java > 正文

java 蓝桥杯 幂方分解

2019-11-08 02:16:42
字体:
来源:转载
供稿:网友
问题描述  任何一个正整数都可以用2的幂次方表示。例如:  137=27+23+20   同时约定方次用括号来表示,即ab 可表示为a(b)。  由此可知,137可表示为:  2(7)+2(3)+2(0)  进一步:7= 22+2+20 (21用2表示)  3=2+20   所以最后137可表示为:  2(2(2)+2+2(0))+2(2+2(0))+2(0)  又如:  1315=210 +28 +25 +2+1  所以1315最后可表示为:  2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)输入格式  输入包含一个正整数N(N<=20000),为要求分解的整数。输出格式

  程序输出包含一行字符串,为符合约定的n的0,2表示(在表示中不能有空格)

import  java.util.*;public class Main{	public static void main(String args[])	{		Scanner cn=new Scanner (System.in);		int n=cn.nextInt();			System.out.PRintln(kk(n));	}	public static String kk(int n)	{		List<Integer> list=new LinkedList<Integer>();   //放置余数		String str=new String("");		int t,u=n;		while(true)		{			t=u%2;			u=u/2;			list.add(t);			if(u==0)break;		}		int count=0;		for(int i=list.size()-1;i>=0;i--)		{			if(list.get(i)==1)			{							if(count>0)str=str+"+";		 //控制str顶端第一次没有+号    					if(i==1)str=str+2;				if(i==0)str=str+"2(0)";				if(i!=0&&i!=1)str=str+"2("+kk(i)+")";	//进行递归求解								count=1;			}		}		return str;    	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表