UVALive 2889(回文数字)

题意:给定i,输出第i个回文数字。

分析:1,2,3,4,……,9------------------------------------------------------------------------------------------9个

   11,12,13,14,……,19-------------------------------------------------------------------------------------9个

   101,111,121,131,141,151,161,171,181,191,202,212,222,232,……,979,989,999-------------------------------90个

   1001,1111,1221,1331,1441,……,9889,9999----------------------------------------------------------------90个

   10001,10101,10201,10301,……,99899,99999--------------------------------------------------------------900个

   规律是把回文串一分为二看,例如第四行,前两个数字是从10到99,共90个数字。而第三行也是从10到99,区别在于,需要去掉最后一位再反转,才是另一半(后两个数字)。

思路:先确定i所对应的回文数字的位数,再确定具体值。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cctype>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<iostream>
 7 #include<sstream>
 8 #include<iterator>
 9 #include<algorithm>
10 #include<string>
11 #include<vector>
12 #include<set>
13 #include<map>
14 #include<deque>
15 #include<queue>
16 #include<stack>
17 #include<list>
18 #define fin freopen("in.txt", "r", stdin)
19 #define fout freopen("out.txt", "w", stdout)
20 #define pr(x) cout << #x << " : " << x << "   "
21 #define prln(x) cout << #x << " : " << x << endl
22 typedef long long ll;
23 typedef unsigned long long llu;
24 const int INT_INF = 0x3f3f3f3f;
25 const int INT_M_INF = 0x7f7f7f7f;
26 const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
27 const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
28 const double pi = acos(-1.0);
29 const double EPS = 1e-6;
30 const int dx[] = {0, 0, -1, 1};
31 const int dy[] = {-1, 1, 0, 0};
32 const ll MOD = 1e9 + 7;
33 const int MAXN = 1000000 + 10;
34 const int MAXT = 10000 + 10;
35 using namespace std;
36 ll a[25];
37 void init()
38 {
39     ll tmp = ll(9);
40     for(int i = 1; i < 20; i += 2)
41     {
42         a[i] = a[i - 1] = tmp;
43         tmp *= ll(10);
44     }
45 }
46 ll POW(ll x)
47 {
48     ll w = 1;
49     for(ll i = 1; i <= x; ++i)
50         w *= ll(10);
51     return w;
52 }
53 int main()
54 {
55     init();
56     int n;
57     while(scanf("%d", &n) == 1 && n)
58     {
59         int cnt = 0;
60         while(n > a[cnt])
61         {
62             n -= a[cnt];
63             ++cnt;
64         }
65         if(cnt == 0)
66         {
67             printf("%d\n", n);
68             continue;
69         }
70         else if(cnt == 1)
71         {
72             printf("%d%d\n", n, n);
73             continue;
74         }
75         else
76         {
77             int tmp = cnt / 2;
78             char str[50];
79             memset(str, 0, sizeof str);
80             ll ans = POW(ll(cnt / 2)) + ll(n - 1);
81             sprintf(str, "%lld", ans);//把数字变为指定格式的字符串
82             printf("%s", str);
83             string s = string(str);
84             int len = s.size();
85             if(cnt % 2 == 0) s.resize(len - 1);
86             reverse(s.begin(), s.end());
87             printf("%s\n", s.c_str());
88         }
89     }
90     return 0;
91 }

   

时间: 2024-12-15 23:32:45

UVALive 2889(回文数字)的相关文章

LeetCode 9 Palindrome Number(回文数字判断)

Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的位数.按照已知数字x对10进行多次求余运算,可以得到数字位数. 具体思路: 1.每次取出该数字的最高位和最低位进行比较. 2.如果不相等则直接返回FALSE, 3.如果相等修改x的值(去掉最高位也同时去掉最低位)其中去掉最高位可以通过求模运算,去掉最低位可以采用除以10 4.进行循环直到x的值不大于0为止

1503140110-蓝桥杯-历届试题 回文数字

历届试题 回文数字 时间限制:1.0s   内存限制:256.0MB 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找到一些5位或6位的十进制数字.满足如下要求: 该数字的各个数位之和等于输入的整数. 输入格式 一个正整数 n (10<n<100), 表示要求满足的数位和. 输出格式 若干行,每行包含一个满足要求的5位或6位整数. 数字按从小到大的顺序排列. 如果没有满足条件的,输出:-1 样例

回文数字判断

/** * 回文数字判断 * * @param num * @since 0.0.1 */ public void huiwen(String num){ Integer temp = (num.length())/2 ; System.out.println(temp); boolean flag = true; for (int i = 0; i < num.length(); i++) { if (temp > 0) { if (i >= temp) { break; } } Sy

算法笔记_181:历届试题 回文数字(Java)

目录 1 问题描述 2 解决方案   1 问题描述 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找到一些5位或6位的十进制数字.满足如下要求: 该数字的各个数位之和等于输入的整数. 输入格式 一个正整数 n (10<n<100), 表示要求满足的数位和. 输出格式 若干行,每行包含一个满足要求的5位或6位整数. 数字按从小到大的顺序排列. 如果没有满足条件的,输出:-1 样例输入 44 样例输

【蓝桥杯】PREV-21 回文数字

题目链接:http://lx.lanqiao.org/problem.page?gpid=T113 历届试题 回文数字 时间限制:1.0s   内存限制:256.0MB 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找到一些5位或6位的十进制数字.满足如下要求: 该数字的各个数位之和等于输入的整数. 输入格式 一个正整数 n (10<n<100), 表示要求满足的数位和. 输出格式 若干行,每行

Palindrome-Number(判断回文数字)

判断一个数是否为回文数字,如1,2,3,121,1001,999都是回文数字,10,9898就不是回文数字. 解法:判断对称中心两端数字是否相同. 代码如下: bool isPalindrome(int x) { if (x<0 || (x != 0 && x % 10 == 0)) return false; int sum = 0; while (x>sum) { sum = sum * 10 + x % 10; x = x / 10; } return (x == sum

判断一个数字是否是回文数字,如果是则打印出100以内的回文数字,若不是只给出提示信息。

回文数字指的是什么呢?什么是回文数字呢? 回文数字的特征是:一组数字,从左读和从右读都是一样的,比如:123.123321.12345654321 public class HuiWenTest{    public static void main(String[] args)    {        Scanner sc = new Scanner(System.in); try        {            System.out.println("请输入你要判断的数字: &quo

条件结构的实例-水仙花数、猜拳游戏、回文数字

1.判断输入的数是否为水仙花数 int num,ge,shi,bai,he;  //声明变量 printf("请输入三位数");  //由用户输入一个三位数 scanf("%d",&num);   //将用户输入的数字保存给num //用户输入的数=个位的三次方+ 十位数的三次方+ 百位数的三次方 //1.从num获取个位 ge=num%10; //2.从num获取十位 shi=num/10%10; //3.从num获取百位 bai=num/100; he=

蓝桥杯:回文数字

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;}