C#练习题答案: 卫队良好的灵魂深渊【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

mac2024-11-11  16

卫队良好的灵魂深渊【难度:1级】:

答案1:

using System.Linq; public class Abyss { public static Gate Sort(string queue) { int gud = queue.Count(c=>c=='1'); return new Gate(new string('1', gud), new string('0', queue.Length - gud)); } }

答案2:

using System; public class Abyss { public static Gate Sort(string queue) { return new Gate(queue.Replace("0",""), queue.Replace("1","")); } }

答案3:

public class Abyss { public static Gate Sort(string queue, string left = "", string right = "") => queue == "" ? new Gate(left, right) : Sort(queue.Substring(1), queue[0] == '1' ? left + "1" : left, queue[0] == '0' ? right + "0" : right); }

答案4:

public class Abyss { public static Gate Sort(string queue) { return new Gate(queue.Replace("0", ""), queue.Replace("1", "")); } }

答案5:

using System; using System.Linq; public class Abyss { public static Gate Sort(string queue) { var left = String.Join("", queue.Where(q => q == '1').ToList()); var right = String.Join("", queue.Where(q => q == '0').ToList()); return new Gate(left, right); } }

答案6:

using System.Linq; public class Abyss { public static Gate Sort(string queue) { return new Gate(string.Concat(queue.Where(x => x == '1')), string.Concat(queue.Where(x => x == '0'))); } }

答案7:

public class Abyss { public static Gate Sort(string queue) { char[] chars = queue.ToCharArray(); string one="", zero=""; foreach(char c in chars) if(c == '1') one+="1"; else if (c == '0') zero+="0"; return new Gate(one, zero); } }

答案8:

public class Abyss { public static Gate Sort(string queue) { string one = ""; string zero = ""; foreach (char ch in queue) { if (ch == '0') zero += ch; else if (ch == '1') one += ch; } return new Gate(one, zero); } }

答案9:

using System; public class Abyss { public static Gate Sort(string queue, string left = "", string right = "") => queue == "" ? new Gate(left, right) : Sort(queue.Substring(1), _Left(queue[0], left), _Right(queue[0], right)); static string _LeftRight(char chr, string str, char dir) => chr == dir ? str + chr : str; static string _Left(char c, string s) => _LeftRight(c, s, '1'); static string _Right(char c, string s) => _LeftRight(c, s, '0'); }

答案10:

public class Abyss { public static Gate Sort(string queue) { var ones = ""; var zeros = ""; foreach (var item in queue) { if(item == '0') { zeros += '0'; } else { ones += '1'; } } return new Gate(ones, zeros); } }
最新回复(0)