C++to_string application example

Given a non-negative integer N, your task is to calculate the sum of all the digits of N and output each digit of the sum in English.

The input format is
one line and contains an integer N.

The output format is
one line, each number of the sum is output in English, and the words are separated by spaces.

code show as below:

#include <iostream>
#include <cstring>
using namespace std;
string d[] = {
    
    "zero","one","two","three","four","five","six","seven","eight","nine"};
int main()
{
    
    
    int sum = 0;
    string a;
    cin>>a;
    for (int i = 0;i<a.size();i++)
    {
    
    
        sum+=a[i]-'0';
    }
    string str = to_string(sum);
    for(int i = 0;i<str.size();i++)
    {
    
    
        cout<<d[str[i]-'0']<<" ";
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/114055206