A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 聂广强 中级黑马   /  2013-7-30 16:18  /  2205 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看了之前的面试题一直认为回文数是一个老大难 但是昨天面试翻转字符串的时候突然发现 回文数也是可以这样简单进行判断
  1. class Program
  2.     {//判断输入的字符串是否为回文数

  3.         static void Main(string[] args)
  4.         {
  5.         label:

  6.             Console.WriteLine( "请输入字符串判断是否为回文数");
  7.             string strWorld = Console.ReadLine();

  8.             if (check(strWorld))
  9.             {
  10.                 Console.WriteLine("是回文数");
  11.                 goto label;
  12.             }
  13.             else
  14.             {
  15.                 Console.WriteLine("不是回文数");
  16.             }
  17.             Console.ReadKey();
  18.         }

  19.         private static bool check(string p)
  20.         {
  21.             bool boo=false;
  22.             char[] ch = p.ToCharArray();
  23.             for (int i = 0; i < ch.Length / 2; i++)
  24.             {
  25.                 if (ch[i] == ch[ch.Length - 1 - i])
  26.                 {
  27.                    boo=true;
  28.                 }
  29.                 else
  30.                 {
  31.                   boo=false;
  32.                 }
  33.             }
  34.             return boo;
  35.         }

  36.    
  37.     }
复制代码
请广大马友看看 是否还有其他的 判断回文数的方法

5 个回复

倒序浏览
本帖最后由 黑骏马 于 2013-7-30 18:04 编辑

我把你的代码修改了下,看这样是不是更方便,代码可以重用:
  1. namespace 判断回文数
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Console.WriteLine( "请输入字符串判断是否为回文数");
  8.             string strWorld = Console.ReadLine();

  9.             checkHuiwen(strWorld);
  10.            
  11.             Console.ReadKey();
  12.         }

  13.         public static void checkHuiwen(string p)
  14.         {
  15.             bool boo=false;
  16.             char[] ch = p.ToCharArray();
  17.             for (int i = 0; i < ch.Length / 2; i++)
  18.             {
  19.                 if (ch[i] == ch[ch.Length - 1 - i]) //字符串两头对比
  20.                 {
  21.                     boo=true;
  22.                 }
  23.                 else
  24.                 {
  25.                     boo=false;
  26.                 }
  27.             }
  28.             if (boo)
  29.             {
  30.                 Console.WriteLine(p+"是回文数");
  31.             }
  32.             else
  33.             {
  34.                 Console.WriteLine(p + "不是回文数");
  35.             }
  36.         }

  37.     }
  38. }
复制代码
回复 使用道具 举报
本帖最后由 黑骏马 于 2013-7-30 18:24 编辑

然后这是我的代码:
  1. namespace 判断回文数
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Console.WriteLine( "请输入字符串判断是否为回文数");
  8.             string strWorld = Console.ReadLine();
  9.                                               //调用方法判断是否为回文数    //如需重用代码,把相应语句换成注释里的就行了
  10.             checkHuiwen(strWorld);                                /bool boo = checkHuiwen(strWorld);            //int ch = checkHuiwen(strWorld);
  11.                                                                             //Console.WriteLine(boo);                            //Console.WriteLine(ch);
  12.             Console.ReadKey();
  13.         }

  14.         public static void checkHuiwen(string str)            //public static bool checkHuiwen(string str)    //public static int checkHuiwen(string str)
  15.         {
  16.             string _str = "";  //定义一个空字符串,用于存放翻转后的字符串
  17.             for (int i = str.Length - 1; i >= 0; i--)
  18.             {
  19.                 _str += str[i];  //原字符串倒序排列
  20.             }
  21.             if (_str == str)    //翻转后的字符串如果与原字符串相同则为回文数
  22.             {
  23.                 Console.WriteLine(str + "是回文数");            //return true;                   //return 1;
  24.             }
  25.             else
  26.             {
  27.                 Console.WriteLine(str + "不是回文数");        //return false;                   //return 0;
  28.             }
  29.         }   
  30.     }
  31. }
复制代码
回复 使用道具 举报
嗯,又学到了好多
回复 使用道具 举报
之前发的代码注释比较多,被编译器搞乱了,重新发一下:
  1. namespace 判断回文数
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Console.WriteLine( "请输入字符串判断是否为回文数");
  8.             string strWorld = Console.ReadLine();
  9.             checkHuiwen(strWorld);

  10.             Console.ReadKey();
  11.         }

  12.         public static void checkHuiwen(string str)
  13.         {
  14.             //定义一个空字符串,用于存放翻转后的字符串
  15.             string _str = "";

  16.             //原字符串倒序排列
  17.             for (int i = str.Length - 1; i >= 0; i--)
  18.             {
  19.                 _str += str[i];  
  20.             }

  21.             //翻转后的字符串如果与原字符串相同则为回文数
  22.             if (_str == str)   
  23.             {
  24.                 Console.WriteLine(str + "是回文数");
  25.             }
  26.             else
  27.             {
  28.                 Console.WriteLine(str + "不是回文数");
  29.             }
  30.         }
  31.     }
  32. }
复制代码
回复 使用道具 举报
学习 路过~~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马