C#练习题答案: BasE91编码和解码【难度:4级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客主要探讨了C#中BasE91编码和解码的实践,提供了10个不同解题思路和解决方案,适合C#初学者及进阶者提升编码技能和逻辑思维。
摘要由CSDN通过智能技术生成

BasE91编码和解码【难度:4级】:

答案1:

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

class Base91
{
   
         public static string Decode ( string input )
        {
   

            string stringTable ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&amp;()*+,./:;<=>?@[]^_`{|}~\"";
          Dictionary<byte, int> stringDecode= new Dictionary<byte, int>();
       for ( int i = 0; i < stringTable. Length; i++ )
            {
   
                stringDecode [ ( byte )stringTable [ i ] ] = i;
            }
            string output = "";
            int c = 0;
            int v = -1;
            int b = 0;
            int n = 0;
            for ( int i = 0; i < input. Length; i++ )
            {
   
                c = stringDecode [ ( byte )input [ i ] ];
                if ( c == -1 ) continue;
                if ( v < 0 )
                {
   
                    v = c;
                }
                else
                {
   
                    v += c * 91;
                    b |= v << n;
                    n += ( v &amp; 8191 ) > 88 ? 13 : 14;
                    do
                    {
   
                        output += ( char )( b &amp; 255 );
                        b >>= 8;
                        n -= 8;
                    } while ( n > 7 );
                    v = -1;
                }
            }
            if ( v + 1 != 0 )
            {
   
                output += ( char )( ( b | v << n ) &amp; 255 );
            }
            return output;
        }
        public static string Encode ( string s )
        {
   
            string output = "";
            int b = 0;
            int n = 0;
            int v = 0;
            string stringTable ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&amp;()*+,./:;<=>?@[]^_`{|}~\"'";
            for ( int i = 0; i < s. Length; i++ )
            {
   
                b |= ( byte )s [ i ] << n;
                n += 8;
                if ( n > 13 )
                {
   
                    v = b &amp; 8191;
                    if ( v > 88 )
                    {
   
                        b >>= 13;
                        n -= 13;
                    }
                    else
                    {
   
                        v = b &amp; 16383;
                        b >>= 14;
                        n -= 14;
                    }
                    output += ( char )stringTable [ v % 91 ];
                    output += ( char )stringTable [ v / 91 ];
                }
            }
            if ( n != 0 )
            {
   
                output += ( char )stringTable [ b % 91 ];
                if ( n > 7 || b > 90 )
                    output += ( char )stringTable [ b / 91 ];
            }
            return output;
        }
}

答案2:

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

class Base91
{
   
    private static readonly char[] _encodeTable = new char[]
    {
   
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$',
    '%', '&amp;', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=',
    '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"'
    };

    private static readonly Dictionary<byte, int> _decodeTable = new Dictionary<byte, int>();

    static Base91()
    {
   
        InitDecodeTable();
    }

    private static void InitDecodeTable()
    {
   
        for (byte i = 0; i < 255; i++)
            _decodeTable[i] = -1;
        for (int i = 0; i < _encodeTable.Length; i++)
            _decodeTable[(byte)_encodeTable[i]] = i;
    }

    public static string Encode(string input)
    {
   
        string output = "";
        int b = 0;
        int n = 0;
        for (int i = 0; i < input.Length; i++)
        {
   
            b |= (byte)input[i] << n;
            n += 8;
            if (n > 13)
            {
   
                int v = b &amp; 8191;
                if (v > 88)
                {
   
                    b >>= 13;
                    n -= 13;
                }
                else
                {
   
                    v = b &amp; 16383;
                    b >>= 14;
                    n -= 14;
                }
                output += _encodeTable[v % 91];
                output += _encodeTable[v / 91];
            }
        }
        if (n != 0)
        {
   
            output += _encodeTable[b % 91];
            if (n > 7 || b > 90)
                output += _encodeTable[b / 91];
        }
        return output;
    }

    public static string Decode(string input)
    {
   
        string output = "";
        int v = -1;
        int b = 0;
        int n = 0;
        for (int i = 0; i < input.Length; i++)
        {
   
            int c = _decodeTable[(byte)input[i]];
            if (c == -1) continue;
            if (v < 0)
            {
   
                v = c;
            }
            else
            {
   
                v += c * 91;
                b |= v << n;
                n += (v &amp; 8191) > 88 ? 13 : 14;
                do
                {
   
                    output += (char)(b &amp; 255);
                    b >>= 8;
                    n -= 8;
                } while 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值