zoj 2476

叮咚 2012-03-12 07:45:42
浙大平台的ACM题,wa了,但是找不出错误,望帮忙解决
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define N 10000

int myatof(char *s)
{
char *head=s;
char *p=s;

while(*s!='\0')
if(*s=='$' || *s==',' || *s=='.' ||*s=='\n')
s++;
else
*p++=*s++;
*p='\0';

return atoi(head);
}

void reverse(char *s)
{
char *p=strlen(s)-1+s;
char temp;

for(;s<p;s++,p--){
temp=*s;
*s=*p;
*p=temp;
}
}

void myatoi(int sum,char *s)
{
char *p=s;
int n;

n=sum%100;
sum/=100;
*s++ ='$';
do {
*s++ = sum%10+'0';
if((s-p)%4==0 && sum>10)
*s++=',';
}while((sum/=10));
*s='\0';
reverse(1+p);
*s++='.';
*s++=n/10+'0';
*s++=n%10+'0';
*s='\0';
}

main()
{
FILE *fp;
int sum,num;
char s[20];
int n,i;

fp=fopen("a.txt","r");
while(fscanf(fp,"%d",&n),n!=0) {
fgets(s,20,fp);
sum=0;
for(i=0;i<n;i++) {
fgets(s,20,fp);
num=myatof(s);
sum+=num;
}
myatoi(sum,s);
printf("%s\n",s);
}
return 0;
}
...全文
202 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
叮咚 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gqjjqg 的回复:]
引用 2 楼 abc225dingdong 的回复:

不能沉呀,有没有人看看呀


单个数字 20,000,000
你的代码至少得支持1000* 20,000,000的总和。
[/Quote]
不是吧!
All amounts and the total amount are between $0.00 and $20,000,000.00, inclusive
的意思不是每个数字和总和都在$0.00 and $20,000,000.00的闭区间吗?
叮咚 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gqjjqg 的回复:]
引用 2 楼 abc225dingdong 的回复:

不能沉呀,有没有人看看呀


单个数字 20,000,000
你的代码至少得支持1000* 20,000,000的总和。
[/Quote]

什么意思,到底哪错了
叮咚 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qixing1115 的回复:]
有什么提示错误,我的和示例结果一样,初步判断你文件存的位置可能有问题,要不你存在C盘试试,
然后这样:fp=fopen("c:\\a.txt","r");
[/Quote]
这个没错误,我写了好多都是这个形式,我提交的时候用的是fp=stdin;
qixing1115 2012-03-13
  • 打赏
  • 举报
回复
有什么提示错误,我的和示例结果一样,初步判断你文件存的位置可能有问题,要不你存在C盘试试,
然后这样:fp=fopen("c:\\a.txt","r");
gqjjqg 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 abc225dingdong 的回复:]

不能沉呀,有没有人看看呀
[/Quote]

单个数字 20,000,000
你的代码至少得支持1000* 20,000,000的总和。
叮咚 2012-03-13
  • 打赏
  • 举报
回复
可是我的哪里有错呀,我输出的结果和示例一样,但是还是wa啦
neofung 2012-03-13
  • 打赏
  • 举报
回复
怎么不能贴代码了

// ZOJ2476 Total Amount.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"
// #include "stdafx.h"
// #define DEBUG

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <memory.h>

#include <math.h>
#include <algorithm>
#include <numeric>
#include <functional>
#include <limits>
#include <assert.h>
#include <ctype.h>

#include <vector>
#include <stack>
#include <queue>
#include <deque>

using namespace std;

int main(void)
{
#ifdef DEBUG
freopen("data.txt","r",stdin);
freopen("result.txt","w",stdout);
#endif
char ch;
int n;
double sum;
char amount[20];
while(scanf("%d",&n)&&n)
{
sum=0.0;
getchar();
while(n--)
{
int ind=0;
getchar();
while(ch=getchar())
{
if(ch=='\n') break;
if(ch!=',')
amount[ind++]=ch;
}
amount[ind]='\0';
sum+=atof(amount);
}
sprintf(amount,"%0.2lf",sum);
int len=strlen(amount),ind=0,comma=0;
char result[30];
for(int i=len-1;i>=len-3;--i)
result[ind++]=amount[i];
for (int i=len-4;i>=0;--i)
{
result[ind++]=amount[i];
++comma;
if(comma%3==0 && i)
result[ind++]=',';
}
result[ind]='$';
for (int i=ind;i>=0;--i)
{
putchar(result[i]);
}
printf("\n");
}

return 0;
}
neofung 2012-03-13
  • 打赏
  • 举报
回复
注意整数部分是3的倍数的情况

这是我ac的代码

// ZOJ2476 Total Amount.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"
// #include "stdafx.h"
// #define DEBUG

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <memory.h>

#include <math.h>
#include <algorithm>
#include <numeric>
#include <functional>
#include <limits>
#include <assert.h>
#include <ctype.h>

#include <vector>
#include <stack>
#include <queue>
#include <deque>

using namespace std;

int main(void)
{
#ifdef DEBUG
freopen("data.txt","r",stdin);
freopen("result.txt","w",stdout);
#endif
char ch;
int n;
double sum;
char amount[20];
while(scanf("%d",&n)&&n)
{
sum=0.0;
getchar();
while(n--)
{
int ind=0;
getchar();
while(ch=getchar())
{
if(ch=='\n') break;
if(ch!=',')
amount[ind++]=ch;
}
amount[ind]='\0';
sum+=atof(amount);
}
sprintf(amount,"%0.2lf",sum);
int len=strlen(amount),ind=0,comma=0;
char result[30];
for(int i=len-1;i>=len-3;--i)
result[ind++]=amount[i];
for (int i=len-4;i>=0;--i)
{
result[ind++]=amount[i];
++comma;
if(comma%3==0 && i)
result[ind++]=',';
}
result[ind]='$';
for (int i=ind;i>=0;--i)
{
putchar(result[i]);
}
printf("\n");
}

return 0;
}
叮咚 2012-03-12
  • 打赏
  • 举报
回复
不能沉呀,有没有人看看呀
叮咚 2012-03-12
  • 打赏
  • 举报
回复

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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