首页 > 试题广场 >

判断一个数是否是回文数

[编程题]判断一个数是否是回文数
  • 热度指数:1716 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
定义回文数的概念如下:
  • 如果一个非负数左右完全对应,则该数是回文数,例如:121,22等。
  • 如果一个负数的绝对值左右完全对应,也是回文数,例如:-121,-22等。
给定一个32位整数num,判断num是否是回文数。
[要求]


输入描述:
输入一个整数N.


输出描述:
若N是回文整数输出"Yes",否则输出"No"
示例1

输入

121

输出

Yes
示例2

输入

-121

输出

Yes
示例3

输入

998244353

输出

No

备注:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s=to_string(n);
    if(s[0]=='-')
        s=s.substr(1,s.size()-1);
    string t=s;
    reverse(t.begin(),t.end());
    if(s==t)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

发表于 2019-09-06 22:00:15 回复(0)
先是python流氓解法
num = input().strip('-')
print("Yes" if num == num[::-1] else "No")
然后依照题目要求来用O(log10n)的算法来求解,主要思路是对输入数字不断除以10取余来计算其翻转的数字,然后比较二者是否相等
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int num = Math.abs(Integer.parseInt(br.readLine()));
        if(num == reverse(num))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
    
    private static int reverse(int num){
        int reverseNum = 0;
        while(num > 0){
            reverseNum = reverseNum * 10 + num % 10;
            num /= 10;
        }
        return reverseNum;
    }
}

发表于 2021-05-28 10:41:59 回复(0)
#include <bits/stdc++.h>
using namespace std;

void F(string s){
    int n = s.length();
    for(int i=0,j=n-1;i<j;i++,j--)
        if(s[i]!=s[j]){
            cout<<"No"<<endl;
            return;
        }
    cout<<"Yes"<<endl;
    return ;
}

int main(){
    string s;
    cin>>s;
    if(s[0]=='-')
        F(s.substr(1));
    else
        F(s);
    return 0;
}

发表于 2020-03-19 09:56:30 回复(0)
#include <stdio.h>
#
include<stdlib.h>
int main(){
    int num,s,y=0;
    scanf("%d", &num);
    s=abs(num);
    while(s>0){
        y=y*10+s%10;
        s=s/10;
    }
    if(y==abs(num))
        printf("Yes");
    else
     printf("No");
    return 0;
}
发表于 2020-03-06 21:40:53 回复(0)
#include<iostream>
 
#include<algorithm>
using namespace std;
void hw(int n){
    int s=abs(n),y;
   while(s>0){
       y=y*10+s%10;
       s/=10;
   }
    if(y==abs(n)){
        cout<<"Yes";
    }else{
        cout<<"No";
    }
}
int main()
{
   int n;
    cin>>n;
    hw(n);
    return 0;
}

发表于 2022-01-23 00:08:14 回复(0)
今天好热
let line=readline()
let stack=[]
if(line[0]=='-')line=line.slice(1)
for(let i=0;i<line.length;i++){
    stack.push(line[i])
}
let flag=0
for(let i=0;i<line.length;i++){
    if(line[i]!=stack.pop())  flag=1
    if(flag==1) break;
}
if(flag==1) console.log("No")
else console.log('Yes')


发表于 2021-07-03 13:02:12 回复(0)
s=input()
#判断是否是回文数
def judge(s):
    length=len(s)
    for i in range(length//2):
        if s[i]!=s[length-i-1]:
            return False
    return True
#是负数
if s[0]=='-':
    if judge(s[1:]):
        print('Yes')
    else:
        print('No')
else:
    if judge(s):
        print('Yes')
    else:
        print('No')

发表于 2021-06-17 19:53:19 回复(0)
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
bool judge(int n){
    int help=1;
    while(n/help>=10){
        help*=10;
    }
    while(n){
        if(n/help!=n%10)
            return false;
        n=(n%help)/10;
        help/=100;
    }
    return true;
}
int main(){
    int n;
    cin>>n;
    if(n<0){
        n=abs(n);
    }
    if(judge(n)){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
    
    
    return 0;
}
参考了大佬的书籍的思想,然后写出的代码,,,
发表于 2020-01-07 15:36:45 回复(0)
JavaScript实现方法:
var N = readline();          //或者输入的值
var n = Math.abs(N).toString();    //整数----->String字符
var len = n.length-1;
var flag = true;           //是否为回文标记
for(var i=0;i<n.length/2;i++){     //字符串折半,并进行对比
    if(n.charAt(i)!=n.charAt(len-i)){
        flag= false;
        break;
    }
}
if(flag)
    print("Yes");
else
    print("No");


发表于 2019-10-14 16:03:09 回复(0)
import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String s = isHuiwen(n);
        System.out.print(s);
    }
    public static String isHuiwen(int n ){
        if(n==Integer.MIN_VALUE) return "No";
        
        n = Math.abs(n);
        int help = 1;
        while(n/help>=10){
            help *=10;
        }
        while(n!=0){
            if(n/help != n%10){
                return "No";
            }
            n = (n%help)/10;
            help /=100;
        }
        return "Yes";
    }
}

发表于 2019-08-27 22:54:21 回复(1)
#include<iostream>
#include<string>
#include<limits.h>//定义最小整型INT_MIN
using namespace std;
int main() {
	int N;
	cin >> N;
	if (N == INT_MIN) { return false; }
	if (N < 0) {
		N = abs(N);
	}
	string str = to_string(N);
	int i = 0, j = str.size() - 1;
	bool res = true;
	while (i <= j) {
		if (str[i] != str[j]) {
			res = false;
			break;
		}
		i++;
		j--;
	}
	if (res) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}

发表于 2019-08-20 20:34:39 回复(0)