二十进制数的加法
发布日期:2021-05-09 04:22:29 浏览次数:18 分类:博客文章

本文共 2353 字,大约阅读时间需要 7 分钟。

题目详情

在二十进制中,我们除了使用数字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();        }        ///         /// 二十进制相加        ///         /// 字符串a        /// 字符串b.        /// 
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] == '\0') { h--; } string s = ""; while (h >= 0) { s += ar[h--]; } return s; } /// /// Maps the specified c. /// a -> 10 , j-> 19 , others exception /// /// The c. ///
///
c
static int Map(char c) { c = char.ToLower(c); if (c >= 'a' && c <= 'j') { return 10 + (c - 'a'); } throw new ArgumentException("c"); } /// ///Map int to char, 10 -> a , 19 -> j /// /// The i. ///
///
i
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"); } }

 

上一篇:回文数
下一篇:使用NuGet管理项目类库引用

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月08日 02时10分09秒