题目描述
ACM公司生产金币的设备出了问题,使得最近生产的10批金币的重量出现了波动:本来金币的标准重量是10克,但现在有的可能是11克,有的可能9克,也有可能是10克。
现在只知道同一批金币的重量是相同的,你的任务是要把每批的单枚金币的重量找出来。
你的设备有一个电子秤,但只允许称量一次!
你从第1批中取1枚金币,第2批取3枚,…第i批取3(i−1)枚…,第10批取39枚,总共29524枚。将这29524枚金币放在电子秤上,得到了总重量,就交给你的程序去!
输入
有多个测试序列,每个测试序列一行,包含一个6位的正整数W(265716≤W≤324764),表示29524枚金币的总重量
输出
每个测试序列输出一行,包含10个用空格分开的正整数,分别表示10批金币的单枚重量,注意行尾没有空格。
样例输入 Copy
265716
324764
295240
样例输出 Copy
9 9 9 9 9 9 9 9 9 9
11 11 11 11 11 11 11 11 11 11
10 10 10 10 10 10 10 10 10 10
import java.util.Scanner;
public class Main {
static int[] nums = {1,3,3*3,9*3,27*3,81*3,243*3,243*9,243*27,243*81};
static int[] weight = {0,0,0,0,0,0,0,0,0,0};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()){
int N = in.nextInt();
if (search(0,9,0,N)){
print();
}
else if (search(0,10,0,N)){
print();
}
else if (search(0,11,0,N)){
print();
}
}
}
public static boolean search(int i,int t,int total,int n){
total+=nums[i]*t;
weight[i] = t;
int j = i+1;
boolean flag1,flag2,flag3;
if (total<n&&i!=9){
flag1=search(j,9,total,n);
if (flag1){
return true;
}
flag2=search(j,10,total,n);
if (flag2){
return true;
}
flag3=search(j,11,total,n);
if (flag3){
return true;
}
return false;
}
else if (i==9){
return total==n;
}
else if (total>=n&&i!=9){
return false;
}
return true;
}
public static void print(){
for (int i = 0; i < 10; i++) {
if (i==9){
System.out.print(weight[i]);
}
else{
System.out.print(weight[i]+" ");
}
}
System.out.println();
}
}