C#练习题答案: 象棋乐趣#10:国际象棋加密【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客提供了10个不同答案来解决C#中的国际象棋加密问题,适合C#编程练习和面试准备。难度级别为2级,包含一系列解题思路。
摘要由CSDN通过智能技术生成

象棋乐趣#10:国际象棋加密【难度:2级】:

答案1:

namespace myjinxin
{
    using System;    
    public class Kata
    {
        public string ChessEncryption(string msg)
        {
            string chess = "v11u21t31s41r51q61p71o81w12x22y32z42k52l62m72n82a43j53b44i54c45h55d46g56e47f57";
            string res = "";
            foreach(char ele in msg)
            {
                if (ele == ' ') res += ' ';
                else
                {
                    string temp = chess.Substring(chess.IndexOf(ele) + 1, 2);
                    char a = (char)(temp[0] - 48 + 96);
                    char b = temp.Substring(1)[0];
                    res += (a + "" + b);
                }
            }
            return res;
        }
    }
}

答案2:



namespace myjinxin
{
    using System;
    using System.Collections.Generic;
using System.Linq;
using System.Text;
    
    public class Kata
    {
        public string ChessEncryption(string msg){
          if (string.IsNullOrEmpty(msg))
                return null;
            var dictionary = new Dictionary<char,string>();
            var lettre = 'd';
            var chiffre = 3;
            char charInt = 'a';
            string node;
            for (int i = 0; i < 26; i++)
            {
                node = lettre + chiffre.ToString();
                dictionary.Add(Convert.ToChar(charInt),node);
                if (charInt == 'o')
                {
                    //chiffre = 1;
                    //chiffre--;
                    lettre--;
                    charInt++;
                    continue;
                }
                if (charInt == 'w')
                {
                   // chiffre = 2;
                    lettre++;
                    charInt++;
                    continue;
                }
                if (charInt == 'f')
                {
                    // chiffre = 2;
                    
                    chiffre--;
                    charInt++;
                    continue;
                }
                if (chiffre >= 3 &amp;&amp; chiffre < 7 &amp;&amp; charInt <= 'e')
                {
                    chiffre++;
                    charInt++;
                    continue;
                }
                if (chiffre > 2 &amp;&amp; chiffre <= 7 &amp;&amp; charInt > 'f')
                {
                    chiffre--;
                    charInt++;
                    continue;
                }
                if (chiffre == 7)
                {
                    lettre++;
                    charInt++;
                    continue;
                }
                if (chiffre == 2 &amp;&amp; charInt <= 'n')
                {
                    if (charInt == 'n')
                    {
                        chiffre--;

                    }
                    else
                    {
                        lettre++;
                        
                    }
                    charInt++;
                    continue;
                }
                if (chiffre == 1 &amp;&amp; charInt <= 'v')
                {
                    if (charInt == 'v')
                    {
                        chiffre++;
                    }
                    else
                    {
                        lettre--;
                    }
                    
                    charInt++;
                    continue;
                }
                if (chiffre == 2 &amp;&amp; charInt < 'z')
                {
                    lettre++;
                    charInt++;
                    continue;
                }
            }
            dictionary.Add(' ', " ");
            var result = new StringBuilder();
            foreach (var charMsg in msg.ToCharArray())
            {
                result.Append(dictionary[charMsg]);
            }
            return result.ToString();

          
          
        }
    }
}

答案3:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    
    public class Kata
    {
        public string ChessEncryption(string msg){
          
          Dictionary<char, string> dict = new Dictionary<char, string>()
          {
            {'a', "d3"},
            {'b', "d4"},
            {'c', "d5"},
            {'d', "d6"},
            {'e', "d7"},
            {'f', "e7"},
            {'g', "e6"},
            {'h', "e5"},
            {'i', "e4"},
            {'j', "e3"},
            {'k', "e2"},
            {'l', "f2"},
            {'m', "g2"},
            {'n', "h2"},
            {'o', "h1"},
            {'p', "g1"},
            {'q', "f1"},
            {'r', "e1"},
            {'s', "d1"},
            {'t', "c1"},
            {'u', "b1"},
            {'v', "a1"},
            {'w', "a2"},
            {'x', "b2"},
            {'y', "c2"},
            {'z', "d2"}
          };
          
          List<string> result = new List<string>();
          
          for (int i = 0; i < msg.Length; i++)
          {
            if (dict.ContainsKey(msg[i]))
              result.Add(dict[msg[i]]);
            else
              result.Add(msg[i].ToString());
          }
          
          return string.Join("", result);
        }
    }
}

答案4:

namespace myjinxin {
    using System.Collections.Generic;
    using System.Text;

    public class Kata {

        private static Dictionary<char, string> _map = new Dictionary<char, string> {
            {'a', "d3"},
            {'b', "d4"},
            {'c', "d5"},
            {'d', "d6"},
            {'e', "d7"},
            {'f', "e7"},
            {'g', "e6"},
            {'h', "e5"},
            {'i', "e4"},
            {'j', "e3"},
            {'k', "e2"},
            {'l', "f2"},
            {'m', "g2"},
            {'n', "h2"},
            {'o', "h1"},
            {'p', "g1"},
            {'q', "f1"},
            {'r', "e1"},
            {'s', "d1"},
            {'t', "c1"},
            {'u', "b1"},
            {'v', "a1"},
            {'w', "a2"},
            {'x', "b2"},
            {'y', "c2"},
            {'z', "d2"}
        };


        public string ChessEncryption( string msg ) {
            var message = new StringBuilder( msg.Length*2 );
            foreach ( var c in msg ) {
                var l = char.ToLower( c );
                if ( _map.ContainsKey( l ) ) {
                    message.Append( _map [ l ] );
                }
                else {
                    message.Append( c );
                }
            }
            return message.ToString( );
        }
    }
}

答案5:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public string ChessEncryption(string msg){
          char[][] letters = new char[][] {
            new char[2] {'v', 'w' },
            new char[2] {'u', 'x' },
            new char[2] {'t', 'y' },
            new char[7] {'s', 'z', 'a', 'b', 'c', 'd', 'e' },
            new char[7] {'r', 'k', 'j', 'i', 'h', 'g', 'f' },
            new char[2] {'q', 'l' },
            new char[2] {'p', 'm' },
            new char[2] {'o', 'n' }
          };
          string retstring = "";
          foreach (char c in msg) {
            if (c == ' ') retstring += " ";
            else {
              for (int col = 0; col < 8; col++) {
                for (int row = 0; row < letters[col].Length; row++) {
                  if (c == letters[col][row]) retstring += ((char) (col + (int) 'a')).ToString() + ((char) (row + (int) '1')).ToString();
                }
              }
            }
          }
          return retstring;
        }
    }
}

答案6:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
using System.Linq;

    public class Kata
    {
        public readonly Dictionary<char,string> Data = new Dictionary<char,string>
        {
            {'a',"d3"}, {'b',"d4"},{'c',"d5"},{'d',"d6"},{'e',"d7"},
            { 'f',"e7"},{'g',"e6"},{'h',"e5"},{'i',"e4"},{'j',"e3"},
            { 'k',"e2"},{'l',"f2"},{'m',"g2"},{'n',"h2"},{'o',"h1"},
            { 'p',"g1"},{'q',"f1"},{'r',"e1"},{'s',"d1"},{'t',"c1"},
            { 'u',"b1"},{'v',"a1"},{'w',"a2"},{'x',"b2"},{'y',"c2"},
            { 'z',"d2"}, {' '," "}
        };

        public string ChessEncryption(string msg)
        {
            //coding and coding..
            return string.Concat(msg.Select(p => Data[p]));
        }
    }
}

答案7:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    public class Kata
    {
        public string ChessEncryption(string msg){
          Dictionary<char, string> letters = new Dictionary<char, string>()
          {
            {'a', "d3"}, {'b', "d4"},{'c', "d5"}, {'d', "d6"},{'e', "d7"},
            {'f', "e7"}, {'g', "e6"},{'h', "e5"}, {'i', "e4"},{'j', "e3"},
            {'k', "e2"}, {'l', "f2"},{'m', "g2"}, {'n', "h2"},{'o', "h1"},
            {'p', "g1"}, {'q', "f1"},{'r', "e1"}, {'s', "d1"},{'t', "c1"},
            {'u', "b1"}, {'v', "a1"},{'w', "a2"}, {'x', "b2"},{'y', "c2"},{'z',"d2"}, {' '," "}
          };
          
          StringBuilder builder = new StringBuilder();
          
          foreach(char s in msg){
            builder.Append(letters[s]);
          }
          return builder.ToString();
        }
    }
}

答案8:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public string ChessEncryption(string msg){
          string result = "";
          string[] encrypted = new string[] { "d3", "d4", "d5", "d6", "d7", "e7", "e6", "e5", "e4", "e3", "e2", "f2", "g2", "h2", "h1", "g1", "f1", "e1", "d1", "c1", "b1", "a1", "a2", "b2", "c2", "d2"};
          
          for(int i = 0; i < msg.Length; i++) {
            if(msg[i] >= 97 &amp;&amp; msg[i] <= 122) {
              result += encrypted[msg[i] - 97];
            }
            else { result += msg[i]; }
          }
          return result;
        }
    }
}

答案9:

namespace myjinxin
{
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    public class Kata
    {
        public string ChessEncryption(string msg)
        {
            var dict = new Dictionary<string,string>
            {
                {"v","a1"},{"u","b1"},{"t","c1"},{"s","d1"},{"r","e1"},{"q","f1"},{"p","g1"},{"o","h1"},
                {"w","a2"},{"x","b2"},{"y","c2"},{"z","d2"},{"k","e2"},{"l","f2"},{"m","g2"},{"n","h2"},
                {"a","d3"},{"j","e3"},{"b","d4"},{"i","e4"},{"c","d5"},{"h","e5"},{"d","d6"},{"g","e6"},
                {"e","d7"},{"f","e7"}
            };
            return Regex.Replace(msg, @"[a-z]", x => dict[x.Value]);
        }
    }
}

答案10:

namespace myjinxin
{
    using System;
    using System.Text;
    
    public class Kata
    {
        public string ChessEncryption(string msg){
          //coding and coding..
          string[] codeword = new string[26]{"d3","d4","d5","d6","d7","e7","e6","e5","e4","e3","e2","f2","g2","h2","h1","g1","f1","e1","d1","c1","b1","a1","a2","b2","c2","d2"};
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < msg.Length; i++)
            {
                if (msg[i].ToString() != " ")
                {
                    sb.Append(codeword[Convert.ToInt32(msg[i]) - 97]);
                }
                else
                {
                    sb.Append(" ");
                }
            }
            return sb.ToString();
          
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值