1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-09 11:09:07 +01:00
CodeWars/4kyu/adding_big_numbers/solution.cs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

44 lines
1.1 KiB
C#

using System;
public class Kata {
public static (uint remainder, uint digit) SumTwo(char a, char b, uint c = 0) {
uint val_a = (uint) a - 48;
uint val_b = (uint) b - 48;
var sum = val_a + val_b + c;
return (sum / 10, sum % 10);
}
public static string Add(string a, string b) {
var result = "";
if (a.Length > b.Length) {
var temp = a;
a = b;
b = temp;
}
int idx_a = a.Length - 1;
int idx_b = b.Length - 1;
uint remainder = 0;
uint last_digit = 0;
while (idx_a >= 0) {
(remainder, last_digit) = SumTwo(a[idx_a], b[idx_b], remainder);
result = last_digit.ToString() + result;
idx_a--;
idx_b--;
}
while (idx_b >= 0) {
(remainder, last_digit) = SumTwo('0', b[idx_b], remainder);
result = last_digit.ToString() + result;
idx_b--;
}
if (remainder > 0) {
result = remainder.ToString() + result;
}
return result;
}
}