fork download
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Test
  5. {
  6. private static int? SwapMax(int n)
  7. {
  8. var a = n.ToString().ToCharArray();
  9. var c = a.Max();
  10. var i = Array.IndexOf(a, c);
  11. if (i == 0) return null;
  12. a[i] = a[i-1];
  13. a[i-1] = c;
  14. return int.Parse(new String(a));
  15. }
  16.  
  17. public static void Main()
  18. {
  19. foreach (var n in new int[] {23, 32, 4056, 4056456, 8, 99, 89, 98, 103})
  20. Console.WriteLine("{0} => {1}", n, SwapMax(n));
  21. }
  22. }
Success #stdin #stdout 0.02s 25444KB
stdin
Standard input is empty
stdout
23 => 32
32 => 
4056 => 4065
4056456 => 4065456
8 => 
99 => 
89 => 98
98 => 
103 => 130