news 2026/4/28 10:20:00

把数组排成最小的数-C++

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
把数组排成最小的数-C++

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程https://www.captainai.net/troubleshooter

// 面试题45:把数组排成最小的数 // 题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼 // 接出的所有数字中最小的一个。例如输入数组{3, 32, 321},则打印出这3个数 // 字能排成的最小数字321323。 #include "cstdio" #include <string> #include <algorithm> int compare(const void *strNumber1, const void *strNumber2); // int型整数用十进制表示最多只有10位 const int g_MaxNumberLength = 10; char *g_StrCombine1 = new char[g_MaxNumberLength * 2 + 1]; char *g_StrCombine2 = new char[g_MaxNumberLength * 2 + 1]; void PrintMinNumber(const int *numbers, int length) { if (numbers == nullptr || length <= 0) return; char **strNumbers = (char **)(new int[length]); for (int i = 0; i < length; ++i) { strNumbers[i] = new char[g_MaxNumberLength + 1]; sprintf(strNumbers[i], "%d", numbers[i]); } qsort(strNumbers, length, sizeof(char *), compare); for (int i = 0; i < length; ++i) printf("%s", strNumbers[i]); printf("\n"); for (int i = 0; i < length; ++i) delete[] strNumbers[i]; delete[] strNumbers; } // 如果[strNumber1][strNumber2] > [strNumber2][strNumber1], 返回值大于0 // 如果[strNumber1][strNumber2] = [strNumber2][strNumber1], 返回值等于0 // 如果[strNumber1][strNumber2] < [strNumber2][strNumber1], 返回值小于0 int compare(const void *strNumber1, const void *strNumber2) { // [strNumber1][strNumber2] strcpy(g_StrCombine1, *(const char **)strNumber1); strcat(g_StrCombine1, *(const char **)strNumber2); // [strNumber2][strNumber1] strcpy(g_StrCombine2, *(const char **)strNumber2); strcat(g_StrCombine2, *(const char **)strNumber1); return strcmp(g_StrCombine1, g_StrCombine2); } // ====================测试代码==================== void Test(const char *testName, int *numbers, int length, const char *expectedResult) { if (testName != nullptr) printf("%s begins:\n", testName); if (expectedResult != nullptr) printf("Expected result is: \t%s\n", expectedResult); printf("Actual result is: \t"); PrintMinNumber(numbers, length); printf("\n"); } void Test1() { int numbers[] = {3, 5, 1, 4, 2}; Test("Test1", numbers, sizeof(numbers) / sizeof(int), "12345"); } void Test2() { int numbers[] = {3, 32, 321}; Test("Test2", numbers, sizeof(numbers) / sizeof(int), "321323"); } void Test3() { int numbers[] = {3, 323, 32123}; Test("Test3", numbers, sizeof(numbers) / sizeof(int), "321233233"); } void Test4() { int numbers[] = {1, 11, 111}; Test("Test4", numbers, sizeof(numbers) / sizeof(int), "111111"); } // 数组中只有一个数字 void Test5() { int numbers[] = {321}; Test("Test5", numbers, sizeof(numbers) / sizeof(int), "321"); } void Test6() { Test("Test6", nullptr, 0, "Don't print anything."); } int main(int argc, char *argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0; } // ------ Output ------ /* Test1 begins: Expected result is: 12345 Actual result is: 1245�a`5 Test2 begins: Expected result is: 321323 Actual result is: 32132�a`5 Test3 begins: Expected result is: 321233233 Actual result is: 32123323�a`5 Test4 begins: Expected result is: 111111 Actual result is: 11111�a`5 Test5 begins: Expected result is: 321 Actual result is: 321 Test6 begins: Expected result is: Don't print anything. Actual result is: */
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/28 10:20:00

快速体验胶片质感AI绘画:FLUX.1-Krea真实感模型部署与试用

快速体验胶片质感AI绘画&#xff1a;FLUX.1-Krea真实感模型部署与试用 1. 引言&#xff1a;当AI遇见专业摄影美学 你是否曾被AI生成图像的"塑料感"困扰&#xff1f;那些过于完美却缺乏真实质感的作品&#xff0c;往往难以满足专业摄影和商业设计的需求。今天我们将…

作者头像 李华
网站建设 2026/4/28 10:17:31

地平线校招 C++ 考试题到底怎么考?它不是互联网算法岗,是 AI、C++、系统软件一起筛

NMS、IOU、GEMM、RTOS、Autosar,这些东西如果出现在同一套面试里,你就不该再把它理解成普通互联网 C++ 岗。 地平线最典型的地方,恰恰不在普通算法题本身。 而是它会把三种东西一起塞进筛选链里: AI 算法理解 C++ 工程实现 芯片和系统软件约束 这三件事拆开看,你可能…

作者头像 李华
网站建设 2026/4/28 10:17:21

亲测可用 免费使用 云远程调试软件V2.1.0 远程串口调试 远程网口调试

云远程调试软件 文章目录云远程调试软件前言一、工具用途二、软件环境三、安装1、安装vspd2、打开远程调试软件四、基本操作1、订阅主题2、连接3、串口调试4、文本发送4、网口调试六、软件地址前言 关键字&#xff1a;云调试、远程调试软件、串口远程调试、串口调试、网口调试…

作者头像 李华
网站建设 2026/4/28 10:16:21

为什么石墨可以导电而金刚石不导电

金刚石和石墨虽然都由碳原子组成&#xff0c;但导电性截然不同&#xff0c;根本原因在于碳原子的排列方式和化学键结构&#xff0c;这决定了自由电子的有无。 我们可以从两个关键点来理解&#xff1a;金刚石&#xff1a;没有自由电子 结构&#xff1a;每个碳原子都与周围4个碳原…

作者头像 李华
网站建设 2026/4/28 10:16:20

免费生成专业级法线贴图:NormalMap-Online终极实战指南

免费生成专业级法线贴图&#xff1a;NormalMap-Online终极实战指南 【免费下载链接】NormalMap-Online NormalMap Generator Online 项目地址: https://gitcode.com/gh_mirrors/no/NormalMap-Online 你是否曾为3D模型缺乏表面细节而烦恼&#xff1f;是否在寻找一款无需昂…

作者头像 李华
网站建设 2026/4/28 10:13:56

Unlock Music Electron:打破音乐平台加密限制的桌面解决方案

Unlock Music Electron&#xff1a;打破音乐平台加密限制的桌面解决方案 【免费下载链接】unlock-music-electron Unlock Music Project - Electron Edition 在Electron构建的桌面应用中解锁各种加密的音乐文件 项目地址: https://gitcode.com/gh_mirrors/un/unlock-music-el…

作者头像 李华