import java.util.Scanner; /** 5个砝码 用天平称重时,我们希望用尽可能少的砝码组合称出尽可能多的重量。 如果只有5个砝码,重量分别是1,3,9,27,81。则它们可以组合称出1到121之间任意整数重量 (砝码允许放在左右两个盘中)。 本题目要求编程实现:对用户给定的重量,给出砝码组合方案。 例如: 用户输入: 5 程序输出: 9-3-1 用户输入: 19 程序输出: 27-9+1 * 用三进制做的 * @author wangmeng * */ public class 五个砝码问题 { int[] arr = new int[]{1,3,9,27,81}; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int a = scan.nextInt(); String s = ""; int[] result = new int[10]; int x = 0; while(a>=1){ int t = a%3; a = a/3; result[x] = t; x++; } StringBuffer sb = new StringBuffer(); for(int i = 0;i<=x;i++){ if(result[i]==2){ result[i] = -(result[i] - 1); result[i+1] = result[i+1] + 1;