Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

解题思路:
(1)将整型数组转换成字符串数组
(2)对字符串数组按照递减的顺序进行希尔排序
(3)将排序后的字符串用strcat()函数拼接起来

char* largestNumber(int* nums, int numsSize) {

//将整型数组转换成字符串数组
char** strNums = (char**)malloc(sizeof(int*)*numsSize);
for (int i = 0; i < numsSize; i++)
{
    strNums[i] = (char*)malloc(sizeof(char)*10);
    sprintf(strNums[i],"%d",nums[i]);
}

//对字符串数组进行希尔排序,按递减的顺序,判断条件与常规的排序有点不同
for (int Incre = numsSize/2; Incre > 0; Incre /= 2)
{
    for (int i = Incre; i < numsSize; i++)
    {
        int j;
        char temp[10];
        strcpy(temp, strNums[i]);
        for (j = i; j >= Incre; j -= Incre)
        {
            char str1[20] = {'\0'};
            char str2[20] = {'\0'};
            sprintf(str1, "%s%s",temp,strNums[j-Incre]);
            sprintf(str2, "%s%s",strNums[j-Incre],temp);
            if (strcmp(str1,str2) > 0)
                strcpy(strNums[j],strNums[j-Incre]);
            else
                break;
        }
        strcpy(strNums[j],temp);
    }
}
//判断数组是不是全部为0
if (strcmp(strNums[0],"0") == 0)
    return "0";

//将排序后的数组合并成字符串
char* res = (char*)malloc(sizeof(char)*numsSize*10);
memset(res,'\0',strlen(res));
for (int i = 0; i < numsSize; i++)
    strcat(res,strNums[i]);

for (int i = 0; i < numsSize; i++)
    free(strNums[i]);
free(strNums);

return res;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码有问题,修改一下,MOV r0, #0x00002000 ; Initialize pointer to first number MOV r1, #9 ; Initialize counter with number of elements LDR r7, [r0] ; Load first number as largest LDR r8, [r0] ; Load first number as smallest Loop: ADD r0, r0, #4 ; Move pointer to next number LDR r2, [r0] ; Load the number in r2 CMP r7, r2 ; Compare largest with current number MOVLT r7, r2 ; If current number is smaller, update largest CMP r8, r2 ; Compare smallest with current number MOVGT r8, r2 ; If current number is larger, update smallest SUBS r1, r1, #1 ; Decrement counter BNE Loop ; Loop until all numbers are compared ; Display largest number on console MOV r0, #1 ; File descriptor for stdout LDR r1, =largest ; Address of string to be displayed MOV r2, #10 ; Length of string MOV r7, #4 ; Syscall number for write SWI 0 ; Call operating system ; Display largest number on LCD screen LDR r0, =0x40020C14 ; Address of LCD data register MOV r1, r7 ; Load largest number from r7 STR r1, [r0] ; Store the number in the LCD data register ; Display smallest number on console MOV r0, #1 ; File descriptor for stdout LDR r1, =smallest ; Address of string to be displayed MOV r2, #12 ; Length of string MOV r7, #4 ; Syscall number for write SWI 0 ; Call operating system ; Display smallest number on LCD screen LDR r0, =0x40020C14 ; Address of LCD data register MOV r1, r8 ; Load smallest number from r8 STR r1, [r0] ; Store the number in the LCD data register largest: .asciz "Largest number: %d\n" smallest: .asciz "Smallest number: %d\n"
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值