这样应该更明白 int apple=1000; int boxNum=0; int leave=apple; do { apple=apple/2 + apple % 2; boxNum+=1; leave-=apple; System.out.println("box[" + boxNum + "]="+ apple); apple=leave;
}while(leave>0);
private static void calculate(int len, int total) { int[] x = new int[len + 1]; x[1] = 1; for(int i = 1; i <= len; i++) { if(i == len) x[i] = total; for(int j = i - 1; j >= 1; j--) { if(i == len) { x[i] -= x[j]; } else { x[i] += x[j]; } } if((i > 1) && (i < len)) x[i]++; } for(int i = 1; i <= len; i++) { System.out.println("x[" + i + "] = " + x[i]); } }
来个位运算的: int apple=1000; int boxNum=0; int i=0; do { i=apple & 1; apple>>=1; boxNum+=1; System.out.println("box[" + boxNum + "]=" + (apple + i)); }while(apple>0);
另一个金山试题,麻烦给解释一下:代码如下 class classA { public classA() { showself(); } public void showself() {System.out.println("I am A");}} public class classB extends classA{ public classB(){ showself(); } public void showself() {System.out.println("I am B");}//overriding public static void main(String[] args){ classB B=new classB(); } }
I am A I am B??????????????
class classA { public classA() { showself(); }
public void showself() { System.out.println("I am A"); }}public class classB extends classA{ public classB(){ System.out.println("111"); showself(); }
public void showself() { System.out.println("I am B"); System.out.println("222"); }
public static void main(String[] args){ classB B=new classB(); } }加上两行打印信息,就知道怎么解释了,自己去运行看看!
我当时答错了. 对java的动态绑定不理解啊。
先调用父类的构造函数,然后是自己的各数据成员的声明(按顺序),最后试自己的构造函数。 而父类的构造函数调用的函数showself()被重载了,故调用它是动态榜定到重载后的函数,然后试自己的构造函数。 所以结果是: I am B(父类调用子类的) I am B
waiting
{
nb=nb/2 + nb%2;
System.out.println(nb);
}while(nb!=0)
int nb=1000;
int i=0;
do
{
nb=nb/2 + nb%2;
i+=1;
System.out.println("[" + i + "]="+ nb);
}while(nb>1);
int nb=1000;
int i=0;
int t=0;
do
{
nb=nb/2 + nb%2;
i+=1;
t+=nb;
System.out.println("[" + i + "]="+ nb + " " + t);
nb=1000-t;
}while(nb>0);
int apple=1000;
int boxNum=0;
int leave=apple;
do
{
apple=apple/2 + apple % 2;
boxNum+=1;
leave-=apple;
System.out.println("box[" + boxNum + "]="+ apple);
apple=leave;
}while(leave>0);
int[] x = new int[len + 1];
x[1] = 1;
for(int i = 1; i <= len; i++) {
if(i == len) x[i] = total;
for(int j = i - 1; j >= 1; j--) {
if(i == len) {
x[i] -= x[j];
} else {
x[i] += x[j];
}
}
if((i > 1) && (i < len)) x[i]++;
}
for(int i = 1; i <= len; i++) {
System.out.println("x[" + i + "] = " + x[i]);
}
}
int apple=1000;
int boxNum=0;
int i=0;
do
{
i=apple & 1;
apple>>=1;
boxNum+=1;
System.out.println("box[" + boxNum + "]=" + (apple + i));
}while(apple>0);
1、已知2的n次方的阶层可以组合得到(2^(n+1)-1)的数
2、只要(2^(n+1)-1)大于给定总数的一半,我就可以使用这个阶层和余数组合得到任何的值所以程序只要求出((2^(n+1)-1)>=N/2)的最小的n即可,同时可以得到你给出的序列直接利用数学定理,无需再次程序证明,所以只需要求n的时候尽量少算,同时在应该在同一循环中得到结果序列
这题就是考的二进制及其移位问题。
不过上面 anis0444() 兄说的却是更好的思路,用上歌德巴赫猜想的理论(任何一个数都可以表示成两个质数之和)了,呵呵。学习。
class classA {
public classA() {
showself();
}
public void showself()
{System.out.println("I am A");}}
public class classB extends classA{
public classB(){
showself();
}
public void showself()
{System.out.println("I am B");}//overriding
public static void main(String[] args){
classB B=new classB(); }
}
I am B??????????????
public classA() {
showself();
}
public void showself() {
System.out.println("I am A");
}}public class classB extends classA{
public classB(){
System.out.println("111");
showself();
}
public void showself() {
System.out.println("I am B");
System.out.println("222");
}
public static void main(String[] args){
classB B=new classB();
}
}加上两行打印信息,就知道怎么解释了,自己去运行看看!
而父类的构造函数调用的函数showself()被重载了,故调用它是动态榜定到重载后的函数,然后试自己的构造函数。
所以结果是:
I am B(父类调用子类的)
I am B
{ for(j=1,s=1;s<=i;j=2*j,s=s+j)
{ /*output j*/
}
s=s-j;
i=i-s;
}
for(j=1,s=1;s<=N;j=2*j,s=s+j)
{ /*output j*/
}
s=s-j;
i=i-s;
/*output i*/
int apple=1000;
int temp;
int i=0;for(;(temp=(int)Math.pow(2,i))<apple;i++){
apple-=temp;
System.out.println("box["+i+"]:"+temp);
}
if(i>0){
System.out.println("box["+i+"]:"+apple);
}
答案就是1,2,4,8,16,32,64,128,256,489我没有用到移位。移位的算法应该是:
(1)移位求出apple的第一个非0位
(2)循环打出apple数
实际上做了两遍循环。请大家评价一下我的算法。