Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _08.AddNumbersInCharArrays
- {
- class Program
- {
- /*
- Write a method that adds two positive integer numbers
- represented as arrays of digits
- (each array element arr[i] contains a digit;
- the last digit is kept in arr[0]).
- Each of the numbers that will be added could have up to 10 000 digits.
- */
- static void Main()
- {
- char [] firstNum = new char [3]{'9', '9', '9'};
- char[] secondNum = new char[2] { '9', '9' };
- Array.Reverse(firstNum);
- Array.Reverse(secondNum);
- int[] newArray = addNumbers(firstNum, secondNum);
- foreach (var charche in newArray)
- {
- Console.Write(charche + " ");
- }
- }
- static int[] addNumbers(char[] fisrtNum, char[] secondNum)
- {
- if (fisrtNum.Length > secondNum.Length)
- {
- int[] result = new int[fisrtNum.Length + 1];
- bool inMind = false;
- for (int i = 0; i < secondNum.Length; i++)
- {
- if (inMind == false)
- {
- if ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48 > 9)
- {
- inMind = true;
- result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48) - 10;
- }
- else
- {
- inMind = false;
- result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48);
- }
- }
- else
- {
- if ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48 + 1 > 9)
- {
- inMind = true;
- result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48) + 1 - 10;
- }
- else
- {
- inMind = false;
- result[i] = ((int)(fisrtNum[i]) - 48 + (int)(secondNum[i]) - 48 + 1);
- }
- }
- }
- return result;
- }
- else
- {
- int[] nullArr = new int[0];
- return nullArr;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement