お世話になっております。
以下のC#コードでABC082Bを解こうとしたらREになりました。
どこらへんが怪しいでしょうか?
また、REになった場合、どのように検討をつければいいのでしょうか。
以上となりますがよろしくお願いいたします。
using System.Linq; using System; class Program { static void Main(string[] args) { string s = Console.ReadLine(); string t = Console.ReadLine(); s = String.Concat(s.OrderBy(c => c)); t = String.Concat(t.OrderByDescending(c => c)); Console.WriteLine(isAlphabetically(s,t) ? "Yes" : "No"); } static bool isAlphabetically(string s, string t) { bool res = false; if (s[0] < t[0]) { res = true; } else if (s[0] == t[0]) { isAlphabetically(s.Substring(1), t.Substring(1)); } return res; } }
追記
ACしたコード
using
1using System; 2 3class Program 4{ 5 static void Main(string[] args) 6 { 7 string s = Console.ReadLine(); 8 string t = Console.ReadLine(); 9 s = String.Concat(s.OrderBy(c => c)); 10 t = String.Concat(t.OrderByDescending(c => c)); 11 12 Console.WriteLine(isSortedAlphabetically(s,t) ? "Yes" : "No"); 13 } 14 15 static bool isSortedAlphabetically(string s, string t) 16 { 17 if (s[0] < t[0]) 18 { 19 return true; 20 } 21 else if (s[0] == t[0]) 22 { 23 if (s.Length == 1 && t.Length == 2) return true; 24 if (s.Length == 2 && t.Length == 1) return false; 25 if (s.Length == 2 && t.Length == 1) return false; 26 if (s.Length == 1 && t.Length == 1) return false; 27 return isSortedAlphabetically(s.Substring(1), t.Substring(1)); 28 } 29 else 30 { 31 return false; 32 } 33 } 34} 35
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。