-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path027 - Next bigger number with the same digits.cs
105 lines (95 loc) · 2.68 KB
/
027 - Next bigger number with the same digits.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Next bigger number with the same digits
// https://www.codewars.com/kata/next-bigger-number-with-the-same-digits/train/csharp
// "C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe" /debug+ /langversion:6 /r:"C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\mstest.testframework\1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll" /r:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.dll "027 - Next bigger number with the same digits.cs"
public class Kata
{
public static long NextBiggerNumber(long n)
{
if (n < 12)
return -1;
Console.WriteLine("n={0}", n);
StringBuilder res = null;
StringBuilder buff = new StringBuilder();
buff.Append(n);
for (int i = (buff.Length - 2); i >= 0; i--)
{
if (buff[i] < buff[i + 1])
{
char val = '9';
int k = i + 1;
for (int j = (i + 1); j < buff.Length; j++)
{
if (buff[j] > buff[i] && buff[j] < val)
{
val = buff[j];
k = j;
}
}
res = new StringBuilder();
res.Append(buff.ToString());
val = res[i];
res[i] = res[k];
res[k] = val;
if (i < (res.Length - 2))
{
Console.WriteLine("i={0}; res-len={1}", i, res.Length);
bool fswap = false;
int len = res.Length;
do
{
fswap = false;
for (int j = (i + 2); j < len; j++)
{
if (res[j - 1] > res[j])
{
fswap = true;
val = res[j - 1];
res[j - 1] = res[j];
res[j] = val;
}
}
len--;
} while (fswap);
}
break;
}
}
if (res != null)
{
long v = Convert.ToInt64(res.ToString());
Console.WriteLine("v={0}", v);
return v;
}
return -1;
}
}
public class NextBiggerNumberTests
{
public static void Main()
{
try
{
Console.WriteLine("****** Small Number");
Assert.AreEqual(21, Kata.NextBiggerNumber(12));
Assert.AreEqual(531, Kata.NextBiggerNumber(513));
Assert.AreEqual(2071, Kata.NextBiggerNumber(2017));
Assert.AreEqual(441, Kata.NextBiggerNumber(414));
Assert.AreEqual(414, Kata.NextBiggerNumber(144));
Assert.AreEqual(-1, Kata.NextBiggerNumber(9));
Assert.AreEqual(-1, Kata.NextBiggerNumber(111));
Assert.AreEqual(-1, Kata.NextBiggerNumber(531));
//Assert.AreEqual(397521, Kata.NextBiggerNumber(297531));
Assert.AreEqual(1234567908, Kata.NextBiggerNumber(1234567890)); // 1234567980
Assert.AreEqual(1051106629, Kata.NextBiggerNumber(1051106296)); // 1051106692
Assert.AreEqual(1853978458, Kata.NextBiggerNumber(1853975884)); // 1853978584
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.ToString());
Environment.Exit(1);
}
}
}