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 }



posted on 2012-01-14 15:20  Dev-T  阅读(218)  评论(0编辑  收藏  举报