定义回文数的概念如下:
- 如果一个非负数左右完全对应,则该数是回文数,例如:121,22等。
- 如果一个负数的绝对值左右完全对应,也是回文数,例如:-121,-22等。
[要求]
输入一个整数N.
若N是回文整数输出"Yes",否则输出"No"
121
Yes
-121
Yes
998244353
No
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; } }
#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; }
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')
#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; }参考了大佬的书籍的思想,然后写出的代码,,,
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");
#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; }