
二十进制数的加法
发布日期: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"); } }
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年04月08日 02时10分09秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
C语言的数值溢出问题(上)
2021-05-08
vue项目通过vue.config.js配置文件进行proxy反向代理跨域
2021-05-08
android:使用audiotrack 类播放wav文件
2021-05-08
ACM/NCPC2016 C Card Hand Sorting(upc 3028)
2021-05-08
SLAM学习笔记-求解视觉SLAM问题
2021-05-08
程序员应该知道的97件事
2021-05-08
shell编程(六)语言编码规范之(变量)
2021-05-08
vimscript学习笔记(二)预备知识
2021-05-08
Android数据库
2021-05-08
HTML基础,块级元素/行内元素/行内块元素辨析【2分钟掌握】
2021-05-08
23种设计模式一:单例模式
2021-05-08
spring启动错误:Could not resolve placeholder
2021-05-08
invalid byte sequence for encoding
2021-05-08
技术美术面试问题整理
2021-05-08
ORB-SLAM2:LoopClosing线程学习随笔【李哈哈:看看总有收获篇】
2021-05-08
js求阶乘
2021-05-08
python-day3 for语句完整使用
2021-05-08
基于LabVIEW的入门指南
2021-05-08