Codeforces Round #102 (Div. 2) ——B. Help Kingdom of Far Far Away 2

唉~~~格式处理题,水题啊。。。。。。

细节还是很重要的

View Code
 1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4
5 int main()
6 {
7 char data[110];
8 char newdata[200];
9 for (int i(0); i<110; ++i)data[i] = '0';
10 cin>>data;
11 newdata[0] = '$';
12 bool flag = false;
13 int cnt = strlen(data);
14 int i(0),j(1);
15 if (data[i] == '-')++i;
16 for (int k = i; k<strlen(data); ++k) {
17 if (data[k] == '.') {
18 flag = true;
19 cnt = k;
20 break;
21 }
22 }
23 if (cnt-i+1 > 1) {处理大于10的数
24 int CNT;
25 CNT = (cnt - i)%3;
26 if (CNT == 0)CNT = 3;
27 for (; i<cnt; ++i) {
28 if (data[i] <= '9' && data[i] >= '0') {
29 newdata[j++] = data[i];
30 --CNT;
31 }
32 if (!CNT) {
33 if (i+3 >= cnt)CNT = 100;
34 else {
35 CNT = 3;
36 newdata[j++] = ',';
37 }
38 }
39 }
40 newdata[j++] = '.';
41 newdata[j++] = data[i+1] == '\0'?'0':data[i+1];
42 newdata[j++] = data[i+2] == '\0'?'0':data[i+2];
43 } else {//处理小于10的数
44 for (int k = 0; k < 4; ++k){
45 newdata[j++] = data[i++];
46 if (newdata[j-1] == '\0')newdata[j-1] = '0';
47 }
48 }
49 newdata[j] = '\0';
50 if(data[0] == '-')cout<<"("<<newdata<<")"<<endl;
51 else cout<<newdata<<endl;
52 return 0;
53 }



转载于:https://www.cnblogs.com/devtang/archive/2012/01/14/2322407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值