C++输出顺序问题

如图,输入123后,通过求模、去个位,分离每一位数字。但输出呈321,有什么便捷的办法,让输出呈1 2 3?
再问一下,怎么求一个不知道位数的整数,各位上的数之和?

第1个回答  2016-10-15

可以把每位数用数组暂存,再反向输出。

#include <iostream>
using namespace std;
int main()
{
    const char *str[]={"yi","er","san","si","wu","liu","qi","ba","jiu","ling"};
    int arr[0x10];
    int n,count=0;
    cin>>n;
    do
    {
        arr[count++]=n%10;
        n/=10;
    }while(n);
    for (int i=count-1;i>=0;i--)
        arr[i] ? cout<<str[arr[i]-1]<<" " : cout<<"ling ";
    return 0;
}

第2个回答  2016-10-15

求不知位数的整数各位之和——

//#include "stdafx.h"//If the vc++6.0, with this line.
#include <iostream>
using namespace std;
int main(void){
    int n,i,sum;
    while(1){
        cout << "Input an integer...\n";
        if(cin >> n){
            for(sum=0;n;sum+=n%10,n/=10);
            cout << "The result is " << sum << endl;
            break;
        }
        cout << "Error, redo: ";
        cin.clear();
        cin.ignore();
    }
    return 0;
}

本回答被网友采纳
第3个回答  2016-10-14
使用 char aaa[][] 字符串存储,然后反过来输出

~追问

求解,把temp换成char型?
反过来输出应该怎么写?

相似回答