вот мой вывод:
1 = + 1
2 = - 1 + 3
74 = - 1 + 3 - 9 + 81
1234 = + 1 - 9 + 27 - 243 - 729 + 2187
3000 = + 3 + 81 + 729 + 2187
package com.javarush.task.task30.task3012;
/*
Получи заданное число
*/
import java.util.ArrayList;
import java.util.List;
public class Solution {
private static List<String> integerList = new ArrayList<>();
public static void main(String[] args) {
Solution solution = new Solution();
solution.createExpression(1);
integerList.clear();
solution.createExpression(2);
integerList.clear();
solution.createExpression(74);
integerList.clear();
solution.createExpression(1234);
integerList.clear();
solution.createExpression(3000);
}
public void createExpression(int number) {
int ostatok = number % 3;
if (ostatok == 0) integerList.add("0");
else if (ostatok == 1) integerList.add("+");
else if (ostatok == 2) {
integerList.add("-");
number += 1;
}
if (number == 1) {
String result = "";
int sq = 1;
int num = 0;
for (int i=0; i<integerList.size();i++) {
if (!integerList.get(i).equals("0")) {
result = result.concat(" ").concat(integerList.get(i).concat(" ").concat(String.valueOf(sq)));
num += sq * (integerList.get(i).equals("+") ? +1 : -1);
}
sq = sq * 3;
}
System.out.println(num + " =" + result);
} else {
number = number / 3;
createExpression(number);
}
}
}