-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCJ2017_B_TidyNumbers.cs
52 lines (49 loc) · 1.36 KB
/
GCJ2017_B_TidyNumbers.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
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace Contest
{
public class GCJ2017_B_TidyNumbers : ALineProblem
{
protected override string SolveProblem(int problemIndex, string line)
{
// Read data
int n = line.Length;
int[] digits = new int[n];
for (int i = 0; i < n; i++)
{
digits[i] = line[i] - '0';
}
// Solve
while (true)
{
bool restart = false;
for (int i = 0; i < n - 1; i++)
{
if (digits[i] > digits[i + 1])
{
digits[i]--;
for (int j = i + 1; j < n; j++)
digits[j] = 9;
restart = true;
break;
}
}
if (!restart)
break;
}
// Print
StringBuilder sb = new StringBuilder();
int start = (digits[0] == 0) ? 1 : 0;
for (int i = start; i < n; i++)
{
sb.Append(digits[i]);
}
string result = sb.ToString();
return result;
}
}
}