Ajouter deux grands nombres
public static string AddTwoNumber(string x, string y)
{
var X = x.ToCharArray().ToList().Select(s => Convert.ToInt32(s.ToString())).ToList();
var Y = y.ToCharArray().ToList().Select(s => Convert.ToInt32(s.ToString())).ToList();
X.Reverse();
Y.Reverse();
if (X.Count > Y.Count)
{
return Add(X, Y);
}
else
{
return Add(Y, X);
}
}
public static string Add(List<int> first, List<int> second)
{
var remainder = 0;
var list = new List<int>();
for (int i = 0; i < first.Count; i++)
{
var temp = 0;
if(i<second.Count)
{
temp = second[i];
}
var num = temp + first[i] + remainder;
var inString = num.ToString();
if (inString.Length > 1)
{
list.Add(Convert.ToInt32(inString[1].ToString()));
remainder = Convert.ToInt32(inString[0].ToString());
}
else
{
list.Add(Convert.ToInt32(inString[0].ToString()));
remainder = 0;
}
}
list.Reverse();
var str = string.Empty;
foreach (var item in list)
{
str += item;
}
return str;
}
PrashantUnity