1. 程式人生 > >lqb 基礎練習 回文數

lqb 基礎練習 回文數

PE inf ron 2.0 col 一個 map 時間限制 main

基礎練習 回文數

時間限制:1.0s 內存限制:512.0MB 問題描述   1221是一個非常特殊的數,它從左邊讀和從右邊讀是一樣的,編程求所有這樣的四位十進制數。 輸出格式   按從小到大的順序輸出滿足條件的四位十進制數。 分析:   回文數的判斷只用判斷到數字的中間位數,即可。 取一個數的不同位數的值的方法:
1 while (temp)
2 {
3     A[j ++] = temp % 10;
4     temp /= 10;
5 }

C/C++代碼實現(AC):

 1 #include <iostream>
 2 #include <algorithm>
 3
#include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 #include <stack> 7 #include <map> 8 #include <queue> 9 10 using namespace std; 11 12 inline void solve() 13 { 14 int temp1[6]; 15 for (int i = 1000; i <= 9999; ++ i) 16 { 17 int
j = 0, temp = i; 18 while(temp) 19 { 20 temp1[j ++] = temp % 10; 21 temp /= 10; 22 } 23 bool flag = true; 24 for (int k = 0; k <= j / 2; ++ k) 25 { 26 if (temp1[k] != temp1[j - 1 - k]) 27 { 28 flag = false
; 29 break; 30 } 31 } 32 if (flag) printf("%d\n", i); 33 } 34 return ; 35 } 36 37 int main() 38 { 39 // 調用solve(),判斷是否滿足條件
40 solve(); 41 return 0; 42 }

lqb 基礎練習 回文數