输入一串数字,判断它是否为回文数··(回文数:12321)『第一位数字和最后以为数字相等,第二位数字和倒数第二位数字相等』

解决方案 »

  1.   

    说下思路:
    2个指针a, b,a开始指向字符串的第一个字符,b指向最后一个字符
    每一次,a的职位右移,b左移动,判断a,b指向的字符是否相等,直到a,b指针相遇
      

  2.   

    可以用java来实现它吗?~我不懂·c
      

  3.   


    public static void main(String[] args) {

    String str = "要判断的值";
    int strLen = str.length();
    int point = strLen;
    int oldLen = 0,newLen = 0;
    char first = 'a',end = 'a';

    oldLen = (strLen+1)/2;
    if (str == "")
    {
    System.out.println("String is null");
    return;
    }
    for (int i=0;i<oldLen;i++)
    {
    first = str.charAt(i);
    end = str.charAt(point-1);
    point--;
    if (first == end)
    {
    newLen++;

    }

    }
    if (oldLen == newLen)
    {
    System.out.println("String is huiwen");
    }else 
    {
    System.out.println("String is not huiwen");
    }

    }
    刚写的。是实现就好。。
      

  4.   


    int huiwen(int n){
    int m=0,s=0;
    while(m>0){
    s=s*10+m%10;
    m=m/10;
    }
    if(s==n)return (true);
    else return (false);
    }
      

  5.   


    public class Palindrome {
        public static void main(String[] args) {
          System.out.print(isPalindrome("123454321"));
        }
        
        public static boolean isPalindrome(String in){
          if(in == null || in.length() == 0){
            return true; //空字符串或长度为零的字符串,当作回文
          }
          
          for(int i=0; i<(in.length()+1)/2; i++){
            if(in.charAt(i) != in.charAt(in.length()-1-i)){
              return false;
            }
          }
          return true;
        }
    }
      

  6.   

    /**
     * 判断是否为回文数
     * 
     * @param n
     *            要判断的数
     * @return 是否为回文数
     */
    public boolean isHuiWenShu(int n) {
    String s = String.valueOf(n);
    int count = s.length() >> 1;
    for (int i = 0; i < count; i++)
    if (s.charAt(i) != s.charAt(s.length() - 1 - i))
    return false;
    return true;
    }
      

  7.   

    刚好以前做过,提供两种方法,
    方法一,比较头尾字符import java.util.*;public class PalindromeV1
    {
      public static void main(String[] args)
      {
       Scanner sc = new Scanner(System.in);
       System.out.println("输入一个字符串: ");
       String word = sc.next();
       int i = word.length();
       int j = 0;
       while (j <= (i / 2) -1 && word.charAt(j) == word.charAt(i - j - 1))
         j++;
       if (j == i / 2) 
        System.out.println("输入字符串是回文.");
       else 
        System.out.println("输入字符串不是回文.");
       }
    }
    方法之二,使用StringBuffer的reverse方法进行比较import java.util.*;public class PalindromeV2
    {
      public static void main(String[] args)
      {
       Scanner sc = new Scanner(System.in);
      
       System.out.println("输入一个字符串: ");
       String word = sc.next();
       
       if (word.equals(new StringBuffer(word).reverse().toString()))
          System.out.println("输入字符串是回文.");
       else
          System.out.println("输入字符串不是回文.");
       }
    }
      

  8.   

    package spciality;import java.util.Scanner;
    public class NumberSpeciality
    {
    private Scanner scanner;
    public NumberSpeciality()
    {
    this.isSpeciality();

    }

    public void isSpeciality()
    {
    scanner = new Scanner(System.in);
    System.out.print("Please Enter Number(the length is 5):");
    String str = scanner.next();
    if(str.charAt(0)==str.charAt(str.length()-1)&&str.charAt(1)==str.charAt(str.length()-2))
    {
    System.out.println(str+"是回文数!");
    }
    else
    {
    System.out.println(str+"不是回文数!");
    }

    }
    public static void main(String args[])
    {
    new NumberSpeciality();
    }
    }
      

  9.   

    很简单啊,每必要写取余,取整
    public class huiwen {
    public static void main(String[] args) {
    String s = new String(args[0]);//输入数
    StringBuffer ss= new StringBuffer(s);
    ss.reverse();//返转数
    String sb=ss.toString();//StringBuffer转换String
    if(s.equals(sb)){
    System.out.println("是个回文数");
    }else{
    System.out.println("不是回文数");
    }
    }}
      

  10.   

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;public class huiwen1
    {
      public static void main(String[] args) throws IOException
      {
      System.out.println("请输入:");
      String ss = new BufferedReader(new InputStreamReader(System.in)).readLine().toString();
      if(ss.equals(new StringBuffer(ss).reverse().toString()))
      System.out.println("是回文");
      else
      System.out.println("不是回文");
      }}
      

  11.   


    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;public class huiwen1
    {
      public static void main(String[] args) throws IOException
      {
      System.out.println("请输入:");
      String ss = new BufferedReader(new InputStreamReader(System.in)).readLine().toString();
      if(ss.equals(new StringBuffer(ss).reverse().toString()))
      System.out.println("是回文");
      else
      System.out.println("不是回文");
      }}
      

  12.   


    String ss = new BufferedReader(new InputStreamReader(System.in)).readLine().toString();
    if(ss.equals(new StringBuffer(ss).reverse().toString()))