UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)

【Niagara Vykon N4 】物联网学习 03照明控制及照明时间表 3.1 设计思路 3.2逻辑组件添加 ①依据原有控制照明逻辑,打开palette,找到schedule模块,添加BoolenSchedule。 ②打开kitContrl,找到Util,并添加BoolenSwitch。 ③剪切到原有的逻辑线,并进行逻辑连接,如下图。 ④新建BooleanWritable,命名为Switch,设置参数并进行逻辑连接,若下图。 3.3界面设计 ⑤至此完成了逻辑的设... 阅读详情

4thIIUCInter-University Programming Contest, 2005

A

Children’s Game

Input: standard input
Output: standard output

Problemsetter: Md. Kamruzzaman

There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be givenN positive integer. (S)He can make a big integer by appending those integers after one another. Such as if there are 4 integers as 123, 124, 56, 90 then the following integers can be made – 1231245690, 1241235690, 5612312490, 9012312456, 9056124123 etc. In fact 24 such integers can be made. But one thing is sure that 9056124123 is the largest possible integer which can be made.

You may think that it’s very easy to find out the answer but will it be easy for a child who has just got the idea of number?

Input

Each input starts with a positive integer N (≤ 50). In next lines there areN positive integers. Input is terminated byN = 0, which should not be processed.

Output

For each input set, you have to print the largest possible integer which can be made by appending all theN integers.

Sample Input

Output for Sample Input

4
123 124 56 90
5
123 124 56 90 9
5
9 9 9 9 9
0

9056124123
99056124123
99999

思路1:

贪心原则,为了让最终得到的数尽可能大,所以要让高位的数值尽可能大,直接按反字典序将数字串排序,并不正确,比如9和90排序后是90在前。

不过两个数字排列方式无非是一前一后,所以可以把两种排列的结果都比较下,这样做的的时间复杂度(只是两个数的比较)为O(3n),同时要借助2n的空间(n为两数字的长度之和),但是写法很简单。

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool Cmp(const string &s, const string &t)
{
	string st = s + t;
	string ts = t + s;
	return st > ts;
}

int main(void)
{
	int n;
	while (cin >> n)
	{
		if (n == 0) break;

		vector<string> integers;
		int i;
		for (i = 0; i < n; ++i)
		{
			string str;
			cin >> str;
			integers.push_back(str);
		}

		sort(integers.begin(), integers.end(), Cmp);

		for (i = 0; i < n; ++i)
		{
			cout << integers[i];
		}
		cout << endl;
	}
	return 0;
}

思路2:

假设如下数字串:

A: a1a2a3…an

B: b1b2b3…bm

AB: a1a2a3…anb1b2b3…bm

BA: b1b2b3…bma1a2a3…an

判断两数字串谁应该放在前面时(这里把字符串应该放在前面的称为较大数字串),有两种情况

1、  A和B能够直接比较出大小

当比较到ax和bx时(x<=n且x<=m),分出大小。

2、  需要对AB和BA的值进行比较

AB: a1a2a3…anb1b2b3…bm

BA: b1b2b3…bma1a2a3…an

比较到am和bm时(m<n),仍然相等,这时说明

a1a2a3…am= b1b2b3…bm①

接下来从AB式中的am+1和BA式中的a1进行比较

AB1:am+1am+2…anb1b2b3…bm

BA1:a1a2a3…an

用①式对BA1进行替换,得到

AB1:am+1am+2…anb1b2b3…bm

BA1:b1b2b3…bm am+1am+2…an

可以发现AB1和BA1与AB和BA的形式完全相同,只是下标的值不同而已,所以这个比较过程是递归的。

因为整个比较过程中,AB和BA中总是有一个下标在向后走,所以时间复杂度为O(n)(n为两数字串的长度之和)。

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool Cmp(const string &s, const string &t)
{
	int i;
	for (i = 0; i < s.length() && i < t.length(); ++i)
	{
		if (s[i] != t[i]) return s[i] > t[i];
	}

	if (s.length() == t.length())
	{
		return true;
	}
	else if (i == s.length())
	{
		return Cmp(s, t.substr(i));
	}
	else
	{
		return Cmp(s.substr(i), t);
	}
}

int main(void)
{
	int n;
	while (cin >> n)
	{
		if (n == 0) break;

		vector<string> integers;
		int i;
		for (i = 0; i < n; ++i)
		{
			string str;
			cin >> str;
			integers.push_back(str);
		}

		sort(integers.begin(), integers.end(), Cmp);

		for (i = 0; i < n; ++i)
		{
			cout << integers[i];
		}
		cout << endl;
	}
	return 0;
}

欢迎转载,但请尊重作者,标明出处  http://blog.youkuaiyun.com/code_pang/article/details/17762445。

vue3:最新实现腾讯人脸核身+增强版人脸核身使用方法及示例源码,Vue3如何使用腾讯云慧眼人脸核身,提供人脸核身案例、身份信息核验、活体检测与核身比对等示例代码(后端spring与thinkphp) vue3接入腾讯人脸核身,腾讯云人脸核身Vue3+后端代码,H5人脸核身,vue3 H5如何使用腾讯人脸核身sdk,vue3+thinkphp腾讯人脸核身,Spring Boot接入腾讯云人脸核身教程,hinkphp腾讯人脸核身,vue3人脸核身插件接入流程,vue+spring boot实现腾讯人脸核身,vue3接入腾讯云【增强版人脸核身接入】,vue3 h5微信公众号网页腾讯云慧眼人脸核身,tvue3接入人脸核身SDK,vue3+typescrip+vite接入慧眼实名认证,vue3腾讯云活体人脸核身( 阅读详情

相关推荐

【电子元件篇】电力电子中的变压器原理、作用、选型与测量指南-超简单解读

变压器虽小,却是电力电子的 “关键纽带”——从识别符号型号,到理解 “传交流隔直流” 的特点,再到按用途选参数、用万用表检测,只要抓住 “场景对应核心” 的逻辑,就能轻松掌握它的用法。

2301_81299416的博客 3024

Children's Game uva 10905

There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. ...

qq_43605752的博客 392

电池管理系统(BMS)系列(四)—数据采集之温度

属于BMS系列文章,介绍了电池包温度采集相关方式

zxx52113140的博客 5456

UVa 10905 - Children's Game

4th IIUC Inter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: standard output Problemsetter: Md. Kamruzzaman There a

ACM2272902662的专栏 1010

ST7789P3初始化配置

【代码】ST7789P3初始化配置。

zqq_1314的博客 4151

利用Skipper跨层追踪技术高效定位LVS Short问题

本文详细介绍了如何利用Skipper跨层追踪技术高效定位LVS Short问题。通过解析Skipper的跨层trace net原理和实战操作步骤,帮助芯片设计工程师快速识别版图中的短路问题,显著提升验证效率。文章还提供了处理大规模设计的高级技巧和常见问题排查方法。

weixin_29239661的博客 508

UVa 10905 Children's Game 解题报告(排序)

4th IIUC Inter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: standard output Problemsetter: Md. Kamruzzaman There are

SF-_- 的 ACM 博客 1136

Xilinx DDR3 MIG IP核设计--汇总导航

快速上手Xilinx DDR3 IP核----汇总篇

孤独的单刀 2万+

uva 10905 - Children's Game

4th IIUC Inter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: standard output Problemsetter: Md. Kamruzzaman

236

【国产MCU】-认识CH32V307及开发环境搭建

CH32V307是[沁恒](https://www.wch.cn/)推出的一款基于32位RISC-V设计的互联型微控制器,配备了硬件堆栈区、快速中断入口,在标准RISC-V基础上大大提高了中断响应速度。加入单精度浮点指令集,扩充堆栈区,具有更高的运算性能。扩展串口UART数量到8组,电机定时器到4组。提供USB2.0高速接口(480Mbps)并内置了PHY收发器,以太网MAC升级到千兆并集成了10M-PHY模块。

视觉与物联智能 2340

[Python从零到壹] 十三.机器学习之聚类算法四万字总结全网首发(K-Means、BIRCH、树状聚类、MeanShift)

欢迎大家来到“Python从零到壹”,在这里我将分享约200篇Python系列文章,带大家一起去学习和玩耍,看看Python这个有趣的世界。所有文章都将结合案例、代码和作者的经验讲解,真心想把自己近十年的编程经验分享给大家,希望对您有所帮助,文章中不足之处也请海涵。Python系列整体框架包括基础语法10篇、网络爬虫30篇、可视化分析10篇、机器学习20篇、大数据分析20篇、图像识别30篇、人工智能40篇、Python安全20篇、其他技巧10篇。您的关注、点赞和转发就是对秀璋最大的支持,知识无价人有情,希望

杨秀璋的专栏 2万+

10905 - Children's Game UVA

4th IIUC Inter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: standard output Problemsetter: Md. Kamruzzaman There are

510

ChatGPT充值,银行卡被拒绝,图文教程

ChatGPT充值,银行卡被拒绝

weixin_46713695的博客 2万+

UVA 10905 Children's Game

UVA_10905     这个题实际上也是个排序的题目,只不过排序的标准比较特殊,但有一点是不变的,如果任意局部是有序的,那么全局一定是有序的。 所以我们只要把qsort函数中的cmp函数的“排序规则”给改写一下就可以了,即对任意两个数a、b来说,如果ab大于ba,那么在最大值中a一定在b的前面,而对于ab与ba的比较,由于长度相同,只要调用一下strcmp就可以了。 由于输入数据里面有比...

weixin_33695082的博客 145

深度学习工具包 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")

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

uva 10905 Children's Game (排序)

题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大。 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面。可是,该怎么判断数值的大小(判断数值大小不能单单比较两个数的大小,比如遇到:1 、10的情况)。其实,判断一个不行,那就将两个合在一起考虑就可以了(就是比较110合101的大小来普判断1...

weixin_33989780的博客 114

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

UVa 10905: Children's Game

这真是一道有趣的题目,不知道别人怎么想,总之我觉得这题真的很有意思,值得一做。先附上题目: There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game her

rising_fallmoon的专栏 1966

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

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

UVA 10905 Children's Game (贪心)

Children's Game Problem Description There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Eac...

diaorong4751的博客 230

Uva10905

Children’s Game There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given

代码艺术的道与术 1104

UVA - 10905 Children's Game(贪心)

4thIIUCInter-University Programming Contest, 2005 A Children�s Game Input: standard input Output: standard output Problemsetter: Md. Kamruzzaman There

小G的ACM之路 1143

UVA - 10905 - Children's Game (简单排序)

UVA - 10905 Children's Game Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 4thIIUCInter-Universi

Never say never 1206

Children’s Game(String)

Description   4thIIUCInter-University Programming Contest, 2005 A Children’s Game Input: standard inputOutput: standard output Problemsetter: Md. Kamruzzaman ...

Simone chou... 205

UVA 10905 Children's Game [贪心]

#include <cstdio> #include <iostream> #include <algorithm> #include <string> using namespace std; int n; const int MAXN = 50 +9; string a[MAXN]; bool comp(string x, string y) { return x + y > y + x;

more time 413

Children's Game UVA - 10905 (简单贪心)

There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integ...

bibibi724 328

BNU19323 UVA10905 Children's Game

Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 10905 64-bit integer IO format: %lld      Java class name: Main Prev  Submit Status Statistics

要做ADC 480

湖南工业大学第一届ACM程序设计大赛

分糖果 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte 总提交:14            测试通过:11 描述   肖恩和帕特里克是兄弟,他们从他们的父母那里得到了很多糖果。每一块糖具有一个的正整数的价值,孩子们希望分他们得到的糖果。首先,肖恩将这些糖果分成两堆,并选择一堆给帕特里克。然后,帕特里克将尝试计算每堆

林下的码路 2298
上一篇: 单调队列——HDU 3415 Max Sum of Max-K-sub-sequence
下一篇: UVa 10382 - Watering Grass(贪心算法,区间覆盖问题)
庞老板
博客等级 码龄15年 127粉丝 66原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值