Easy-26

leetcode   506. Relative Ranks           

Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

Example 1:

Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
For the left two athletes, you just need to output their relative ranks according to their scores.

Note:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

AC:

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */

char** findRelativeRanks(int* nums, int numsSize, int* returnSize) {
   
    char** result=(char**)malloc(numsSize*sizeof(char*));
    int A[10000];
    for(int j=0;j<numsSize;j++)
    {
        A[j]=j;
    }
    for(int i=0;i<numsSize-1;i++)
    {
        for(int j=i+1;j<numsSize;j++)
        {
            if(nums[i]<nums[j])
            {
                int k=nums[i];
                nums[i]=nums[j];
                nums[j]=k;
                k=A[i];
                A[i]=A[j];
                A[j]=k;
            }
        }
    }
   
    for(int i=0;i<numsSize;i++)
    {
        result[A[i]]=(char*)malloc(13*sizeof(char));
        if(i>=3)
        {
            char buf[32];
            sprintf(buf,"%d",i+1);
            strcpy(result[A[i]],buf);
        }else if(i==0)
        {
            strcpy(result[A[0]],"Gold Medal");
        }
        else if(i==1)
        {
            strcpy(result[A[1]],"Silver Medal");
        }
        else if(i==2)
        {
            strcpy(result[A[2]],"Bronze Medal");
        }
    }
    *returnSize=numsSize;
    return result;
}
tips: C 数字转字符串 sprintf(char buf[],"%d", num);  字符串转数字 long int strtol(const char *nptr,char **endptr,int base);

strtol函数说明 编辑
参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如’0x’前置字符则会使用16进制做转换、遇到’0’前置字符而不是’0x’的时候会使用8进制做转换。
一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符 指针由endptr返回;若参数endptr为NULL,则会不返回非法字符串。

strtol特点

编辑
1.不仅可以识别十进制整数,还可以识别其它进制的整数,取决于base参数,比如strtol("0XDEADbeE~~", NULL, 16)返回0xdeadbee的值,strtol("0777~~", NULL, 8)返回0777的值。
2.endptr是一个传出参数,函数返回时指向后面未被识别的第一个字符。例如char *pos; strtol("123abc", &pos, 10);,strtol返回123,pos指向字符串中的字母a。如果字符串开头没有可识别的整数,例如char *pos; strtol("ABCabc", &pos, 10);,则strtol返回0,pos指向字符串开头,可以据此判断这种出错的情况,而这是atoi处理不了的。
3.如果字符串中的整数值超出long int的表示范围(上溢或下溢),则strtol返回它所能表示的最大(或最小)整数,并设置errno为ERANGE,例如strtol("0XDEADbeef~~", NULL, 16)返回0x7fffffff并设置errno为ERANGE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FAILED: MyTestHttp : && /usr/bin/c++ -g CMakeFiles/MyTestHttp.dir/main.cpp.o -o MyTestHttp && : /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::Http()': /home/hy-20/project/MyTestHttp/main.cpp:20: undefined reference to `curl_global_init' /usr/bin/ld: /home/hy-20/project/MyTestHttp/main.cpp:21: undefined reference to `curl_easy_init' /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::~Http()': /home/hy-20/project/MyTestHttp/main.cpp:25: undefined reference to `curl_easy_cleanup' /usr/bin/ld: /home/hy-20/project/MyTestHttp/main.cpp:26: undefined reference to `curl_global_cleanup' /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::setUrl(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': /home/hy-20/project/MyTestHttp/main.cpp:29: undefined reference to `curl_easy_setopt' /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::setPostFields(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': /home/hy-20/project/MyTestHttp/main.cpp:31: undefined reference to `curl_easy_setopt' /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::setMethod(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': /home/hy-20/project/MyTestHttp/main.cpp:35: undefined reference to `curl_easy_setopt' /usr/bin/ld: /home/hy-20/project/MyTestHttp/main.cpp:36: undefined reference to `curl_easy_setopt' /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::setHeaders(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': /home/hy-20/project/MyTestHttp/main.cpp:41: undefined reference to `curl_slist_append' /usr/bin/ld: /home/hy-20/project/MyTestHttp/main.cpp:42: undefined reference to `curl_easy_setopt' /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::setWriteFunction(std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)> const&)': /home/hy-20/project/MyTestHttp/main.cpp:46: undefined reference to `curl_easy_setopt' /usr/bin/ld: /home/hy-20/project/MyTestHttp/main.cpp:47: undefined reference to `curl_easy_setopt' /usr/bin/ld: CMakeFiles/MyTestHttp.dir/main.cpp.o: in function `Http::perform()': /home/hy-20/project/MyTestHttp/main.cpp:50: undefined reference to `curl_easy_perform' collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed.
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值