Java和C++实现回文

public class plaindrome {
	static int r, y = 0;
	static int key;
	public static void main(String[] args) {
		System.out.println("请输入一个整数:");
		Scanner in = new Scanner(System.in);
		int integer = in.nextInt();
		huiW(integer);
	}
		public static void huiW(int number){


		key = number;
		while(number != 0)
		{
			r = number % 10;
			number = number / 10;
			y = y * 10 + r;
		}
		if(y == key)
			System.out.println(key + " 是回文^");
		else
			System.out.println(key + " 不是回文!");
	}

}

#include<iostream>
using namespace std;
void main()
{
int i,x,y,r;
y=0;
cout<<"请输入数  "<<endl;cin>>x;
i=x;
while(i!=0)
{
r=i%10;
i=i/10;
y=y*10+r;
}
if(y==x)
cout<<x<<"  是回文数"<<endl;
if(y!=x)
cout<<x<<"  不是回文数"<<endl;
}

另外一种方法实现

package com.swjtu.other.pric;

import java.util.Scanner;

public class checkPalindrome {
	public static void main(String[] args) {
		System.out.println("请输入字符串...");
		Scanner in = new Scanner(System.in);
		String charz = in.next();
		if(isPalindrom(charz) == true)
			System.out.println(charz + " 是回文!!");
		else
		System.out.println(charz + " 不是回文……");
		
	}
	public static boolean isPalindrom(String chars){
		int low = 0;
		int hight = chars.length() - 1;
		while(low < hight){
			if(chars.charAt(low) != chars.charAt(hight))
				return false;
			low++;
			hight--;
		}
		return true;
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值