-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
51 lines (49 loc) · 1.51 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Linq;
namespace LargestNumber
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 3, 30, 34, 5, 9 };
Console.WriteLine(LargestNumber_Sort(nums));
}
static string LargestNumber(int[] nums)
{
if (nums.All(n => n == 0))
return "0";
for (int i = 0; i < nums.Length; i++)
{
for (int j = i + 1; j < nums.Length; j++)
{
if (CheckGreater(nums[i], nums[j]))
{
int tem = nums[i];
nums[i] = nums[j];
nums[j] = tem;
}
}
}
string res = "";
for (int i = nums.Length - 1; i >= 0; i--)
res += nums[i].ToString();
return res;
}
static bool CheckGreater(int a, int b)
{
string A = a.ToString();
string B = b.ToString();
return double.Parse(A + B) > double.Parse(B + A);
}
static string LargestNumber_Sort(int[] nums)
{
if (nums.All(n => n == 0)) return "0";
var res = new string[nums.Length];
for (int i = 0; i < nums.Length; i++)
res[i] = nums[i].ToString();
Array.Sort(res, (a, b) => (int) (double.Parse(b + a) - double.Parse(a + b)));
return string.Join("", res);
}
}
}