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

暗号【难度:1级】:

答案1:

using System;
using System.Linq;

public static class Kata
{
     
  public static string Encode(string s) => Cipher(s, i => i * 6);
  public static string Decode(string s) => Cipher(s, i => i / 6);
  private static string Cipher(string s, Func<int, int> f) => String.Concat(s.Select(c => (char)f(c)));
}

答案2:

using System;
using System.Linq;
public static class Kata
{
     
  public static string Encode(string str)
  {
     
    return string.Concat(str.Select(c => (char)(c * 6)));
  }
  
  public static string Decode(string str)
  {
     
    return string.Concat(str.Select(c => (char)(c / 6)));
  }
}

答案3:

using System;

public static class Kata
{
     
  public static string Encode(string str)
  {
     
    char[] res = new char[str.Length];
    for(int i=0;i<str.Length;i++)
    {
     
      res[i] = Convert.ToChar((int)str[i]*6);
    }
    return new string(res);
  }
  
  public static string Decode(string str)
  {
     
    char[] res = new char[str.Length];
    for(int i=0;i<str.Length;i++)
    {
     
      res[i] = Convert.ToChar((int)str[i]/6);
    }
    return new string(res);
  }
}

答案4:

using System;
using System.Linq;

public static class Kata
{
     
  public static string Encode(string str)
  {
     
    return string.Join(string.Empty, str.Select(c => (char)(c * 6)));
  }
  
  public static string Decode(string str)
  {
     
    return string.Join(string.Empty, str.Select(c => (char)(c / 6)));
  }
}

答案5:

using System;

public static class Kata
{
     
    public static string Encode(string str)
    {
     
        var muilti = "";
        
        foreach (var val in str)
        {
     
            muilti += (char)(val * 6);
        }
        return muilti;
    }

    public static string Decode(string str)
    {
     
        var muilti = "";

        foreach (var val in str)
        {
     
            muilti += (char)(val / 6);
        }

        return muilti;
    }
}

答案6:

using System.Linq;

public static class Kata {
     
    public static string Encode( string str ) {
     
        return new string( str.Select( c => ( char ) ( c*6 ) ).ToArray( ) );
    }

    public static string Decode( string str ) {
     
        return new string( str.Select( c => ( char ) ( c/6 ) ).ToArray( ) );
    }
}

答案7:

using System;
using System.Linq;

public static class Kata
{
     
  public static string Encode(string str)
  {
     
    return string.Join("", str.Select(c => (char)((int)c * 6)));
  }
  
  public static string Decode(string str)
  {
     
    return string.Join("", str.Select(c => (char)((int)c / 6)));
  }
}

答案8:

using System;
using System.Linq;

public static class Kata
{
     
  public static string Encode(string str)
  {
     
    return String.Concat(str.Select(x => (char)(x*6)));
  }
  
  public static string Decode(string str)
  {
     
    return String.Concat(str.Select(x => (char)(x/6)));
  }
}

答案9:

using System;
using System.Text;

public static class Kata
{
     
  public static string Encode(string str)
  {
     
    StringBuilder sb = new StringBuilder();
    
    foreach (char c in str)  sb.Append((char)(c*6));
    
    return sb.ToString();
  }
  
  public static string Decode(string str)
  {
     
    StringBuilder sb = new StringBuilder();
    
    foreach (char c in str)  sb.Append((char)(c/6));
  
    return sb.ToString();
  }
}

答案10:

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

public static class Kata
{
     
  public static string Encode(string str)
  {
     
    Console.WriteLine("enc: " + str);
    var codes = "";
    
    foreach(var c in str) {
     
      codes += Encoding.Unicode.GetString(BitConverter.GetBytes(6*c))[0];
    }
    Console.WriteLine(codes);
    return codes;
  }
  
  public static string Decode(string str)
  {
     
    Console.WriteLine("dec: " + str);
    var codes = "";
    foreach(var c in str) {
     
      codes += Encoding.Unicode.GetString(BitConverter.GetBytes(c/6))[0];
    }
    Console.WriteLine(codes);
    return codes;
  }
}

你可能感兴趣的:(C#编程高级练习,csharp面试题库和答案,csharp编程练习,算法,逻辑,加密算法)