高手帮我,谢谢啊!我是菜鸟!

解决方案 »

  1.   

    import java.io.*;
    class A{
    public static void main(String[] args)throws Exception{
    System.out.println("Please enter Number:");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s = in.readLine();
    int a = Integer.parseInt(s);
    in.close();
    if((a&(a-1))==0){
    System.out.println("Yes");
    }
    else{
    System.out.println("No");
    }
    }
    }
      

  2.   

    a&(a-1) 是什么?能不能解释一下?
    我觉得这样:1不用说了肯定是了。其他的数对2求余判断余数是多少,0就肯定是了,1当然就不是了。
      

  3.   

    public static void main(String[] args)throws Exception{
    System.out.println("Please enter Number:");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s = in.readLine();
    int a = Integer.parseInt(s);
    in.close();
                    out:
                    for(int i=0;;){
                        if(a%2==0){
                            int b;
                            b= a/2;
                            if(b==1){
                                System.out.println("yes");
                                break out;
                            }
                            a= b;
                        }else{
                            System.out.println("no");
                            break out;
                        }         
                    }
            // TODO code application logic here
        }
    ===================================================================
    俺也是菜鸟~借鉴前者~改的~只能用于数字不很巨大的求解~
    期待高手正解
      

  4.   

    a&(a-1)就是两者与楼主到底是要2的幂还是2的倍数?真搞不懂了
    如果是2的倍数的话直接%2 == 0判断一下就ok了
      

  5.   

    public static void main(String[] args)throws Exception{
    System.out.println("Please enter Number:");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s = in.readLine();
    int a = Integer.parseInt(s);
    in.close();
                    out:
                    for(int i=0;;){
                        if(a%2==0){
                            int b;
                            b= a/2;
                            if(b==1){
                                System.out.println("yes");
                                break out;
                            }
                            a= b;
                        }else{
                            System.out.println("no");
                            break out;
                        }         
                    }
            // TODO code application logic here
        }
    ------------------------------------
    这个程序的逻辑倒是没什么问题,只不过太不完善了,光0和1就判断不出来了,0是直接死循环
      

  6.   

    二进制的方法不就是你借鉴的那个程序吗,难道只看了个BufferedReader?
      

  7.   

    import java.io.*;public class Two {

    public static void main(String args[]){
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int it=-1;

    System.out.println("intput the integer number: ");
    try{
    it = Integer.parseInt(br.readLine());
    }catch(Exception e){
    e.printStackTrace();
    System.exit(0);
    }

    int ones=0;
    for(int i=0; i<31; i++){
    if(((1 << i) & it) != 0){
    ones++;
    if(ones>=2){
    System.out.println("false");
    //break;
    System.exit(0);
    }
    }
    }
    System.out.println("true");
    }
    }
      

  8.   

    interhanchi(路曼曼其修远兮,吾将上下而求索.)
    -------------------------
     精典,老鸟就是不一样啊!
      

  9.   

    2楼的令人佩服2的x次幂:可以写成  2的m次幂 * 2的n次幂(m+n=x).  但不能写成: 2的m次幂 + 2的n次幂(m+n=x). 
    所以,在用二进制表示的时候,只能是最高位为1,其余位为0. 任何出现两个1的二进制数都不是
    2的幂
    例如:10, 100, 1000, 10000, 100000--->是
         11, 110, 1100, 11000, 等就不是了
    所以 如果a为二的幂的话,a & (a-1)肯定等于零
      

  10.   

    那为什么48能整除2也能整除4,它不是2的幂呢?
    public boolean div(int a){
    do{n=a/2;
       t=a%2;
       if(t) return false;
      }while(n);
      return true;
    }