Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit973fbbc

Browse files
committed
Add c/0179-largest-number.c
1 parent303ea5b commit973fbbc

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎c/0179-largest-number.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
intcompareInt(constvoid*a,constvoid*b) {
2+
inti=10;
3+
intj=10;
4+
intx=*(int*)a;
5+
inty=*(int*)b;
6+
7+
while (x /=10)i *=10;
8+
while (y /=10)j *=10;
9+
10+
return (((unsignedint)*(int*)b*i)+*(int*)a)> (((unsignedint)*(int*)a*j)+*(int*)b);
11+
}
12+
13+
intnumsOfDigits(intnum) {
14+
intl=1;
15+
while (num /=10)l++;
16+
returnl;
17+
}
18+
19+
char*largestNumber(int*nums,intnumsSize){
20+
inti,d;
21+
char*res=NULL;
22+
23+
if (numsSize<0) {
24+
returnres;
25+
}
26+
27+
// 1. sort the array with specified comparaotor.
28+
qsort(nums,numsSize,sizeof(int),compareInt);
29+
30+
// 2. caculate the length of the return string.
31+
d=0;
32+
for (i=0;i<numsSize;i++)d+=numsOfDigits(nums[i]);
33+
res=calloc(d+1,sizeof(char));
34+
35+
// 3. print all nums to the return string.
36+
if (nums[0]==0) {
37+
res[0]='0';
38+
returnres;
39+
}
40+
d=0;
41+
for (i=0;i<numsSize;i++) {
42+
sprintf(res+d,"%d",nums[i]);
43+
d+=numsOfDigits(nums[i]);
44+
}
45+
46+
returnres;
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp