计算纸张对折次数
纸张长度(mm): 宽度(mm): 厚度(mm):

核心代码


        /// <summary>
        /// 
        /// </summary>
        /// <param name="thickness">纸张厚度</param>
        /// <param name="width">纸张长度</param>
        /// <param name="height">纸张宽度</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DuiZhe(float thickness, float width, float height) {
            try
            {
                if (thickness<=0||width<=0||height<=0)
                {
                    throw new Exception($"请输入大于0的数。");
                }
                StringBuilder sb = new StringBuilder();
                sb.Append($"<div>纸张的长度:{width} mm</div>");
                sb.Append($"<div>纸张的宽度:{height} mm</div>");
                sb.Append($"<div>纸张的厚度:{thickness} mm</div>");
                float[] side = new float[] { width,height};
                float total = thickness; //存储总厚度,一开始的厚度就是纸张的厚度
                int count = 0; //对折次数
                float s; //对折边长度
                sb.Append($"<table class='table'><thead><tr><td>次数</td><td>对折边长</td><td>厚度</td></tr></thead>");
                sb.Append($"<tbody>");
                for (int i = 0; i < 2; i++)
                {
                    s = side[i];//取得边长
                    if (i == 0)
                    {
                        sb.Append($"<tr><td colspan='3'>从长度方向对折</td></tr>");
                    }
                    else {
                        sb.Append($"<tr><td colspan='3'>从宽度方向对折</td></tr>");
                    }
                    while (s>total)
                    {
                        count++;
                        s /= 2; //对折边长减半
                        total *= 2; //厚度加倍
                        sb.Append($"<tr><td>{count}</td><td>{s}</td><td>{total}</td></tr>");
                    }
                }
                sb.Append($"</tbody>");
                sb.Append($"</table>");

                return Json(new { code = 0, msg = sb.ToString() }) ;
            }
            catch (Exception ex)
            {
                return Json(new { code=1,msg="出错:"+ex.Message});
            }
        }