62,614
社区成员
发帖
与我相关
我的任务
分享
public class Foo {
private static final int[] item = new int[] {1, 3, 9, 27, 81, 243};
private static int target;
private static final int[] sign = new int[item.length];
public static void main(String[] args) throws IOException {
int S = 0;
for (int anItem : item) {
S += anItem;
}
for (target = 1; target <= S; target++) {
calculate(0);
}
}
private static void calculate(int deepth) {
if (deepth == item.length) {
int S = 0;
for (int i = 0; i < item.length; i++) {
S += item[i] * sign[i];
}
if (S == target) {
for (int i = item.length - 1; i >= 0; i--) {
switch(sign[i]) {
case 0: break;
case 1:
System.out.print(" + " + item[i]);
break;
case -1:
System.out.print(" - " + item[i]);
}
}
System.out.println(" = " + target);
}
} else {
for (int s = -1; s < 2; s++) {
sign[deepth] = s;
calculate(deepth + 1);
}
}
}
}
from java
import java.util.Scanner;
public class CacuTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("please input the number you want to get:");
Scanner in=new Scanner(System.in);
int num;
num=in.nextInt();
System.out.println(toString(num));
}
private static String toString(int num) {
for(int i=1;i<=121;i++){
String str=Integer.toString(i, 3);
int sum=0;
for(int j=0;j<str.length();j++){
sum+=(int)Math.pow(3, j)*(Integer.parseInt(""+str.charAt(j))-1);
}
if(sum==num){
StringBuilder sb=new StringBuilder();
for(int k=4;k>=0;k--){
if(k>=str.length()){
continue;
}
switch(str.charAt(k)){
case '2':
if(sb.length()!=0){
sb.append("+");
}
sb.append(""+(int)Math.pow(3,k));
break;
case '1':
break;
case '0':
sb.append("-"+(int)Math.pow(3, k));
default:
break;
}
}
return sb.toString();
}
}
return null;
}
}
测试通过
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package chapter4;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author user
*/
public class Guess {
public static void main(String[] args) {
System.out.print("input your number:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
boolean flag = false;
for (int a = -1; a < 2; a++) {
for (int b = -1; b < 2; b++) {
for (int c = -1; c < 2; c++) {
for (int d = -1; d < 2; d++) {
for (int e = -1; e < 2; e++) {
if (num == 1 * a + 3 * b + 9 * c + 27 * d + 81 * e) {
if (e == 1) {
System.out.print("81");
flag = true;
}
if (d == 1) {
if (flag) {
System.out.print("+");
}
System.out.print("27");
flag = true;
} else if (d == -1) {
if (flag) {
System.out.print("-");
}
System.out.print("27");
flag = true;
}
if (c == 1) {
if (flag) {
System.out.print("+");
}
System.out.print("9");
flag = true;
} else if (c == -1) {
if (flag) {
System.out.print("-");
}
System.out.print("9");
flag = true;
}
if (b == 1) {
if (flag) {
System.out.print("+");
}
System.out.print("3");
flag = true;
} else if (b == -1) {
if (flag) {
System.out.print("-");
}
System.out.print("3");
flag = true;
}
if (a == 1) {
if (flag) {
System.out.print("+");
}
System.out.print("1");
} else if (a == -1) {
if (flag) {
System.out.print("-");
}
System.out.print("1");
}
}
}
}
}
}
}
}
}