using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "Input5"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("12"); } else if (InputPattern == "Input2") { WillReturn.Add("21"); } else if (InputPattern == "Input3") { WillReturn.Add("1122"); } else if (InputPattern == "Input4") { WillReturn.Add("999999999"); } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); char[] StrArr = InputList[0].ToCharArray(); char[] DescStr = StrArr.OrderByDescending(X => X).ToArray(); ; //降順に並べた配列と、差異がある中で最も左の添字 int ChangeInd1 = -1; for (int I = 0; I <= StrArr.Length - 1; I++) { if (StrArr[I] != DescStr[I]) { ChangeInd1 = I; break; } } if (ChangeInd1 == -1) { Console.WriteLine(StrArr); return; } //降順に並べた配列のChangeInd1にある数字を持つ、最も右側の添字 int ChangeInd2 = Array.LastIndexOf(StrArr, DescStr[ChangeInd1]); //三角交換 char wkSankaku = StrArr[ChangeInd1]; StrArr[ChangeInd1] = StrArr[ChangeInd2]; StrArr[ChangeInd2] = wkSankaku; Console.WriteLine(new string(StrArr)); } }