1038. Recover the Smallest Number (30)

汽车工程师必看:从CAN到Ethernet,6种车载通信协议全解析(附应用场景对比) 本文全面解析了从CAN到Ethernet的6种车载通信协议,包括CAN、LIN、MOST、FlexRay等,并对比了它们的应用场景和技术特点。文章详细介绍了各协议的演进历程、技术优势及在现代汽车中的混合网络架构设计,为汽车工程师提供了实用的协议选型指南和未来通信技术发展趋势。 阅读详情

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:

Each input file contains one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the smallest number in one line. Do not output leading zeros.

Sample Input:
5 32 321 3214 0229 87
Sample Output:

22932132143287

这一题,看上去不难,就是对每个数字段进行排序,不过不能按照简单的字典顺序排序,比如32和3214,应该3214在前面的。所以自己写compare函数时,需要这样写,如果a+b > b+a,这样才交换a和b的位置,否则不变。这样以来排序得到的结果连起来再把前几位的0去掉就可以。

不过一开始我用C写的,就是最后一个case老是过不去,最后一个case就是测试时间复杂度的,按道理前面的都能过最后一个没理由过不了。百度了一下发现有好多人用的方法和我的是一样的,只不过用的是C++写的,于是我改成C++就AC了。实在不懂。。。。

AC代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

bool cmp(const void *a,const void *b)
{
	char *pa,*pb;
	char tmp1[20],tmp2[20];
	pa = (char *)a;
	pb = (char *)b;
	strcpy(tmp1,pa);
	strcat(tmp1,pb);
	strcpy(tmp2,pb);
	strcat(tmp2,pa);

	return strcmp(tmp1,tmp2)<0;
}

int main(int argc, char *argv[])
{
	int i,n;
	char **s;
	char *r;

	cin >> n;
	s = new char *[n];
	for (i = 0; i < n; ++i)
		s[i] = new char[10];
	for (i = 0; i < n; ++i)
		cin >> s[i];

	sort(s,s+n,cmp);
	r = new char[10*n];
	for (i = 0; i < n; ++i)
		strcat(r,s[i]);
	i = 0;
	while(r[i] == '0')
		i++;
	if( !r[i] )
		std::cout << 0 << std::endl;
	else
		std::cout << r+i << std::endl;

	delete []r;
	return 0;
}

下面的是我最后一个case过不了的C代码,几乎和上面的C++代码一样,不知到底是为何,求大神告知

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PRINT_I(x) printf("%s = %d\n",#x,x);
#define PRINT_F(x) printf("%s = %f\n",#x,x);
#define PRINT_S(x) printf("%s = %s\n",#x,x);

int cmp(const void *a,const void *b);

int main(int argc,char* argv[])
{
	int i,n;
	char *result;
	char **seg;

	//freopen("input","r",stdin);
	scanf("%d",&n);

	if( !n )
		return 0;

	seg = (char **)malloc(sizeof(char *)*n);
	for (i = 0; i < n; ++i)
	{
		seg[i] = (char *)malloc(sizeof(char)*10);
	}

	for (i = 0; i < n; ++i)
		scanf("%s",seg[i]);

	qsort(seg,n,sizeof(seg[0]),cmp);

	result = (char *)malloc(10*n);
	for (i = 0; i < n; ++i)
		strcat(result,seg[i]);
	i = 0;
	while(result[i] == '0')
		i++;
	if( !result[i] )
		printf("0\n");
	else
		printf("%s\n", result+i);

	free(result);
	return 0;
}

int cmp(const void *a,const void *b)
{
	char *pa,*pb;
	char tmp1[20],tmp2[20];
	pa = *(char **)a;
	pb = *(char **)b;
	strcpy(tmp1,pa);
	strcat(tmp1,pb);
	strcpy(tmp2,pb);
	strcat(tmp2,pa);

	return strcmp(tmp1,tmp2);
}


SAP 创建PO时提示“不能确定任何组件” SAP 创建委外加工的PO时提示“不能确定任何组件” 而如果BOM已经创建了,但还是提示该错误保存。 解决办法:1. BOM selection 未配置好; 2. 由于Purchasing prosessing time的设置,如果PO中的delivery time - Purchasing prosessing time是过去的日期,那么会找不到BOM,BOM的有效期需早 于PO审批的有效期。 3. BOM未... 阅读详情

相关推荐

网络变压器等效电路及网络变压器中间抽头作用详解

       华强盛电子导读:要详细了解网络变压器周边EMC管控及网络变压器BOB-SMITH电路的连接原理,首先我们对网络变压器的认识及其电路各参数影响都要有个简要的认识,本文,对非理想网络变压器等效电路做一个详细的分析,其中重点讨论一下网络变压器中间抽头的作用其影响                          网络变压器等效电路及                        ...

网络变压器专业资料 6010

1038 Recover the Smallest Number (30 分)

1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover...

92377 742

Linux文件操作四剑客

① /^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日 yyyy-MM-dd / yy-MM-dd 格式。⑥“^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$” //正浮点数。

wuds_158的博客 1613

1038 Recover the Smallest Number (30 分)

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0...

SourDumplings的博客 328

【STM32Cube HAL】输入捕获(六)——PWM测量

实验内容:使用通用定时器输入捕获测量信号的脉宽(检测按键按下的时间)。 一、原理图 二、 CubeMX配置 Step1.打开 STM32CubeMX,点击“New Project”,选择芯片型号,STM32F103VETx。 Step2.选择时钟源,并配置时钟树。选择Crystal/Ceramic Resonator,并配置系统时钟为72M。 Step3.配置SYS,我们这里选择的是Serial Wire。(正常情况配置不配置不影响,debug可以使用。但是你不可以把这...

HAHAHA 1万+

基于单片机红外线遥控Proteus仿真电路发射接收

*单片机设计介绍,基于单片机红外线遥控Proteus仿真电路发射接收。

2201_75940997的博客 3952

1038 Recover the Smallest Number (30分)--PAT甲级真题

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229...

Samson的博客 297

【扣子AI开发】如何开发一个图像生成智能体?

【扣子AI开发】如何开发一个图像生成智能体,从创建到发布

tl437002770的博客 2432

PAT甲级 1038 Recover the Smallest Number (30分)

1038 Recover the Smallest Number (30分) 题目链接:PAT A 1038 题目大意:给出一个数n,随后给出n个不超过8位的数字,问如何将这些数字组合成一个最小的数,要求输出这个最小数。注意如果生成的数是类似000325这种,那么输出时应舍去最前面的所有0,输出325。 思路分析:从样例里就可以看出来,组合似乎跟字符串的字典序有很大关系,但是仅根据字典序又是不对...

weixin_43684054的博客 321

【小白深度教程 1.32】手把手教你从多视角图像进行 3D 重建(SfM 算法)

重建的目标是从一组照片中推导场景的几何结构,假设摄像机位置和内部参数是已知的或可以从图像集中猜测。 这是通过使用多个照片,在其中应用运动结构法(Structure from Motion,SfM)来解决像素级对应问题,部分地恢复3D信息来实现的。

2401_87064292的博客 2166

Ollama + OpenCLAW 在 Windows 系统的完整部署指南

Windows 部署优先选 WSL2,避免原生环境的权限/兼容性问题;网络问题通过“离线包+国内镜像”解决,版本问题通过升级 Node.js 解决;内存不足可切换轻量模型或增大 WSL2 内存分配;核心链路:Ollama(模型管理)→ Gateway(网关)→ OpenCLAW TUI(交互),各组件启动正常即可稳定使用。

lpfasd123的博客 2840

1038 Recover the Smallest Number (30)

1038 Recover the Smallest Number (30)(30 分) Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can reco...

Become hunger become strong 526

SAP RAP学习系列合集介绍

《SAP RAP学习系列合集:从入门到部署的完整指南》摘要: 本文介绍了作者创建的SAP RAP学习系列合集,涵盖RAP开发从基础概念到实战部署的全流程。系列目前包含17篇文章,系统讲解了RAP核心开发技术:从数据建模、UI构建到行为定义(包括并发控制、草稿功能、授权验证等),最后详细演示了应用部署到Fiori Launchpad的两种方案(OP版和ES版)。

DeveloperMrMeng的博客 1512

1038 Recover the Smallest Number (30分)

题目 Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0...

小白的博客 229

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

1038 Recover the Smallest Number (30分)【贪心】

1038Recover the Smallest Number(30分) Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover...

胡小涛的博客 215

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

1038 Recover the Smallest Number (30 分)

1038Recover the Smallest Number(30 分) Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can ...

weixin_30808693的博客 76

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

PAT 1038 Recover the Smallest Number (30 分)

1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recove...

小渣渣的博客 254
上一篇: 1037. Magic Coupon (25)
下一篇: 图像验证码的识别(一)——浅谈bmp文件格式
ysc_ysc
博客等级 码龄15年 212粉丝 48原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值