이 문제에서는 두 개의 숫자가 제공되며 그 중 하나는 자릿수 배열을 사용하여 표시됩니다. 우리의 임무는 한 숫자가 숫자 배열로 표현되는 두 숫자의 합을 찾는 프로그램을 만드는 것입니다.
문제를 이해하기 위해 예를 들어 보겠습니다.
Input: n = 213, m[] = {1, 5, 8, }
Output: 371
Explanation: 213 + 158 = 371 이 문제를 해결하기 위해 우리는 단순히 배열의 어떤 요소를 숫자에서 숫자로 나타낼 것입니다. 배열의 (n-1)번째 요소에 숫자의 lsb가 추가됩니다. 캐리는 다음 금액으로 전달됩니다.
예시
솔루션의 작동을 설명하는 프로그램,
#include <iostream>
using namespace std;
void addNumbers(int n, int size, int *m){
int carry = 0;
int sum = 0;
for(int i = size-1; i >= 0; i--){
sum = (n%10) + m[i] + carry;
n /= 10;
carry = sum/10;
m[i] = sum%10;
}
}
int main() {
int n= 679;
int m[] = {1, 9, 5, 7, 1, 9};
int size = sizeof(m)/sizeof(m[0]);
cout<<"The sum of two numbers where one number is represented as array of digits is ";
addNumbers(n, size, m);
for(int i = 0; i < size; i++)
cout<<m[i];
} 출력
The sum of two numbers where one number is represented as array of digits is 196398