列表

详情


JAVA48. 回文数判断

描述

请补全预设代码中判断回文数的方法,若输入的五位数字是回文是则返回true,否则返回false。回文数即个位与万位相同,十位与千位相同的数字。如:12321、89898

输入描述

控制台输入的五位数

输出描述

true/false

示例1

输入:

12321

输出:

true

示例2

输入:

18182

输出:

false

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 3ms, 内存消耗: 324KB, 提交时间: 2021-10-18

#include<iostream>
using namespace std;
const int N =1e6+10;

bool find(string s)
{
    return equal(s.begin(),s.end(),s.rbegin());
}
void solve()
{
    string s;
    cin >> s;
    if(find(s)) cout << "true";
    else cout << "false";
    return;
}
int main(void)
{
    solve();
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 436KB, 提交时间: 2022-02-25

#include <stdio.h>
int main()
{
    int n;
    int m = 0;
    scanf("%d", &n);
    for(int i = n;i>0;i/=10){
        m = m*10+i%10;
    }
    if (m == n)
        printf("true");
    else
        printf("false");
}

Java 解法, 执行用时: 27ms, 内存消耗: 10544KB, 提交时间: 2022-02-08

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Main main = new Main();
        int number = console.nextInt();
        System.out.println(main.palindromeNumber(number));
    }

    public Boolean palindromeNumber(int number) {

        //write your code here......
        int a1 = number/10000;
        int a2 = number%10000/1000;
        int a3 = number%1000/100;
        int a4 = number%100/10;
        int a5 = number%10;
        if(a1==a5&&a2==a4){
            return true;
        }
        return false;
    }
}

Java 解法, 执行用时: 27ms, 内存消耗: 10588KB, 提交时间: 2022-02-08

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Main main = new Main();
        int number = console.nextInt();
        System.out.println(main.palindromeNumber(number));
    }

    public Boolean palindromeNumber(int number) {

        //write your code here......
        String strNum=number+"";
        int len=strNum.length();
        boolean flag=false;
        for(int i=0;i<=(len/2);i++){
            if(strNum.charAt(i)==(strNum.charAt(len-1-i)))
                flag=true;
            else
                break;
        }
        return flag;
    }
}

Java 解法, 执行用时: 28ms, 内存消耗: 10680KB, 提交时间: 2021-11-29

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Main main = new Main();
        int number = console.nextInt();
        System.out.println(main.palindromeNumber(number));
    }

    public Boolean palindromeNumber(int number) {

        //write your code here......
        StringBuilder str = new StringBuilder(number + "").reverse();
        if(str.toString().equals(number + "")){
            return true;
        }
        return false;
        
    }
}

上一题