这个题目是我面试的时候的一道算法题,当时手写的差不多了,但是直到昨天晚上,我才想起来要以真实程序的方式把它写出来。题目的意思就是两个整数相 加,然后输出结果,大家可能再想,这个问题,还不是很简单吗?定义两个int32位的变量,然后相加输出结果,如果考虑到数据的大,int32不行的话, 就用int64 ,double,decimal型的。但是这个不是题目的本意,也不符合出题人的初衷,而且题目明确要求,数字的大小是无限的,大到任何一个c#的数值类型都无法可以表示这个数字,那在这样的情况下,首先先考虑如何来表示这两个无限大的数字,在我的算法中是用整型数组来表示的。
具体的代码如下:
首先我们定义一个名为NumsAdd.cs的类,负责两数相加。
public int[] Cal(int[] a, int[] b)
{
IList<int> list = new List<int>();
if (a == null && b == null)
{
return null;
}
if (a == null && b != null)
{
return b;
}
if (a != null && b == null)
{
return a;
}
int lena = a.Length - 1;
int lenb = b.Length - 1;
int toke = 0;
Int32 temp = 0;
while (lena >= 0 && lenb >= 0)
{
temp = 0;
temp = a[lena] + b[lenb] + toke;
toke = RecodeTokeAndRecodeResult(list, toke, temp);
lena--;
lenb--;
}
while (lena >= 0)
{
temp = 0;
temp = a[lena] + toke ;
toke = RecodeTokeAndRecodeResult(list, toke, temp);
lena--;
}
while (lenb >= 0)
{
temp = 0;
temp = b[lenb] + toke;
toke = RecodeTokeAndRecodeResult(list, toke, temp);
lenb--;
}
if (toke == 1)
{
list.Add(toke);
}
for (int i = list.Count - 1; i > 0; i--)
{
if (list[i] != 0)
{
break;
}
else
{
list.Remove(list[i]);
}
}
return list.ToArray<Int32>().Reverse<int>().ToArray<int>();
}
private int RecodeTokeAndRecodeResult(IList<int> list, int toke, Int32 temp)
{
if (temp >= 10)
{
toke = 1;
list.Add(temp - toke * 10);
}
else
{
toke = 0;
list.Add(temp);
}
return toke;
}
}
数字相加的原理,我想大家都是知道的,从个位数开始加起,大于等于10的话,要进位。
调用的代码
int[] a = { 0,1 };
int[] b = { 0 };
NumsAdd add = new NumsAdd();
int[] result = add.Cal(a,b);
foreach (int item in result )
{
Console.Write(item);
}
Console.ReadLine();
然后这次我还给出了测试的代码(用的是VS自带的测试)
[TestMethod()]
public void CalTest()
{
NumsAdd target = new NumsAdd(); // TODO: Initialize to an appropriate value
int[] a = null; // TODO: Initialize to an appropriate value
int[] b = null; // TODO: Initialize to an appropriate value
a = null;
b = null;
Assert.AreEqual(null, target.Cal(a, b));
a = null;
b=new int[3]{1,2,4};
Assert.AreEqual(b, target.Cal(a, b));
a = new int[3];
b = null;
Assert.AreEqual(a,target.Cal(a,b));
a = new int[1]{0};
b = new int[1]{0};
Assert.AreEqual("0", numsToString(target.Cal(a, b)));
a = new int[1] { 1 };
b = new int[1] { 0 };
Assert.AreEqual("1", numsToString(target.Cal(a, b)));
a = new int[1] { 1 };
b = new int[1] { 3 };
Assert.AreEqual("4", numsToString(target.Cal(a, b)));
a = new int[1] { 1 };
b = new int[1] { 9 };
Assert.AreEqual("10", numsToString(target.Cal(a, b)));
a = new int[2] { 1,0 };
b = new int[1] { 9 };
Assert.AreEqual("19", numsToString(target.Cal(a, b)));
a = new int[2] { 1, 5 };
b = new int[1] { 8 };
Assert.AreEqual("23", numsToString(target.Cal(a, b)));
a = new int[2] { 1, 6 };
b = new int[2] { 9,9 };
Assert.AreEqual("115", numsToString(target.Cal(a, b)));
a = new int[3] { 9, 9,9 };
b = new int[3] { 9, 9,9 };
Assert.AreEqual("1998", numsToString(target.Cal(a, b)));
a = new int[3] { 0, 0, 0 };
b = new int[3] { 0, 0, 0 };
Assert.AreEqual("0", numsToString(target.Cal(a, b)));
a = new int[3] { 0, 0, 0 };
b = new int[3] { 0, 0, 1 };
Assert.AreEqual("1", numsToString(target.Cal(a, b)));
a = new int[3] { 0, 0, 0 };
b = new int[2] { 0, 0 };
Assert.AreEqual("0", numsToString(target.Cal(a, b)));
a = new int[3] { 0, 9, 0 };
b = new int[2] { 0, 0 };
Assert.AreEqual("9", numsToString(target.Cal(a, b)));
}
public String numsToString(int[] result)
{
string str = string.Empty;
foreach (int item in result)
{
str += item.ToString();
}
return str;
}