B. Help Kingdom of Far Far Away 2
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer.
The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows:
A number contains the integer part and the fractional part. The two parts are separated with a character "." (decimal point).
To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character "," (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678
In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests).
When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets.
Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency — snakes ($), that's why right before the number in the financial format we should put the sign "$". If the number should be written in the brackets, then the snake sign should also be inside the brackets.
For example, by the above given rules number 2012 will be stored in the financial format as "$2,012.00" and number -12345678.9 will be stored as "($12,345,678.90)".
The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them?
Input
The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100characters, including (possible) signs "-" (minus) and "." (decimal point). The number's notation is correct, that is:
The number's notation only contains characters from the set {"0" – "9", "-", "."}.
The decimal point (if it is present) is unique and is preceded and followed by a non-zero quantity on decimal digits
A number cannot start with digit 0, except for a case when its whole integer part equals zero (in this case the integer parts is guaranteed to be a single zero: "0").
The minus sign (if it is present) is unique and stands in the very beginning of the number's notation
If a number is identically equal to 0 (that is, if it is written as, for example, "0" or "0.000"), than it is not preceded by the minus sign.
The input data contains no spaces.
The number's notation contains at least one decimal digit.
Output
Print the number given in the input in the financial format by the rules described in the problem statement.
Sample test(s)
input
2012
output
$2,012.00input
0.000
output
$0.00input
-0.00987654321
output
($0.00)input
-12345678.9
output
($12,345,678.90)題意:輸入一個數,先判斷是正數還是負數。
1、如果是正數:先輸出一個$,判斷如果是整數,從個位開始每三個數之前加一個',' ,最前面不加;如果是小數,整數部分處理方式同整數,小數部分保留2為小數,如果小數位數大於2,則把多余的部分捨棄;
2、如果是負數,負號‘-’不輸出,用()代替,其余部分同正數處理方式相同;
代碼比較長,但是好理解:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { char s[104]; int i,j,k,n,m,len,t; while(gets(s)!=NULL) { len=strlen(s); if(s[0]!='-') { printf("$"); if(strchr(s,'.')==NULL) { k=len%3; if(k==0) { for(i=0;i<=2;i++) printf("%c",s[i]); for(i=3,j=0;i<len;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } } else { for(i=0;i<=k-1;i++) printf("%c",s[i]); for(i=k,j=0;i<len;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } } printf(".00"); } else { for(i=0;i<len;i++) if(s[i]=='.') { m=i; break; } k=m%3; if(k==0) { for(i=0;i<=2;i++) printf("%c",s[i]); for(i=3,j=0;i<m;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } printf("."); n=len-m-1; if(n>=2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } else { for(i=0;i<=k-1;i++) printf("%c",s[i]); for(i=k,j=0;i<m;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } printf("."); n=len-m-1; if(n>=2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } } } else { printf("($"); if(strchr(s,'.')==NULL) { k=(len-1)%3; if(k==0) { for(i=1;i<=3;i++) printf("%c",s[i]); for(i=4,j=0;i<len;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } } else { for(i=1;i<=k;i++) printf("%c",s[i]); for(i=k+1,j=0;i<len;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } } printf(".00"); } else { for(i=0;i<len;i++) if(s[i]=='.') { m=i; break; } k=(m-1)%3; if(k==0) { for(i=1;i<=3;i++) printf("%c",s[i]); for(i=4,j=0;i<m;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } printf("."); n=len-m-1; if(n>=2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } else { for(i=1;i<=k;i++) printf("%c",s[i]); for(i=k+1,j=0;i<m;i++) { if(j%3==0) printf(","); printf("%c",s[i]); j++; } printf("."); n=len-m-1; if(n>=2) printf("%c%c",s[m+1],s[m+2]); else if(n==1) printf("%c0",s[m+1]); } } printf(")"); } printf("\n"); } return 0; }