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

串上衣【难度:2级】:

答案1:

using System.Text;

public static class Kata
{
  public static string Tops(string msg)
  {
    StringBuilder result = new StringBuilder();
    
    for(int i = 1, x = 0; i < msg.Length; i += 5 + 4 * x, x++)
      result.Insert(0, msg[i]);
    
    return result.ToString();
  }
}

答案2:

using System;
using System.Linq;
using System.Collections.Generic;


public static class Kata
{
    public static string Tops(string msg)
    {
        int jump = 1;
        Stack<char> result = new Stack<char>() { };
        for (int i = 1; i < msg.Length; i += jump)
        {
            if (jump % 2 != 0)
                result.Push(msg[i]);
            jump += 1;
        }
        return String.Join("", result);
    }

  

}

答案3:

using System;
using System.Text;

public static class Kata
{
  public static string Tops(string msg)
  {
      int            adder     = 1;
      int            ithValue  = 1;
      bool           finshed   = false;
      StringBuilder  sb        = new StringBuilder();
      StringBuilder  bs        = new StringBuilder();
      if ( msg.Length ==0) 
      {
          return "";
      }
      while (!finshed) 
      {
          sb.Append(msg[ithValue]);   
          // get the next "top" value
          adder++;
          ithValue += adder;
          adder++;
          ithValue += adder;
          // dont go again if the next "top" is beyond string length
          if (ithValue >= msg.Length) 
          {
              finshed = true;
          }
      }
      
      //flip the string so were high to low
      for ( int i = sb.Length -1; i>= 0; i--) 
      {
          bs.Append(sb[i]);
      }
      return bs.ToString();
  }
}

答案4:

using System;
using System.Collections;

public static class Kata
{
  public static string Tops(string msg)
  {
//     Console.WriteLine("\nStart:\n" + msg);
    Stack stk = new Stack();
    string res = "";
    int top = -1;
    int count = 0;
    
    for(int i=0; i < msg.Length; i++) {
      if (i == 1) {
        stk.Push(msg[i]);
        count += 2;
        top = count * 2 + 1 + i;
//         Console.WriteLine("Res: " + res + "\tCount: " + count + "\tTop: " + top);
      } else if (top == i) {
        stk.Push(msg[i]);
        count += 2;
        top = count * 2 + 1 + i;
//         Console.WriteLine("Res: " + res + "\tCount: " + count + "\tTop: " + top);
      }
    }
    
    while(stk.Count != 0) {
      res += stk.Pop();
    }
//     Console.WriteLine("\nEnd:\n" + res);
    return res;
  }

}

答案5:

using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;

public static class Kata
{
  public static string Tops(string msg)
  {
    if(string.IsNullOrEmpty(msg))
      return msg;
    if(msg.Length < 3)
      return msg[msg.Length - 1].ToString();
    int idx0 = 0;
    int idx1 = 1;
    int idx2 = 1;
    var chs = new List<char>();
    while(idx2 < msg.Length){ 
      chs.Add(msg[idx2]);
      int tmp = idx2;      
      idx2 = idx1 + idx1 - idx0 + 4; 
      idx0 = idx1;
      idx1 = idx2;
    }
    return new string(chs.Select(x=>x).Reverse().ToArray());
    //throw new NotImplementedException();
  }
}

答案6:

using System;
using System.Linq;

public static class Kata
{
  public static string Tops(string msg)
        {
            string edges = "";

            for (int i = 1, k = 0; i + k < msg.Length; i += k)
            {               
                edges += msg[i + k];
                k++;
            }

            string peaks = "";

            for (int i = edges.Length - 1; i >= 0; i--)
                peaks += (i % 2 == 0) ? edges[i].ToString() : "";

            return peaks;
        }
}

答案7:

using System;
using System.Linq;
public static class Kata
{
  public static string Tops(string msg)
        {
            char[] msgarray = msg.ToCharArray();
            string result="";
            int k=0;
            int m=0;
            for (int i = 1; i < msg.Length; i=i+k+m)
            {
                m=5;
                Console.WriteLine(i);
                result=result+msgarray[i];
                if (i!=1)
                    k=k+4;
            }
            return string.Join("",result.Reverse());
        }        
}

答案8:

using System;

public static class Kata
{
    public static string Tops(string msg)
    {
        if(msg.Length == 0 || msg.Length == 1) return "";
        string val = "";
        for (int i = 1; i < msg.Length; i = i + 2)
        {
            try
            {
                val += msg[(i * (i + 1)) / 2];
            }
            catch (System.IndexOutOfRangeException)
            {
                break;
            }
        }
        char[] charArray = val.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }
}

答案9:

using System;
using System.Linq;

public static class Kata
{
    public static string Tops(string msg)
    {
        if (msg.Length == 0 )return msg;
        string val = "";
        int increment = 1;
        int index = 1;
        while (true)
        {
            try
            {
                if (increment % 2 == 0 )
                {
                    increment++;
                    index += increment;
                    
                }
                else
                {
                    val += msg[index];
                    increment++;
                    index += increment;
                }
            }
            catch(System.IndexOutOfRangeException)
            {
                break;
            }
        }
        char[] arr = val.ToCharArray();
        return String.Concat(arr.Reverse());
    }
}

答案10:

using System;
using System.Linq;
public static class Kata
{
  public static string Tops(string msg)
  {
     char[] c = msg.ToCharArray();
            string s = "";
            int p = 1;
            for (int i = 1,k=2; i < msg.Length;i= i+p)
            {
                if(k%2==0)
                    s += c[i];
                p++;
                k++;
            }
            return new string(s.Reverse().ToArray());
  }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值