高手帮我,谢谢啊!我是菜鸟!
解决方案 »
- 什么把字符串转化成为字符串数组??
- 求字符串通配符匹配算法或程序
- JTextPanel 的问题
- 做一个内存监测的程序,遇到一个问题,请教高人
- 请问高手,在eclips里的查找和替换界面上输入什么字符,可以查找到.java文件中的tab键,并且用四个空个来代替它。
- 一个关于Calendar类的问题!
- 关于类本身方法调用的问题,急,在线等,解决给分
- String类的indexof , lastindexof, substring, regionMatches, concat 各有什么功能?
- 什么地方有ULTRAEDIT-32 8.20A+ 的注册吗呀?
- 求懂串口通信的的看下,很简单的
- 怎样获得本机出去访问网络的ip地址?
- 请高手帮帮忙!!谢谢
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");
}
}
}
我觉得这样:1不用说了肯定是了。其他的数对2求余判断余数是多少,0就肯定是了,1当然就不是了。
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
}
===================================================================
俺也是菜鸟~借鉴前者~改的~只能用于数字不很巨大的求解~
期待高手正解
如果是2的倍数的话直接%2 == 0判断一下就ok了
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是直接死循环
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");
}
}
-------------------------
精典,老鸟就是不一样啊!
所以,在用二进制表示的时候,只能是最高位为1,其余位为0. 任何出现两个1的二进制数都不是
2的幂
例如:10, 100, 1000, 10000, 100000--->是
11, 110, 1100, 11000, 等就不是了
所以 如果a为二的幂的话,a & (a-1)肯定等于零
public boolean div(int a){
do{n=a/2;
t=a%2;
if(t) return false;
}while(n);
return true;
}