C#练习题答案: 建筑用砖墙壁【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

mac2024-12-29  18

建筑用砖墙壁【难度:1级】:

答案1:

namespace Wall { public class Brick { public string CalculateBricksCount(int w, int h) { int l = 0, ms = 0; for (int i = 1; i <= h / 5; i++) { l += w / 60; for (int j = i + 1, k = 0; j <= h / 5 &amp;&amp; k < 2; i++, j++, k++) { ms += 2; l += (w / 60 - 1); } } return ms > 0 ? l + "L" + ms / 2 + "M" + ms / 2 + "S" : l + "L"; } } }

答案2:

namespace Wall { using System; public class Brick { public string CalculateBricksCount(int width, int height) { int lines = height / 5; int largeBrick = 0; int mediumBrick = 0; int smallBrick = 0; for (int i = 1; i <= lines; i ++) { if (i % 3 == 1) { largeBrick += width / 60; } else { mediumBrick += 1; smallBrick += 1; largeBrick += (width - 60) / 60 ; } } if (mediumBrick > 0) { return $"{largeBrick}L{mediumBrick}M{smallBrick}S"; } else { return $"{largeBrick}L"; } } } }

答案3:

namespace Wall { public class Brick { public string CalculateBricksCount(int width, int height) { int w = width / 60, h = height / 5, ms = h * 2 / 3, l = w * h - ms; return ms != 0 ? $"{l}L{ms}M{ms}S" : $"{l}L"; } } }

答案4:

namespace Wall { using System; public class Brick { public string CalculateBricksCount(int width, int height) { var h = height / 5; var w = width / 60; var l = 0; var m = 0; var s = 0; for(var i=0;i<h;i++) { if (i % 3 == 0) { l += w; } else { l += w - 1; m++; s++; } } var result = ""; if(l > 0) { result += l + "L"; } if(m > 0) { result += m + "M"; } if(s > 0) { result += s + "S"; } return result; } } }

答案5:

namespace Wall { public class Brick { public string CalculateBricksCount(int w, int h) { int e=h/5, f=e++/3+e/3, l=w/60*(++e/3+f)-f; return (l>0?l+"L":"")+(f>0?f+"M"+f+"S":""); } } }

答案6:

namespace Wall { using System; public class Brick { public string CalculateBricksCount(int width, int height) { var rows = height / 5; var cols = width / 60; var c = Math.Floor((decimal)rows * 2 / 3); return String.Format("{0}L",Math.Ceiling(cols * rows - c)) + (c>0 ? String.Format("{0}M{0}S", c) : String.Empty); } } }
最新回复(0)