COTH002 - Editorial

PROBLEM LINK:

Contest

Practice

Author: Shubham Jain

Tester: Shubham Jain

Editorialist: Shubham Jain

DIFFICULTY:

Medium

PREREQUISITES:

Basic Programming, String

PROBLEM:

A child needs help to learn counting. As a professional software engineer, it should be your responsibility to help others through your software. You need to take input in digits and print the equivalent word representation for the number in American name.

EXPLANATION:

According to the question, we have to convert the given number into words up-to 18 digits, i.e., numbers from 0 to 10^18. To solve this problem, we need to create arrays that store individual parts of output strings. One array is used for single digits, one for numbers from 10 to 19, one for 20, 30, 40, 50, … etc, and one for illion’s names.


The given number is first checked for zero. If it is 0, then print zero.


If the number is not equal to 0, then set the length of number to multiple of 3 by adding prefix as 0 so that it will be easy to convert the number into words.


Now convert the first three digits into words and the check for illion’s name by dividing the length by 3 and then convert the next three digits into words and then check for illion’s name by dividing the remaining length by 3 and so on.

AUTHOR’s SOLUTION:


    #include "iostream"
    #include "string"
    using namespace std;

    int main()
    {
        string num;
        string onesName[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        string teensName[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        string tensName[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        string illionName[] = { "million", "billion", "trillion", "quadrillion", "quintillion"};
    
        char repeat = 'n';
        int t;
        cin >> t;
        while(t--){   
	    cin >> num; 
        
            if(num.size() == 1 && num[0] == '0'){
                cout << "zero" << endl;
                continue;
            }
        
            while( num.size()%3 != 0 )
                num = '0' + num;
 
            for( int i = 0; i < num.size(); i += 3 ){
                if( num[i] == '0' && num[i+1] == '0' && num[i+2] == '0' )
                    continue;
 
                if( num[i] > '0' )
                    cout << onesName[ num[i] - '0' - 1 ] << " hundred ";
 
                if( num[i + 1] == '0' || num[i + 1] > '1' ){
                    if( num[i + 1] > '1' ) cout << tensName[ num[i + 1] - '0' - 2 ] << " ";
                    if( num[i + 2] > '0' ) cout << onesName[ num[i + 2] - '0' - 1 ] << " ";
                }
                else
                    cout << teensName[ num[i + 2] - '0' ] << " ";
 
                int j = ( num.size() - i )/3;
                if( j == 2 ) cout << "thousand ";
                else if( j > 2 )
                    cout << illionName[ j - 3 ] << " ";
            }
            cout << endl; 
        }
        return 0;
     }