第1个回答 2008-09-11
int a=1234321;
String s=""+a;
int length=s.length();//得到 a的 位数
int tempR=0;
int tempL=0;
boolean isSame=true;
for(int i=0;i<length/2;i++){
tempR=a%(add(i+1));//右边第i+1位
tempL=a%(add(length-i-1));//左边第length-i位
if(tempR!=tempL) {
isSame=false;
break;
}
}
if(isSame){
回文数
}else{
不是回文数
}
//这个是 算要整除的 数是多少,也可以用Math.Pow(10,i);代替
public int add(int i){
int s=1;
for(int j=0;j<i;j++){
s*=10;
}
return s;
}
//。。受上面那位仁兄启发,想到一个更简单的方法:
int a=1234321;
String s=""+a;
int length=s.length();
boolean is=true;
for(int i=0;i<s.length/2;i++){
if(!s.charAt(i).equals(s.charAt(s.length-i-1))){
is=false;
break;
}
}
if(is) 是会文书
else 不是
第2个回答 2008-09-11
public class test {
public test() {
}
public int numberlen(String num) {
int ret = 0;
ret = num.length();
return ret;
}
public boolean isHW(String num) {
boolean hw=true;
if (num.length() % 2 == 0) {
System.out.println("偶数");
for(int i=0;i<=num.length()/2;i++){//123321
if(num.substring(i,i+1).equals(num.substring(num.length()-i-1,num.length()-i))){hw=true;}
else{hw=false;}
}
} else {
System.out.println("奇数");//1234321
for(int i=0;i<=(num.length()-1)/2;i++){
if(num.substring(i,i+1).equals(num.substring(num.length()-i-1,num.length()-i))){hw=true;}
else{hw=false;}
}
}
return hw;
}
public static void main(String[] args) {
test tes = new test();
System.out.println(tes.numberlen("1234567"));
System.out.println(tes.isHW("123321"));
System.out.println(tes.isHW("1234321"));
}
}
第3个回答 2008-09-11
我猜想主要是想考察我们对整除运算(/)和取模运算(%):
import java.util.ArrayList;
import java.util.List;
public class Zhidao67865598 {
/**
* @param args
*/
public static void main(String[] args) {
int input = 1234;
int q = input / 10;// 商,取整
int r = input % 10; // 余数,求模
int count = 1; // 位数
boolean isPalindrome = true; // 是否回文?
List<Integer> numbers = new ArrayList<Integer>();//保存每一位的数字,从底位到高位
numbers.add(r);
while (q > 0) {
count++;
r = q % 10;
numbers.add(r);
q = q / 10;
}
System.out.println("位数:" + count);
//比较是否回文
for (int i = 0; i < numbers.size() / 2; i++) {
if (numbers.get(i) != numbers.get(numbers.size() - 1 - i)) {//首尾互相比较
isPalindrome = false;
break;
}
}
System.out.println("是否回文:"+ isPalindrome);
}
}