public class test { private int m = 1000;//苹果个数 private int n = 10;//箱子个数 private int[] result= new int[n]; public static void main(String[] args) { new test().start(); } public void start(){ result[0] = 1; System.out.print("begin....."); for(int b=1;b<n;b++){ for(int i=0;i<b;i++){ result[b]=result[b]+result[i]; } result[b]=result[b]+1; if((2*result[b]+1)>m){ result[b]=m-result[b]+1; }else{
我们一起创业,我们一起发财,我们一起快乐,我们一起努力!
10个箱子分别放2^n(0<=n<=9),能够完全满足条件
private int m = 1000;//苹果个数
private int n = 10;//箱子个数
private int[] result= new int[n];
public static void main(String[] args) {
new test().start();
}
public void start(){
result[0] = 1;
System.out.print("begin.....");
for(int b=1;b<n;b++){
for(int i=0;i<b;i++){
result[b]=result[b]+result[i];
}
result[b]=result[b]+1;
if((2*result[b]+1)>m){
result[b]=m-result[b]+1;
}else{
}
}
for(int b=0;b<n;b++){
System.out.print(result[b]);
}
}
}
的答案,我顶
其实这个就像我们在定义C/C++中的移位宏一样
#define a 0x1
#dedine b 0x2
#define c 0x4
....那么你要什么数目,你就可以 * | * |...这么来了
study
1 2 4 8 16 32 64 128 256 488
//////////////////////////////////////////////
你有什么权力笑话别人,看看你给的答案。是1000吗? 1+488 =499 剩下的都是偶数,那个苹果你吃了?
private int []appleBox;
public void putAppleToBox(int appleCount, int boxCount) {
appleBox = new int[boxCount];
for(int i = 0; i < boxCount; i++) {
int temp = (int)Math.pow(2, i);
if (appleCount >= temp) {
appleBox[i] = temp;
appleCount -= temp;
} else {
appleBox[i] = appleCount;
}
}
}
public String getApple(int appleCount){
StringBuffer boxs = new StringBuffer();
for (int i = 9; i >= 0; i--) {
if (appleCount >= appleBox[i]) {
appleCount -= appleBox[i];
boxs.append(i+1).append(" = ").append(appleBox[i]).append("\n");
}
}
return boxs.toString();
}
public static void main(String[] args) {
Test t = new Test();
t.putAppleToBox(1000, 10);
String box = t.getApple(902);
System.out.println(box);
}
}