zoj 2476 Total Amount

DSQ_17 2014-05-28 01:33:41
据说是个水题,不过还是总WA。。求大神给瞄一眼。
题目描述如下:
Total Amount
Time Limit: 2 Seconds Memory Limit: 65536 KB
Given a list of monetary amounts in a standard format, please calculate the total amount.


We define the format as follows:


1. The amount starts with '$'.


2. The amount could have a leading '0' if and only if it is less then 1.


3. The amount ends with a decimal point and exactly 2 following digits.


4. The digits to the left of the decimal point are separated into groups of three by commas (a group of one or two digits may appear on the left).




Input


The input consists of multiple tests. The first line of each test contains an integer N (1 <= N <= 10000) which indicates the number of amounts. The next N lines contain N amounts. All amounts and the total amount are between $0.00 and $20,000,000.00, inclusive. N=0 denotes the end of input.




Output


For each input test, output the total amount.




Sample Input


2
$1,234,567.89
$9,876,543.21
3
$0.01
$0.10
$1.00
0




Sample Output


$11,111,111.10
$1.11

问题代码如下:
#include <iostream>
using namespace std;


int main()
{
int n,totalLeft,totalRight,dotLeft,dotRight;
char ch;
while(cin>>n&&n!=0)
{
totalLeft=0;
totalRight=0;
for(;n>0;n--)
{
cin>>ch;
int sum=0;
while(ch!='.')
{
cin>>dotLeft;
sum+=dotLeft;
cin>>ch;
if(ch==',')sum*=1000;
}
totalLeft+=sum;
cin>>dotRight;
totalRight+=dotRight;


}
totalLeft+=totalRight/100;
cout<<"$";
int baseNum=10000000,numOfZero=7,flag=0;

while(numOfZero)
{
if(flag==0&&totalLeft/baseNum)
{
cout<<totalLeft/baseNum;
flag=1;
}
else if(flag){
if((numOfZero+1)%3==0)cout<<',';
cout<<totalLeft/baseNum;
}
totalLeft%=baseNum;
baseNum/=10;
numOfZero--;
}
cout<<totalLeft<<'.';
totalRight%=100;
if(totalRight==0)cout<<"00"<<endl;
else cout<<totalRight<<endl;




}

//int sum=0;



return 0;
}



...全文
97 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
DSQ_17 2014-06-02
  • 打赏
  • 举报
回复
引用 1 楼 longburulin 的回复:
自己逻辑有错误!测试例子都不对 还想不WA?
请您说得详细些,谢谢您! 另外,我这儿用vs2010测试给的例子,结果是正确的。 您能不吝赐教,万分感激
赵4老师 2014-05-28
  • 打赏
  • 举报
回复
仅供参考
char * comma(__int64 num) {
    static char commades[80];
    static char commasrc[80];

    sprintf(commasrc,"%I64d",num);
    int L=strlen(commasrc);
    register int j=0;
    register int k=0;
    for (int i=L-1;i>=0;i--) {
        commades[j]=commasrc[i];
        k++;
        j++;
        if ((k%3)==0 && i>0) {
            commades[j]=',';
            j++;
        }
    }
    commades[j]=0;
    strcpy(commades,strrev(commades));
    return commades;
}
longburulin 2014-05-28
  • 打赏
  • 举报
回复
自己逻辑有错误!测试例子都不对 还想不WA?

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧