注册 登录
编程论坛 C++教室

一个大数问题(数字分离)

红糖水 发布于 2013-10-17 12:46, 471 次点击
数字分隔
时间限制:1000 ms  |  内存限制:65535 KB
描述
如果有一个很大的数字576876912,为了知道它怎么读,

于是我们会一位一位地数。。。这样很麻烦,如果把这个数

每三位用逗号分隔写成 576,876,912

这样读起来就显得方便多了。

输入
多组测试数据,每一行就一个正整数m(0<=m<2^31)
输出
输出分隔的结果
样例输入
123
12345
576876912
样例输出
123
12,345
576,876,912
6 回复
#2
wp2319572013-10-17 13:01
当字符处理
#3
红糖水2013-10-17 13:31
回复 2楼 wp231957
可用字符处理也没有这么大啊
#4
rjsp2013-10-17 13:50
先试试看
程序代码:
#include <iostream>
#include <locale>
using namespace std;

int main( void )
{
    cout.imbue( locale("chs") );

    cout << 123 << '\n';
    cout << 12345 << '\n';
    cout << 576876912 << '\n';

    return 0;
}

#5
rjsp2013-10-17 13:52
如果不行的话,或者不想依赖已有的某个locale的话,试试如下代码
程序代码:
#include <iostream>
#include <string>
#include <locale>
using namespace std;

class thousands_sep_facet : public std::numpunct<char>
{
public:
    explicit thousands_sep_facet( size_t r=0 ) : std::numpunct<char>(r)
    {
    }
protected:
    string do_grouping() const
    {
        return "\003";
    }
};

int main( void )
{
    locale loc( locale(), new thousands_sep_facet );
    std::cout.imbue( loc );

    cout << 123 << '\n';
    cout << 12345 << '\n';
    cout << 576876912 << '\n';

    return 0;
}

#6
红糖水2013-10-17 16:41
回复 5楼 rjsp
要从就键盘输入啊 ,郁闷
#7
红糖水2013-10-20 17:49
最后还是自己写出来了
1