c++中怎样提取数的每一位数字??

如题所述

好办,除10取余数
比如123,123%10就是3,然后123/10==12, 12%10就得到2, 然后再12/10,得到1,1%10得到1
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-05
用数组保存每一位,输入一个n位数
for(i=0;i<n,i++)
while(temp>0)
{
a[i]=m/10*10;
temp=temp-temp/10;
}
a数组存放的就是各位数字
第2个回答  2011-11-05
//直接用 string 储存 数字就可以了

#include <iostream>
#include <string>
using namespace std;

int main()
{
string num;
cin >> num;
for(string::size_type index = 0; index != num.size(); ++index){
cout << num[index] << " ";}
return 0;
}
相似回答