C#练习题答案: ■□模式□■:波【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

mac2025-02-27  3

■□模式□■:波【难度:2级】:

答案1:

using System.Linq; public class Kata { public static string Draw(int[] waves) { return string.Join("\n", Enumerable.Range(0, waves.Max()) .Select(i => string.Concat(waves.Select(w => w >= i + 1 ? '■' : '□'))) .Reverse()); } }

答案2:

using System.Linq; public class Kata { private const char White = '\u25a0'; private const char Black = '\u25a1'; public static string Draw(int[] waves) { int maxHeight = waves.Max(); var lines = Enumerable.Range(0, maxHeight).Select(depth => new string( waves.Select(height => height >= maxHeight - depth ? White : Black).ToArray())); return string.Join("\n", lines); } }

答案3:

using System; using System.Linq; public class Kata { public static string Draw(int[] waves) { var max = waves.Max(); string[] result = new string[max]; for (var r = 0; r < max; r++) { for (var i = 0; i < waves.Length; i++) { result[r] += (waves[i] >= max - r) ? '■' : '□'; } } return String.Join("\n", result); } }

答案4:

using System; using System.Linq; public class Kata { public static string Draw(int[] waves) { char a = '■', b = '□'; int maks = waves.Max(); int l = waves.Length; string[] temp = new string[l]; string res = ""; for(int i = 0; i < l; i++) temp[i] = new string(b, maks - waves[i]) + new string(a, waves[i]); for(int i = 0; i < maks; i++) { for(int j = 0; j < l; j++) res += temp[j][i]; res += '\n'; } return res.TrimEnd('\n'); } }

答案5:

using System; using System.Collections.Generic; public class Kata { public static string Draw(int[] waves) { // Get max wave value int max = 0; foreach(int val in waves) if(val > max) max = val; // Save each wave line string wavesStr = ""; for(int i = max; i > 0; i--) wavesStr += GetWave(waves, i); return wavesStr; } private static string GetWave(int[] waves, int threshold) { string waveStr = ""; foreach(int wave in waves) waveStr += wave >= threshold ? "■" : "□"; if(threshold > 1) waveStr += "\n"; return waveStr; } }

答案6:

using System.Text; public class Kata { public static string Draw(int[] waves) { int rows = int.MinValue; for (int i = 0; i < waves.Length; i++) if (waves[i] > rows) rows = waves[i]; StringBuilder sb = new StringBuilder(); for (int i = rows - 1; i >= 0; i--) { for (int j = 0; j < waves.Length; j++) sb.Append(waves[j] >= (i + 1) ? "■" : "□"); if (i != 0) sb.Append("\n"); } return sb.ToString(); } }

答案7:

using System; public class Kata { private static char blank = '□'; private static char sound = '■'; public static string Draw(int[] waves) { int waveMax = GetArrayMax(waves); return CreateWaves(waves, waveMax); } private static int GetArrayMax(int[] array) { int max = 0; for (int i = 0; i < array.Length; i++) if (max < array[i]) max = array[i]; return max; } private static string CreateWaves(int [] waves, int waveMax) { string [] returnWaves = new string [waves.Length]; string ret = ""; for (int soundLevel = 0; soundLevel < waveMax; soundLevel++) { for (int wave = 0; wave < waves.Length; wave++) { ret += waves[wave] >= waveMax - soundLevel ? sound : blank; } if (soundLevel < waveMax - 1) ret += '\n'; } return ret; } }

答案8:

using System; public class Kata { private static char blank = '□'; private static char sound = '■'; public static string Draw(int[] waves) { int waveMax = GetArrayMax(waves); return CreateWaves(waves, waveMax); } private static int GetArrayMax(int[] array) { int max = 0; for (int i = 0; i < array.Length; i++) if (max < array[i]) max = array[i]; return max; } private static string CreateWaves(int [] waves, int waveMax) { string [] returnWaves = new string [waves.Length]; for (int i = 0; i < waves.Length; i++) { returnWaves[i] = CreateWave(waves[i], waveMax); } string ret = ""; for (int soundLevel = waveMax - 1; soundLevel >= 0; soundLevel--) { for (int wave = 0; wave < waves.Length; wave++) { ret += returnWaves[wave][soundLevel]; } if (soundLevel > 0) ret += '\n'; } return ret; } private static string CreateWave(int soundLevel, int soundMax) { return new string(sound, soundLevel) + new string(blank, soundMax - soundLevel); } }

答案9:

using System.Linq; public class Kata { public static string Draw(int[] waves) { int rows = waves.Max(); int columns = waves.Count(); char off = '□'; char on = '■'; string s = ""; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { s += rows - i <= waves[j] ? on : off; } s += i < rows - 1 ? "\n" : ""; } return s; } }

答案10:

using System.Collections.Generic; using System.Linq; using System; public class Kata { public static string Draw(int[] waves) { List<int> intList = waves.ToList<int>(); int nbLigne = intList.Max(); String result = ""; for (int i = nbLigne; i > 0 ; i--) { foreach (int num in intList) { if (num >= i) result += "■"; else result += "□"; } result += "\n"; } return result.Substring(0, result.Length - 1); } }
最新回复(0)