程序输出包含一行字符串,为符合约定的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; }}
新闻热点
疑难解答