C#练习题答案: 二进制排序【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

二进制排序【难度:1级】:

答案1:

using System;
using System.Linq;

public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1) 
    {
        if (!s1.Contains('0') || !s1.Contains('1'))
                return string.Empty;

        var result = string.Empty;

        s1.ToCharArray().Where(c => c == '0').Select(c => result += c).ToList();

        s1.ToCharArray().Where(c => c == '1').Select(c => result += c).ToList();

        return result;
    }
  }
}

答案2:

public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1) 
    {
      string outval = "";
      
      foreach(char c in s1){
        if(c == '0'){
          outval += '0';
        }
      }
      
      foreach(char c in s1){
        if(c == '1'){
          outval += '1';
        }
      }
    
      return outval;
    }
  }
}

答案3:


using System.Linq;

public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1) 
    {
    //code it!
      return string.Join("", s1.Where(x => x == '0' || x == '1').OrderBy(x => x));
    }
  }
}

答案4:


using System.Linq;

public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1) 
    {
    //code it!
      return string.Join("", s1.ToCharArray().Where(x => x == '0' || x == '1').OrderBy(x => x));
    }
  }
}

答案5:

using System;
using System.Linq;
public class Kata 
{  
  public class Account
  {
    public static string BinarySort(string s1) 
    {
      return String.Concat(string.Join("",s1.Where(e => e == '1' || e == '0')).OrderBy(c => c));
    }
  }
}

答案6:

using System;
using System.Linq;

public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1)
    {
        string binary = "";
        
        foreach (char c in s1)
        {
            if (c == '0')
            {
                binary += '0';
            }
            else if (c == '1')
            {
                binary += '1';
            }
        }
        
        return String.Concat(binary.OrderBy(x => x));
    }
    
  }
}

答案7:

using System.Linq;

public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1) 
    {      
      return new string(s1.Where(c => new[] {'0', '1'}.Contains(c)).OrderBy(c => c).ToArray());
    }
  }
}

答案8:

using System.Text.RegularExpressions;

public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1) 
    {
      return Regex.Replace(s1, "[^0]", "") + Regex.Replace(s1, "[^1]", "");
    }
  }
}

答案9:

public class Kata 
{
  public class Account
  {
     public static string BinarySort(string s1)
        {
            string left = "", right = "";
            for (int i = 0; i < s1.Length; i++)
            {
                if (s1[i] == '0') left += '0';
                else if (s1[i] == '1') right += '1';
            }
            return left + right; ;
        }
  }
}

答案10:

using System.Text.RegularExpressions;
using System;
using System.Linq;


public class Kata 
{
  public class Account
  {
    public static string BinarySort(string s1) 
    {
      Match match = Regex.Match(s1, @"([0-1]+)", RegexOptions.IgnoreCase);
      return match.Success ? String.Concat(match.Groups[1].Value.OrderBy(c => c)) : "";
    }
  }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值