题解 | #扭蛋机#java二叉树
扭蛋机
https://www.nowcoder.com/practice/9d26441a396242a9a0f7d2106fc130c7
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int traget = in.nextInt(); //假设这是二叉树 //如果结果为奇数,则走左边,反之走右边 List<Character> path = new ArrayList<>(); while(traget > 0){ if(traget % 2 == 0){ //为偶数的话证明是走二叉树右边,意味着2*x + 2了 path.add('3'); traget = (traget - 2) / 2; }else{ //奇数的话走了左边 path.add('2'); traget = (traget - 1) / 2; } } Collections.reverse(path); StringBuilder sb = new StringBuilder(); for(int i = 0;i<path.size();i++){ sb.append(path.get(i)); } System.out.print(sb); } }