zoukankan      html  css  js  c++  java
  • 二十进制数的加法

    题目详情

    在二十进制中,我们除了使用数字0-9以外,还使用字母a-j(表示10-19),给定两个二十进制整数,求它们的和。

    输入是两个二十进制整数,且都大于0,不超过100位;

    输出是它们的和(二十进制),且不包含首0。我们用字符串来表示二十进制整数。

    class Program
        {
            static void Main(string[] args)
            {
                string s = Sum("abc", "abc"); // 1134
    
                Console.WriteLine(s);
                Console.Read();
            }
    
            /// <summary>
            /// 二十进制相加
            /// </summary>
            /// <param name="a">字符串a</param>
            /// <param name="b">字符串b.</param>
            /// <returns></returns>
            static string Sum(string a, string b)
            {
                int len = a.Length > b.Length ? a.Length + 1 : b.Length + 1;
    
                char[] ar = new char[len];
                int i = 0;
    
                int ai = a.Length - 1;
                int bi = b.Length - 1;
                int t;
                int ad = 0;
    
                while (ai >= 0 || bi >= 0)
                {
                    if (ai >= 0 && bi >= 0)
                    {
                        t = Map(a[ai]) + Map(b[bi]) + ad;
                    }
                    else if (ai >= 0)
                    {
                        t = Map(a[ai]) + ad;
                    }
                    else
                    {
                        t = Map(b[bi]) + ad;
                    }
    
                    ar[i++] = RMap(t % 20);
                    ad = t / 20;
    
                    ai--;
                    bi--;
                }
    
    
                if (ad > 0)
                {
                    ar[i] = '1';
                }
    
                int h = ar.Length - 1;
    
                while (ar[h] == '')
                {
                    h--;
                }
    
                string s = "";
    
                while (h >= 0)
                {
                    s += ar[h--];
                }
    
    
                return s;
            }
    
            /// <summary>
            /// Maps the specified c.
            /// a -> 10 , j-> 19 , others exception
            /// </summary>
            /// <param name="c">The c.</param>
            /// <returns></returns>
            /// <exception cref="System.ArgumentException">c</exception>
            static int Map(char c)
            {
                c = char.ToLower(c);
    
                if (c >= 'a' && c <= 'j')
                {
                    return 10 + (c - 'a');
                }
    
                throw new ArgumentException("c");
            }
    
            /// <summary>
            ///Map int to char, 10 -> a , 19 -> j
            /// </summary>
            /// <param name="i">The i.</param>
            /// <returns></returns>
            /// <exception cref="System.ArgumentException">i</exception>
            static char RMap(int i)
            {
                if (i >= 10 && i <= 19)
                {
                    return Convert.ToChar(87 + i);
                }
                else if (i < 10)
                {
                    return i.ToString()[0];
                }
    
                throw new ArgumentException("i");
            }
        }
  • 相关阅读:
    python 调用dll 动态链接库 结构体参数及回调函数等示例
    逆向x64-small-trick
    Malloc碎碎念
    关于OF和CF
    月赛-Crackhash
    ARM64调试环境
    Apache Kafka(六)- High Throughput Producer
    Apache Kafka(五)- Safe Kafka Producer
    Apache Kafka(四)- 使用 Java 访问 Kafka
    Apache Kafka(三)- Kakfa CLI 使用
  • 原文地址:https://www.cnblogs.com/leonwang/p/3510211.html
Copyright © 2011-2022 走看看