输出回文数字

【python】回文查找:在一串数字中查找出最长的回文字段并输出 输入一串数字,找出其中最长的对称回文字段,并输出 输入:“34312332112332114” 输出一:最长回文字段 123321123321 输出二:所有回文字段 [‘343’, ‘123321’, ‘123321123321’, ‘11233211’] # 定义保存回文字段的列表 hw = [] numstr = "34312332112332114" # 第一种回文,金字塔型:343 ... 阅读详情

贴吧上看到有人发蓝桥杯题目。

观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的。这样的数字叫做:回文数字。

本题要求你找到一些5位或6位的十进制数字。满足如下要求:
该数字的各个数位之和等于输入的整数。
【输入格式】
一个正整数 n (10<n<100), 表示要求满足的数位和。
【输出格式】
若干行,每行包含一个满足要求的5位或6位整数。
数字按从小到大的顺序排列。
如果没有满足条件的,输出:-1
【样例输入】
44
【样例输出】
99899
499994
589985
598895
679976
688886
697796
769967
778877
787787
796697
859958
868868
877778
886688
895598
949949
958859
967769
976679
985589
994499
【样例输入2】
60
【样例输出2】
-1
【资源约定】
峰值内存消耗 < 64M

CPU消耗 < 1000ms


写了三种算法,下面算法最快,CMD下输入44,运行时间10毫秒。

考虑到%运算消耗较大,故改用StringBuilder。

而StringBuilder继承了Object的equals()方法,比较的是内存地址,故另写了一个equal

import java.util.Scanner;
import java.util.ArrayList;

public class Test{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long start = System.currentTimeMillis();
        boolean isPrint = false;
        StringBuilder s1 = new StringBuilder();
        ArrayList<String> list = new ArrayList<String>();
        for(int i = 1; i <= 9; i++){
            for(int j = 1; j <= 9; j++){
                for(int k = 1; k <= 9; k++){
                    s1.setLength(0);
                    s1.append(i).append(j).append(k);
                    s1.append(s1.charAt(1)).append(s1.charAt(0));
                    if(sumEveNum(s1) == n){
                        System.out.println(s1);
                        isPrint = true;
                    }
                    s1.setLength(0);
                    s1.append(i).append(j).append(k);
                    s1.append(s1.charAt(2)).append(s1.charAt(1)).append(s1.charAt(0));
                    if(sumEveNum(s1) == n) list.add(s1.toString());
                }
            }
        }
        for(String s: list){
            System.out.println(s);
            isPrint = true;
        }
        if(!isPrint) System.out.println(-1);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    private static int sumEveNum(StringBuilder s1){
        int sum = 0;
        for(int i = 0; i < s1.length(); i++){
            sum += s1.charAt(i) - '0';
        }
        return sum;
    }
}


Java实现蓝桥杯历届试题回文数字 历届试题 回文数字 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述   观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的。这样的数字叫做:回文数字。 本题要求你找到一些5位或6位的十进制数字。满足如下要求:   该数字的各个数位之和等于输入的整数。 输入格式   一个正整数 n (10<n<100), 表示要求满足的... 阅读详情

相关推荐

OCV/AOCV/SOCV的介绍和应用

library中的cell delay是在某个固定的PVT(operating condition)下仿真得出的。,cell的delay并不是一个固定值,而是一个随机值,遵循高斯分布或门特卡洛分布,在STA中用OCV来模拟这一『特征』。OCV全称on chip variation,用于描述不同管子间由于工艺偏差、电压降、温度变化引起的delay变化。,也用于描述工艺偏差引起的net厚度宽度的变化从而导致net的电容电阻变化。优点:设置起来比较简单粗暴,对于不那么先进的工艺可行!的影响,更贴合实际。

m0_61003348的博客 8980

蓝桥杯历届试题之回文数字

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

aiyuan1996的专栏 2118

手机刷双系统

刷机教程(https://www.geekeyes.cn/forum.php/forum.php?mod=viewthread&amp;tid=492) 解锁教程(http://www.gioov.com/index.php/1386)   解锁教程 cmd下 进入adb目录( 如果环境变量配置了adb 可以直接在cmd 下输入命令) 手机连接电脑,确保usb 调试打开,然后手机连接电脑,...

超宇的博客 1万+

回文数(对称数)

回文数

m0_74780250的博客 674

如何使用AKShare作为OpenBB的数据源

`openbb_akshare`项目是 OpenBB 的数据源扩展。可以用于查询A股和港股数据

FinAnalyzer金融分析工具 3196

Cadence IC618/Spectre231安装避坑指南:详解环境变量配置与License管理那些事儿

本文深入解析Cadence IC618/Spectre231安装过程中的环境变量配置与License管理难题,提供从变量冲突解决到高效License管理的实用方案。针对常见的64位标识冲突、路径污染及License服务器连接问题,给出具体诊断步骤和优化建议,帮助用户构建稳定可靠的EDA环境。

weixin_33737134的博客 616

回文数的输出

问题描述   1221是一个非常特殊的数,它从左边读和从右边读是一样的,编程求所有这样的四位十进制数。 输出格式   按从小到大的顺序输出满足条件的四位十进制数。 for i in range(1000,10000): a=str(i) sum=0 if a==a[::-1]: print(a) ...

weixin_60663670的博客 591

YOLO学习笔记 | YOLOv8与卡尔曼滤波实现目标跟踪与预测(附代码)

YOLOv8采用改进的CSPDarknet骨干网络和动态标签分配策略,提供高精度、实时的目标检测能力。使用IOU(交并比)或马氏距离作为代价矩阵,通过匈牙利算法匹配预测框与检测框,解决多目标ID分配问题。卡尔曼滤波通过状态方程和观测方程对目标运动建模,结合预测与观测数据优化状态估计。完整代码需结合具体场景调整参数,并补充匈牙利算法实现(可参考。匈牙利算法(Hungarian Algorithm)是解决。的高效方法,常用于目标跟踪中的。),作为跟踪的输入观测值。(将检测框与预测框匹配)。

尘世冰封的专栏 1250

回文输出

对一个字符串回文输出,如“how are you doing”,回文后输出应该是“doing you are how”,注意,倒序的只是单词的顺序,但是每个单词内部的字母仍然是按照原来的顺序输出。程序如下: #include <stdio.h> void main(void) { char arr[] = "how are you doing"; char...

weixin_30635053的博客 180

【第1章>第9节】基于FPGA的图像开运算处理算法的Verilog实现

本文介绍了基于FPGA的开运算图像处理实现方法。主要内容包括:1)输入图像二值化处理,通过阈值将灰度图像转换为二值图像;2)数据缓存与窗口提取,使用寄存器数组缓存5行图像数据并构建5×5处理窗口;3)腐蚀运算逻辑,判断窗口内是否全为白色像素;4)膨胀运算逻辑,检测窗口内是否存在白色像素。最后给出了Verilog实现方案,通过级联腐蚀和膨胀模块完成开运算处理。该方法可用于FPGA图像处理算法开发。

MATLAB,verilog,python,opencv,tensorflow,caffe,C,C++ 315

【数据集2】CPC Global Unified Temperature/Precipitation数据简介及处理

简介CPC 温度及降雨 数据

WW、forever的博客 3107

java 递归 输出回文数字

  递归 输出回文数字 例如:参数:4 输出:4321234。程序如下:public class TestDG { public int fun(int var) {     if(var>1){   System.out.println(var);   System.out.println( fun(var - 1));     }     return var; }  publ

xiaotao8009的专栏 1398

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

java输出回文数原代码_java 实现判断回文数字的实例代码

前言:有这样一类数字,它们顺着看和倒着看是相同的数,例如:121、656、2332等,这样的数字就称为回文数字。编写一个Java程序,判断从键盘接收的数字是否为回文数字。2、解题思想从回文数字的特点出发,弄清楚其特点是解决本问题的关键。解决方案可以通过将该数字倒置的办法来判断它是否是回文数字,例如:586,它的倒置结果为685,因为586!=685,故586不是回文数字。3、Java代码impor...

weixin_39778150的博客 617

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

python输出回文数字_Python实现寻找回文数字过程解析

回文数字是很有意思的数字,不管从最高位开始念,还是从个位开始念,最终结果都一样,有一种对称美。下面是回文数字的函数判断方式:def is_palindrome(n):str_num = str(n)len_num = len(str_num)if len_num <= 2 and str_num[0] == str_num[-1]:return Trueelse:half_len = rou...

weixin_39760368的博客 1284

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

python输出回文数字_生成python中3个数字的所有回文数字列表

>>> L = [int("%d%d%d" % (x,y,x)) for x in range(1,10) for y in range(10)]>>> L[101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252,262, 272, 282, 292, 303,...

weixin_39580124的博客 1365

使用C++ 输出两个数字之间的回文数,并统计回文数个数

回文数:简单点讲,就是从左至右念和从右至左念都相同的数字。 首先,我们需要的判断一个数字是否是回文数 int main() { int m,i; cin>>m; i=m; while(i>0) { int n=0; n=n*10+i%10; i=i/10; } if(m==n) cout<<m<<"是回文数。"<<endl; else cout<<m<<"不是回文数"<<endl;

qq_51738497的博客 8002

C语言倒叙/回文输出整数数字

2:int main(void)函数的入口,void是无返回值。1:#include<stdio.h>引入标准输入输出库。

2401_87533153的博客 128

python输出回文数字_python中3位数字的最高回文

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.Find the largest palindrome made from the product of two 3-digit nu...

weixin_39687468的博客 297

用C语言输出回文序列

例如给定1234, 输出1234321, 给定1357输出1357531。printf("请输入一个整数:");给定一个数字, 请构造它的回文数字.printf("构造的回文数:");

2302_80604680的博客 632

回文数、整数输出,数字序列最小分割问题

判断一个数字N是不是回文数字      这里介绍两种方法,一、将数字逆序,N mod 10可以计算出最低位,除以      10可以去掉最末尾的数字,不断这样操作就可以得到N的一个逆转序列,将      该序列重新变为一个新的十进制数字,和原来的数字比较可以判断该数字是      否是回文,N的逆序数字可能会溢出,最好将数字提升计算。            和判断字符串是否回文一样从两

yuan_da_xing的专栏 1260

-9 逆序输出一个整数的各位数字_判断一个数是否为回文数

题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。问题分析要会判断用户输入的是几位数,并将每一位数分解出来程序可以利用取整和取余的方法进行判断例子:假设a为5位数,请问如何取得最高位?最高位为a//10000,直接取整;第4位呢?a%10000//1000,先取余,再取整;主要逻辑如下:用户先输入一个数求出第5位数a求出第4位数b求出第3位数c求...

weixin_39790102的博客 108

【输出数字回文金字塔图形】2024-10-14

本文介绍了两种使用C++编写数字回文金字塔的方法。两种方法都通过循环结构实现金字塔的输出,其中包含空格控制、数字递增和递减打印等核心逻辑。第一种方法实现了紧凑的数字排列,第二种方法则在数字间添加了空格。两种实现都采用了goto语句进行流程控制,通过标签跳转实现不同打印阶段的转换。代码结构清晰展示了数字金字塔的生成过程,包括前置空格处理、前半部分数字递增打印和后半部分数字递减回文打印。

浤济的专栏 157

C语言学习 回文数字

回文数字 Description 给定一个数字字符串,判断它是否是回文数字。例如: 121, 1221是回文数字, 123不是回文数字。 Input 输入一个数字字符串。 Output 若是回文输出 Yes, 否则输出 No Sample Input 123321 Sample Output Yes #include<stdio.h> int main() { int count=0,flag=0,count2=0; char c[100]; ...

拖更工作室 1528

Python用户自己输入一数字,判断是否是回文数,输出判断结果

Python用户自己输入一数字,判断是否是回文数,输出判断结果。 一、例如:二、代码:总结 ) 一、例如: 输入:123,输出:不是回文数 输入:12321,输出:是回文数 二、代码: 代码如下(示例): number=input() if number==number[::-1]: print("是回文数"); else: print("不是回文数"); 总结 提示:这里对文章进行总结: ...

Alfred_tfk的博客 7268
上一篇: String与StringBuffer和StringBuilder区别之一
下一篇: 求一亿以内的素数
KingLearnJava
博客等级 码龄12年 93粉丝 84原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值