Code For NonGeek
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Judege whether a number's binary expression is palidrom

2 posters

Go down

Judege whether a number's binary expression is palidrom Empty Judege whether a number's binary expression is palidrom

Post  Admin Mon Aug 29, 2011 10:11 pm

public static boolean judgepalidrom(int num){
char[] ch=new char[Integer.size];
for(int i=0;i<ch.length();i++){
char[Integer.size-i-1]=(char)((num>>i)&1+'0');
}
String str=new String(ch);
for(int j=0;j<str.length();j++){
if(str[j]!=str[str.length()-j-1])
return false;
}
return true;
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

https://codefornongeek.forumotion.com

Back to top Go down

Judege whether a number's binary expression is palidrom Empty Re: Judege whether a number's binary expression is palidrom

Post  Admin Tue Aug 30, 2011 10:35 am

boolean isPalindrome(int n) {
for (int i=0; i<16; i++) {
if ((n>>i) != (n>>(31-i))) return false;
}
return true;
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

https://codefornongeek.forumotion.com

Back to top Go down

Judege whether a number's binary expression is palidrom Empty Re: Judege whether a number's binary expression is palidrom

Post  viterbi Thu Sep 15, 2011 11:32 pm

This is not correct. You probably misunderstood the meaning of >>

Admin wrote:boolean isPalindrome(int n) {
for (int i=0; i<16; i++) {
if ((n>>i) != (n>>(31-i))) return false;
}
return true;
}

viterbi

Posts : 32
Join date : 2011-09-03

Back to top Go down

Judege whether a number's binary expression is palidrom Empty Re: Judege whether a number's binary expression is palidrom

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum