多种编程语言判断“回文数”的经典算法

版权声明:本文如果标明博主原创文章,若未经博主允许,则不得转载;若经博主允许,则转载或者引用本文内容请注明来源及原作者。欢迎知识经验进行交流和传播!文章来源: https://blog.csdn.net/Bee_AI/article/details/83870413

多种编程语言判断“回文数”的经典算法

今天做了一个有趣的事,用C、C++、Java、Python等多种编程语言实现了是否是“回文数”的判断。

首先,我们熟悉下什么是回文数?设N是一个任意自然数,若将n的各位数字反向排列所得自然数N1与N相等,则N称为一个回文数(Palindrome Number)。通俗的讲:顺读、倒读都相等的正整数就是回文数,只有正整数才存在回文数,小数不存在回文数。比如:6789876、678876等类似的数。

1 C语言编程

/*==========================================================
程序功能 : C程序实现回文数的判断
程序思路 : 将正整数Num翻转, 利用求余%和除/的思想
开发时间 : 2018年11月08日  11:41
==========================================================*/
#include <stdio.h>

int main(void){
	int Num, Num1, temp;
	printf("Please enter the positive integer to be judged : \n");
	while( scanf("%d", &Num) != EOF ){
		Num1 = 0;
		temp = Num;
		do{
			Num1 = Num1*10 + Num%10;	//将数字翻转,首先取最后一位
			Num /= 10;						//取一位就减少一位
		}while( Num > 0 );

		if(temp == Num1)
			printf("%d is Palindrome Number ! \n\n", temp);
		else
			printf("%d is not Palindrome Number ! \n\n", temp);
	}
	return 0;
}

2 C++语言编程

/*==========================================================
程序功能 : C++程序实现回文数的判断
程序思路 : for循环连续判断对称数字
开发时间 : 2018年11月08日  12:54
==========================================================*/
#include <iostream>
bool palindrome(char *num);
using namespace std;

int main(){
	char Num[1000];
	cout << "Please enter the positive integer to be judged : " << endl;
	cin >> Num;

	if(palindrome(Num))
		cout << "This is Palindrome Number !" << endl;
	else
		cout << "This is not Palindrome Number !" << endl;
	return 0;
}

bool palindrome(char *num){
	int lenth = strlen(num);
	for(int i = 0; i < lenth/2; i++){
		if(num[i] != num[lenth-i-1]){
			return false;
		}
	return true;
	}
}

3 Java语言编程

/*================================================
程序功能 : Java实现回文数的判断
程序思路 : 进行逐位判断。首先该整数的首位数字和末尾数字进行比较,判断是否相等,若不等,则不是回文数;若相等,则删除左右两位数,再取首位数字和未尾数字进行比较,只等比较结束为止。若相等,则是回文数;反之则不是。
开发日期 : 2018年11月08  19:37
================================================*/
improt java.util.Scanner;

public class PalindromeNumber{
	public static void main(String args[]){
		System.out.println(“ Please enter the positive integer to be judged : “);
		Scanner input = new Scanner( System.in);
		int Num = input.nextInt(); 
		System.out.println( palindrome( Num ) );
    }

    public static boolean palindrome(int num){
		if(num < 0){
 			System.out.println(num + " is not Palindrome Number !"); 
			return false;
		}
		int lenth = 1;
		while(num / lenth >= 10){ 		//判断位数
		lenth *= 10;
		}
		while(num > 0){
			int right = num % 10; 		//取数的尾数
			int left = num / lenth;		//取数的首位
			if(left != right){
				System.out.println(num + " is not Palindrome Number !");
				return false;
			}
			num = num % lenth / 10;		//除掉首尾两位数字,剩余中间的
			lenth = lenth / 100;  		//相应长度也减少2位
 		}
		System.out.println(num + " is Palindrome Number !");
		return true;
	}
}

4 Python语言编程

'''
程序功能 : Python实现回文数的判断
程序思路 : 利用for循环中嵌套if条件语句
开发时间 : 2018年11月08日  21:23
'''
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

Num = str(input("Please enter the positive integer to be judged : "))	#转为字符串
lenth = len(Num)	
flag = True
for n in range(0, lenth//2):				# //向下取整
      if Num[n] != Num[ lenth - n - 1 ]:	#对称数字上判断是否相等
         flag = False
         break
if flag:
	print "This is Palindrome Number !"
else:
	print "This is not Palindrome Number !"

  • 致谢
    若对大家有用,感谢点赞或评论;若有不足或补充之处,也感谢大家评论进行指正或完善。相信这是互相进步的开始!

猜你喜欢

转载自blog.csdn.net/Bee_AI/article/details/83870413
今日推荐