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

建筑用砖墙壁【难度: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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值