Given a number N, the task is to represent N as sum of minimum numbers of the form d000……, i.e, numbers having all digits as 0 except the first digit.
Examples :
Input : N = 1009 Output : 1000 9 Input : N = 9876 Output : 9000 800 70 6 Input : N = 10 Output : 10
Approach :
Extract digit of N one by one and multiply it by it’s position from left and store in a vector. At last print the vector.
Let the number be 9876 Extact digit of 9876 one by one 6 - > 6*1 = 6 7 - > 7*10 = 70 8 - > 8*100 = 800 9 - > 9*1000 = 9000 So 9876 = 9000 + 70 + 800 + 6
Implementation :
// C++ implementation to represent N as
// sum of minimum numbers of the form d000......
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
// Function to represent N as
// sum of minimum numbers of the form d000......
void findTerms(ll n)
{
// vector to store terms to represent N
vector<int> terms;
int power = 1;
while (n > 0)
{
int digit = n % 10;
if ( digit > 0)
{
terms.push_back(digit * power);
}
n /= 10;
power *= 10;
}
for (int i = 0; i < terms.size(); i++)
cout << terms[i] << " ";
}
// Driver Code
int main()
{
ll n = 9876;
findTerms(n);
return 0;
}