zoukankan      html  css  js  c++  java
  • python回文数

    # 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。
    
    #方法一
    def palindrome1(strnum):
        strnum=str(strnum)
        strlen=len(strnum)
    
        for i in range(0,int(2/strlen)+1):
            print(strnum[i])
            print(strnum[strlen-1])
            print(strnum[int(2/strlen)+1])
            if strnum[i]==strnum[strlen-1]:
                print("是回文数")
            else:
                print("不是回文数")
    #方法二
    def palindrome2(strnum):
        if  str(strnum)==''.join(list(reversed(str(strnum)))):
            print("是回文数")
        else:
            print("不是回文数")
    
    #方法三
    def palindrome3(strnum):
        strnum=str(strnum)
        if  strnum==strnum[::-1]:
            print("是回文数")
        else:
            print("不是回文数")
    
    #方法四 只适合数字
    def palindrome4(strnum):
        if strnum<0:
          return False
        temp_x = strnum;
        palindromeNum = 0
        while temp_x != 0:
          palindromeNum = palindromeNum*10 + temp_x%10
          temp_x =int(temp_x/10)
        if palindromeNum == strnum:
            print("是回文数")
        else:
            print("不是回文数")
    
    
    #方法五 整数转字符串,反转字符串,对比反转后字符串与原字符串是否相等
    def palindrome5(strnum):
        str_x = str(strnum)
        str_y = ""
        for i in str_x:
            print(i)
            str_y = i + str_y
        if str_y==str_x:
            print(str_y,"是回文数")
        else:
            print(str_y, "不是回文数")
    

      

    上班求生存,下班求发展
  • 相关阅读:
    python运算符
    CocoChina开发者大会
    iphone模拟器上不能安装从itunes下载的app
    Objective-C学习--源文件结构
    Objective-C学习笔记--复合
    Objective-C学习笔记--继承
    Objective-C学习笔记--实例化对象
    Objective-C学习笔记--@implementation
    Objective-C学习笔记--@interface
    C语言函数原型
  • 原文地址:https://www.cnblogs.com/ljf520hj/p/15187195.html
Copyright © 2011-2022 走看看