标签:输入格式 ted tns 限制 value new uil print scanner
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in); StringBuilder ans = new StringBuilder(); long n = cin.nextLong(); ans = toTwo(n); // ans.append("a"); // System.out.println("a".equals(ans.ch))); System.out.println(ans); } public static StringBuilder toTwo(long n) { StringBuilder ans = new StringBuilder(); StringBuilder two = new StringBuilder(); while(n != 0) { if(n % 2 == 0) { two.append("0"); } else { two.append("1"); } n /= 2; } // System.out.println(two); // System.out.println(two.charAt(0)); int len = two.length(); int flag = 0; for(int i = len - 1; i >= 0; i--) { if(two.charAt(i) == ‘1‘) { //注意StringBuilder.charat()返回的是char if(flag == 1) { ans.append("+"); } else { flag = 1; } if(i == 0) { ans.append("2(0)"); } else if(i == 1) { ans.append("2"); } else if(i == 2) { ans.append("2(2)"); } else { ans.append("2(" + toTwo(i) + ")"); } } } return ans; } } //137 //2(2(2)+2+2(0))+2(2+2(0))+2(0) //样例输入 //1315 //样例输出 //2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
标签:输入格式 ted tns 限制 value new uil print scanner
原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/10419720.html