C#练习题答案: 完成该模式#5 - 即使梯子【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

完成该模式#5 - 即使梯子【难度:1级】:

答案1:

public class EvenLadder
{
  public static string Pattern(int n)
  {
    string pattern = "";
    for (int i = 2; i <= n; i += 2)
    {
        for (int j = 0; j < i; j ++) pattern += i;
        if (i < n - 1) pattern += "\n";
    }
    return pattern;
  }
}

答案2:

using System;

public class EvenLadder
{
  public static string Pattern(int n)
  {
            string html = "";

            for (var i = 2; i <= n; i += 2)
            {
                for (var j = 0; j < i; j++)
                {
                    html += i;
                }

                if (i + 2 <= n)
                {
                    html += "\n";
                }
            }


            Console.WriteLine(html);

            return html;
  }
}

答案3:

using System.Linq;

public class EvenLadder
{
    public static string Pattern(int n)
    {
        if (n <= 1)
        {
            return string.Empty;
        }
        
        n = n%2 == 1 ? n - 1 : n;
    
        return string.Join("\n",
            Enumerable.Range(1, n)
                      .Where(e => e%2 == 0)
                      .Select(e => string.Concat(Enumerable.Repeat(e, e))));
    }
}

答案4:

using System.Linq;

public class EvenLadder
{
  public static string Pattern(int n)
  {
    if(n <= 1)
    {
      return "";
    }
    
    var output="";
    for(var i = 1; i <= n; i++)
    {
      if(i % 2 == 0)
      {
        output += string.Concat(Enumerable.Range(0, i).Select((a,b) => i));
    
        output += '\n';
      }
    }
    
    return output.Substring(0, output.Length - 1);
  }
}

答案5:

public class EvenLadder
{
  public static string Pattern(int n)
  {
        string strPattern = "";
        for (int i = 2; i <=n; i=i+2)
        {
            for (int j = 0; j < i; j++)
            {
                strPattern = strPattern + i;
            }
            if (i != n || i!=n-1 )
            {
                strPattern = strPattern + "\n";
            }
            
        }
        return strPattern == "" ?"":strPattern.Remove(strPattern.Length-1); 
  }
}

答案6:

using System;
using static System.Linq.Enumerable;
public class EvenLadder
{
    public static string Pattern(int n) =>
        n < 2 ? "" : string.Join("\n", Range(1, n % 2 == 0 ? n / 2 : (n - 1) / 2).Select(x => string.Join("", Repeat((x * 2).ToString(), x * 2))));
}

答案7:

using System.Linq;

public class EvenLadder
{
  public static string Pattern(int n)
  {
    if (n < 2) return string.Empty;
    return string.Join('\n', Enumerable.Range(1, n / 2).Select(i => string.Concat(Enumerable.Repeat(i * 2, i * 2))));
  }
}

答案8:

using System.Linq;

public class EvenLadder
{
  public static string Pattern(int n)
  {
    return n>0 ? string.Join('\n', Enumerable.Range(1, (n%2==0 ? n : n-1)/2).Select(x => string.Concat(Enumerable.Repeat(x*2, x*2)))) : "";
  }
}

答案9:

using System.Text;

public class EvenLadder {

    private const string NewLine = "\n";

    public static string Pattern( int n ) {
        if ( n <= 1 ) {
            return "";
        }
        var lines = new string[n/2];
        var j = 2;
        for ( int i = 0; i < n/2; i++ ) {
            var line = new StringBuilder( );
            for ( int k = 0; k < j; k++ ) {
                line.Append( j );
            }
            lines [ i ] = line.ToString( );
            j += 2;
        }
        return string.Join( NewLine, lines );
    }
}

答案10:

using System;
using System.Collections.Generic;

public class EvenLadder
{
  public static string Pattern(int n)
  {
    List<string> res = new List<string> {};
    for (int i = 2; i <= n; i += 2) {
      res.Add(new String('x', i).Replace("x", Convert.ToString(i)));
    }
    return String.Join('\n', res);
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值