codeforces 143B Help Kingdom of Far Far Away 2
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#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;void dao(char a[]){ int i,n=strlen(a); char t; for(i=0; i<n/2; i++) { t=a[i]; a[i]=a[n-1-i]; a[n-1-i]=t; }}int main(){ char a[110],b[110],bb[220],c[10]; while(scanf("%s",a)!=EOF) { memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); memset(bb,0,sizeof(bb)); int i,j,n=strlen(a),tu=0; if(a[0]=='-') { printf("("); i=1; tu=1; } else i=0; for( j=0; i<n&&a[i]!='.'; i++) b[j++]=a[i]; if(i==n) { c[0]='0'; c[1]='0'; } else { strcpy(c,&a[i]+1); } int m=strlen(c); if(m==0) { c[0]='0'; c[1]='0'; } else if(m==1) c[1]='0'; printf("$"); dao(b); //添加“,”,这样做比较简单了 for(i=0,j=0; i<strlen(b); i++) { bb[j++]=b[i]; if((i+1)%3==0&&i!=strlen(b)-1) bb[j++]=','; } dao(bb); printf("%s",bb); //输出部分 printf(".%c%c",c[0],c[1]); if(tu) printf(")"); //用的tu判断是否为负数,开始用if(a[0]=='-'),测试数据都是对的,竟然过不了..... printf("\n"); } return 0;}