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实现人脸检测<转载>
    acm.njupt 1001-1026 简单题
    fedora 系统安装后常用设置
    一个普普通通的计算机研究生找工作的感悟
    一个简单的爬虫程序
    HDU 1005 Number Sequence
    【StatLearn】统计学习中knn算法的实验(1)
    SQL描述(2)
    连续点击返回键,退出应用程序
  • 原文地址:https://www.cnblogs.com/leonwang/p/3510211.html
Copyright © 2011-2022 走看看