Number separation (2) time limit: 1000 MS | memory limit: 65535 kb difficulty: 3
-
Description
-
In a distant country, banks decide to separate a string of numbers according to certain rules to process users' orders more quickly and better. The separation rules are as follows:
1. The integer part of a real number is separated by commas (,). (if there is an excess of 0 in the upper part of an integer, filter the excess 0 before separating the numbers, for example, the output result of 0001234567 is 1,234,567.00)
2. two decimal places are retained (rounded)
3. If this number is negative, brackets are used to enclose the separated numbers in the output. For example, the output result of-10005.1645 is (10,005.16)
-
Input
-
Multiple groups of test data, input a real number N per line (N digits less than 100)
-
Output
-
Output results after separation
-
Sample Input
-
00012345670.0000-10005.1645
-
Sample output
-
1,234,567.000.00(10,005.16)
-
Uploaded
-
ACM _ sun Xiaoyang
# Include <stdio. h> # include <string. h> # define maxn 110 char Buf [maxn]; int main () {int sign, left, Dot, I, Len; char * Ch; while (scanf ("% s", BUF + 1 )! = EOF) {Sign = 0; Buf [0] = '0'; If (BUF [1] = '-') {Sign = 1; buf [1] = '0';} Len = strlen (BUF); If (CH = strchr (BUF ,'. ') {dot = CH-Buf;} else dot = Len; // The following process carry if (LEN-dot> 3) {If (BUF [Dot + 3]> '4') {If (++ Buf [Dot + 2]> '9 ') {Buf [Dot + 2] = '0'; If (++ Buf [Dot + 1]> '9') {Buf [Dot + 1] = '0 '; + + Buf [Dot-1] ;}} I = dot-1; while (BUF [I]> '9') {Buf [I --] = '0 '; ++ Buf [I] ;}} Buf [Dot + 3] = '\ 0 ';} Else if (LEN-dot = 2) {Buf [Len ++] = '0'; Buf [Len] = '\ 0 ';} else if (LEN-dot = 1) {Buf [Len ++] = '0'; Buf [Len ++] = '0 '; buf [Len] = '\ 0';} else if (LEN-dot = 0) {Buf [Len ++] = '. '; Buf [Len ++] = '0'; Buf [Len ++] = '0'; Buf [Len] =' \ 0 ';} // printf ("% s... \ n ", Buf); // The following goes to the leading zero for (Left = 0; Buf [left] = '0'; ++ left ); // output integer part if (BUF [left] = '. ') -- left; If (sign) putchar ('); While (left <D Ot) {putchar (BUF [left ++]); If (left! = Dot & (Dot-left) % 3 = 0) putchar (',') ;}// output the fractional part printf ("% s", BUF + dot ); if (sign) putchar ('); putchar (' \ n');} return 0 ;}
Separate nyoj1092 numbers (2)