时间限制: 1Sec 内存限制: 128MB
题目描述:(原题链接)
C++中int型整数内码是一个32位的01序列,该01序列有些是对称的,有些是不对称的。对于给定的一个整数,在一些场合,需要判断其整数内码的对称性。这个事情要做好还非你不行呢。
输入:
输入中含有一些整数n(0≤n〈2^32)。
样例输入:
0
1
2147483649
2
3
输出:
统计其中一共有多少个其内码对称的整数,输出之。
样例输出 :
2
解题思路:
字符串与2进制
注意事项:
内码是用32位的表示
参考代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int cnt=0;
while(in.hasNext()) {
BigInteger n =in.nextBigInteger();
String tmp =n.toString(2); //转化为2进制串
int len=tmp.length();
if(tmp.length()<32) {
for(int i=1;i<=32-len;i++)
tmp="0"+tmp; //转化为32位内码
}
String rev =new StringBuffer(tmp).reverse().toString();
if(tmp.equals(rev))
cnt++;
}
System.out.println(cnt);
in.close();
}
}