今天牛牛学到了回文串,他想在数字里面找回文,即回文数,回文数是正着读与倒着读都一样的数,比如1221,343是回文数,433不是回文数。请输出不超过n的回文数。
#include <iostream> using namespace std; bool is(int n) { int x = n, res = 0; while (n) { int e = n % 10; res = res * 10 + e; n /= 10; } if (res == x) return true; return false; } int main() { int n; cin >> n; for (int i = 1; i <= n; i ++ ) { if (is(i)) cout << i << endl; } }
#include <stdio.h> #include <math.h> void judge(int num) { int count = 0; int sum = 0; int tmp = 0; int n = num;//避免影响num的值 int m = num;// while (n)//判断数字位数 { count++; n /= 10; } while (m) { tmp = m % 10; m /= 10; sum += tmp * pow(10, --count);//倒置数字 } if (num == sum)//判断 printf("%d\n", sum); } int main() { int n = 0; int i = 1; scanf("%d", &n); for (i = 1; i <= n; i++) { judge(i); } return 0; }
int main() { int n, i, ind; int j = 0; char arr[8] = { 0 }; scanf("%d", &n); for (i = 1; i <= n; i++) { int tmp = i; int mad = i; int count = 0; while (mad) { arr[count] = mad % 10; mad = mad / 10; count++; } if (1 == count) { printf("%d\n", tmp); } else { //奇数偶数都一样 ind = count / 2; for (j = 0; j < ind; j++) { if (arr[j] != arr[count - 1 - j]) break; } if (j == ind) printf("%d\n", tmp); } } return 0; }
int main() { int n, sum, b; scanf("%d", &n); for (int i = 1; i <= n; i++) { b = i; sum = 0; while (b) { sum = sum * 10 + b % 10; b /= 10; } if (sum == i) { printf("%d\n", sum); } } return 0; }
#include<stdio.h> int main(){ int n , i; scanf("%d",&n); for(i = 1 ; i <= n ; i++ ) { //一位数 中的全部回文数 if( i < 10 ) { printf("%d \n",i); continue; } //两位数 中的全部回文数 else if( i < 100 ) { int one = i % 10; int ten = i / 10; if( one == ten ) printf("%d \n",i); continue; } //三位数 中的全部回文数 else if( i < 1000 ) { int one = i % 10; int hun = i / 100; if( one == hun ) printf("%d \n",i); continue; } //四位数 中的全部回文数 else if( i < 10000 ) { int one = i % 10; int thou = i / 1000; int hun = ( i - thou * 1000 ) / 100; int ten = ( i - thou * 1000 - hun * 100 ) / 10; if( one == thou && ten == hun ) printf("%d \n",i); continue; } //五位数 中的全部回文数 else if( i < 100000 ) { int one = i % 10; int ten_thou = i / 10000; int thou = ( i - ten_thou * 10000 ) / 1000; int ten = i % 100 / 10; if( one == ten_thou && ten == thou ) printf("%d \n",i); continue; } } return 0; }
#include <stdio.h> void ispal(int num) { int a[6] = { 0 },i = 0,cnt = 0,flag = 1; int tem = num; while (tem > 0) { a[i] = tem%10; tem /= 10; i++; cnt++;//几位数 } for (int i = 0; i < cnt/2;i++) { if (a[i] != a[cnt-i-1]) flag = 0; } if (flag) printf("%d\n",num); } int main() { int n = 0; scanf("%d",&n); for (int i = 1;i <= n;i++) ispal(i); return 0; }
#include <stdio.h> int main() { int n = 0;//输入的整数 int m = 0;//每次判断的数 int j = 0;//倒着读判断的数 scanf("%d", &n); for (int i = 1; i <= n; i++) { m = i; while (m)//计算倒着读 { j *= 10; j += m % 10; m = m / 10; } if (i == j)//判断并输出 printf("%d\n", j); j = 0;//初始化数据 } return 0; }
#include <stdio.h> int main() { int a[30]; int n = 0; scanf("%d", &n); //回文数相当于换位置,后面的数一直*10,得到得结果与原来的一样 for (int i = 1; i<=n; i++) { //i的值还要和sum比较 int tmp = i; //sum的值每次也需要变成0,不然初值就不为0 int sum = 0; while (tmp) { sum = sum*10+ tmp % 10; tmp /= 10; } if(sum == i) printf("%d\n",sum); } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=1;i<n;i++){ if(isPalindrome(i)) System.out.println(i); } } public static boolean isPalindrome(int x){ boolean is=true; String[] arr=String.valueOf(x).split(""); for(int i=0;i<Math.ceil(arr.length/2);i++){ if(!arr[i].equals(arr[arr.length-i-1])) is=false; } return is; } }
#include <stdio.h> int main (void) { long int n; scanf("%ld", &n); //要求位数比较,需分范围 if (n < 10) //个位数 { for (int i = 1; i < n; i++) printf("%d\n", i); } if (10 <= n && n < 100) //两位数 { for (int i = 1; i < 10; i++) printf("%d\n", i); for (int i = 10; i <= n; i++) { if (i / 10 == i % 10) printf("%d\n", i); } } else if (100 <= n && n < 1000) //三位数 { for (int i = 1; i < 10; i++) printf("%d\n", i); for (int i = 10; i < 100; i++) { if (i / 10 == i % 10) printf("%d\n", i); } for (int i = 100; i <= n; i++) { if (i / 100 == i % 100 ) printf("%d\n", i); } } else if (n <= 1000 && n < 10000) //四位数 { for (int i = 1; i < 10; i++) printf("%d\n", i); for (int i = 10; i < 100; i++) { if (i / 10 == i % 10) printf("%d\n", i); } for (int i = 100; i < 1000; i++) { if (i / 100 == i % 100) printf("%d\n", i); } for (int i = 1000; i <= n; i++) { if (i / 1000 == i % 1000 && i / 100 % 10 == i % 100 / 10 && i / 10 % 10 == i / 100 % 10) printf("%d\n", i); } } else if (n <= 10000 && n < 100000) //五位数 { for (int i = 1; i < 10; i++) printf("%d\n", i); for (int i = 10; i < 100; i++) { if (i / 10 == i % 10) printf("%d\n", i); } for (int i = 100; i < 1000; i++) { if (i / 100 == i % 100) printf("%d\n", i); } for (int i = 1000; i < 10000; i++) { if (i / 1000 == i % 1000 && i / 100 % 10 == i % 100 / 10 && i / 10 % 10 == i / 100 % 10) printf("%d\n", i); } for (int i = 10000; i <= n; i++) { if (i / 10000 == i % 10000 && i / 1000 % 10 == i % 1000 / 10 && i / 10 % 10 == i % 1000 / 10) printf("%d\n", i); } } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int toJudgePalindrome = scanner.nextInt(); for(int i = 1;i <= toJudgePalindrome ;i++){ String str = String.valueOf(i); StringBuffer strBuff = new StringBuffer(str); if(str.equals(strBuff.reverse().toString())){ System.out.println(i); } } } }
n = int(input()) for i in range(1,n+1): if str(i) == str(i)[::-1]: print (i) else: x = 1 #没什么用 就是为了不影响循环哈哈
#include <stdio.h> //主要思路,将数字逐个%,将数字存到数组里,将数组首尾数字逐个比较 int main() { int n, num; scanf("%d", &n); int array[100] = {0}; int i, j; for (num = 1; num <= n; num++) { int temp = num; i = 0; while (temp > 0) //取数字到数组里 { array[i] = temp % 10; i++; temp = temp / 10; } for (j = 0; j <= (i - 1) / 2; j++) //数组数字首尾逐个比较 { if (array[j] != array[i - j - 1]) { break; } } if (j > (i - 1) / 2) //没有发现不相等的情况 { printf("%d\n", num); } } return 0; }
#include<math.h> int main() { int n = 0; scanf("%d", &n); int i = 0; for (i = 1; i <= n; i++) { int k = i; int a = 0; while (k) { k = k / 10; a++; } k = i; int sum = 0; while (k) { sum+=k%10*pow(10,a-1); k = k / 10; a--; } if (sum==i) { printf("%d\n",i); } } return 0; }