【蓝桥杯】PREV-21 回文数字
题目链接:http://lx.lanqiao.org/problem.page?
gpid=T113
本题要求你找到一些5位或6位的十进制数字。满足例如以下要求:
该数字的各个数位之和等于输入的整数。
数字按从小到大的顺序排列。
假设没有满足条件的,输出:-1
499994
589985
598895
679976
688886
697796
769967
778877
787787
796697
859958
868868
877778
886688
895598
949949
958859
967769
976679
985589
994499
然后按从小到大的顺序直接构造。最后直接查找就可以
#include<iostream>
#include<string>
#define N 1000000
using namespace std;
int pos=0,n;
class Node{
public:
int sum;
string num;
void Calc(int k,int flag){
string str="",tmp="";
while(k) {
str+='0'+k%10; k/=10;
}
tmp+=str[2]; tmp+=str[1];
if(flag){
str=tmp+str;
}
else str=tmp+str[0]+str;
num=str;
}
void CalcBit(){
string s=num;
int total=0,len=s.size();
for(int i=0;i<len;i++) total+=s[i]-'0';
sum=total;
}
}x[N];
void init(){
for(int i=100;i<1000;i++){
x[pos].Calc(i,1);
x[pos++].CalcBit();
}
for(int i=100;i<1000;i++){
x[pos].Calc(i,0);
x[pos++].CalcBit();
}
}
int main()
{
init();
cin.sync_with_stdio(false);
cin>>n;
int flag=1;
for(int i=0;i<pos;i++)
if(x[i].sum==n)cout<<x[i].num<<endl,flag=0;
if(flag)cout<<-1<<endl;
return 0;
}
<span style="color: rgb(51, 51, 51); font-family: 宋体, 'Times New Roman';font-size:18px; line-height: 18.5714302062988px;">出处:</span><a target=_blank href="http://blog.csdn.net/mummyding" style="font-family: 宋体, 'Times New Roman';font-size:18px; line-height: 18.5714302062988px;">http://blog.csdn.net/mummyding</a><span style="color: rgb(51, 51, 51); font-family: 宋体, 'Times New Roman';font-size:18px; line-height: 18.5714302062988px;"> </span>
作者:MummyDing
【蓝桥杯】PREV-21 回文数字的更多相关文章
- Java实现蓝桥杯历届试题回文数字
历届试题 回文数字 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做: ...
- BASIC-9_蓝桥杯_特殊回文数
示例代码: #include <stdio.h> int main(void){ int n = 0 ; scanf("%d",&n); int i = 0 ; ...
- [蓝桥杯]PREV-21.历届试题_回文数字
问题描述 观察数字:, 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找到一些5位或6位的十进制数字.满足如下要求: 该数字的各个数位之和等于输入 ...
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- javascript 实现一个回文数字
写一个方法,让"1234"变成回文数字“1234321”,就是顺着读和倒着读都是一样的:注:不让用reverse()方法: function palindrome(str){ va ...
- UVALive 2889(回文数字)
题意:给定i,输出第i个回文数字. 分析:1,2,3,4,……,9------------------------------------------------------------------- ...
- Java 简单算法--打印回文数字
package cn.magicdu.algorithm; public class CircleNumber { public static void main(String[] args) { f ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- 算法笔记_181:历届试题 回文数字(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找 ...
随机推荐
- jQuery动画animate方法使用介绍
用于创建自定义动画的函数. 返回值:jQuery animate(params, [duration], [easing], [callback]) 如果使用的是“hide”.“show”或“togg ...
- PHP http_build_query()方法
http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述 string http_build_query ( arr ...
- python enum 枚举
http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html python 3.4+ from enum import Enu ...
- Maven依赖的Scope去除部署不需要的jar 包(打包)
<dependency> < groupId>javax.servlet</groupId> < artifactId>jsp-api</arti ...
- The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
CurrentStock = db.BillEntry.Where(b => b.GoodsId == item.GoodsId).Sum(b => (decimal?)b.Qty) ?? ...
- windows命令行 批量对源代码添加版权头/头信息
简短精悍的代码,特别适合开源项目使用. for /r %%F in (*.as) DO ( move "%%F" tmp.txt type copyright.txt > & ...
- HotSpot JVM Component
- Axure 实现数字自动加键功能(点击“+”数字加1,点击“-”数字减1)
百度网盘:http://pan.baidu.com/s/1gfPQ9V1 在上面的元件库中,拖“数字框(仅正数)”即可实现自动加减的功能 如图:
- Redis从入门到精通:中级篇(转)
原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...
- SSM框架-使用MyBatis Generator自动创建代码
参考:http://blog.csdn.net/zhshulin/article/details/23912615 SSM搭建的时候用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半 ...