日历
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|
统计
- 随笔 - 30
- 文章 - 0
- 评论 - 51
- 引用 - 0
导航
常用链接
留言簿(4)
随笔分类
随笔档案
ACM
搜索
最新评论

阅读排行榜
评论排行榜
|
这几天一直在修改这道题,一直都改不对,老是错误,提交不成功,我把自己的代码贴出来让大家帮忙看看,在这里谢过了!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int result[4];
int reNumber, reCount, tie, reMax,stampNumber, customerNumber, count[100]; //result是最终客户的邮票种类,reCount是客户邮票总个数,reNumber是客户不同邮票的个数
//stampNumber是邮票的个数,customerNumber是客户个数
int GetNumber(int *stamp, int *customer) //获取邮票 和 客户信息。
  {
int i, n;
n = 0;
stampNumber = 0;
memset(count, 0 ,sizeof(int)*100);
while(1)
 {
if(scanf("%d", &n) == EOF)
return -1;
if(n == 0)
break;
if(count[n]++ < 5)
stamp[stampNumber++] = n;
}
stampNumber--;
customerNumber = 0;
while(1)
 {
scanf("%d", &n);
if(n == 0)
break;
customer[customerNumber++] = n;
}
customerNumber--;
return 1;
}
int NotSame(int *number,int count, int *m,int *stamp) //求不同一组邮票类别的个数和邮票的最大面值。
  {
int i,j, c,s;
c = 0;
*m = stamp[number[0]];
for(i = 0; i < count; i++)
 {
if( *m < stamp[number[i]])
*m = stamp[number[i]];
s = 0;
for(j = 0; j < i; j++)
 {
if(number[i] == number [j])
 {
s = 1;
break;
}
}
if(0 == s)
c++;
}
return c;
}


void Divide(int sum, int *number, int *stamp,int n, int *count, int same,int start)
  {
int i;
int t;
if( *count > 4 )
return;
else if( sum == 0 && *count <= 4)
 {
same = NotSame(number, *count,&t, stamp);
if( same > reNumber || same == reNumber && reCount > *count || same == reNumber && reCount == *count && reMax < t )
 {
reMax = t;
reCount = *count;
reNumber = same;
for(i = 0; i < *count; i++)
result[i] = number[i];
tie = 0;
}
else if(same == reNumber && reCount == *count && reMax == t)
 {
tie = 1;
}

return;
}
for(i = start; i < n; i++)
 {
sum -= stamp[i];
if(sum >= 0)
 {
number[(*count)++] = i;
Divide(sum, number, stamp, n, count,same,i);
(*count)--;
}
sum += stamp[i];
}
}


int main(int argc, char* argv[])
  {
int stamp[100], customer[100];
int number[4];
int count;
int i,j;
do
 {
memset(stamp, 0, 100*sizeof(int));
memset(customer, 0, 100*sizeof(int));
memset(number, 0 ,4);
if(GetNumber(stamp, customer) == -1)
break;
for(i = 0; i <= customerNumber; i++)
 {
reMax = -1;
memset(result, 0, 4);
reNumber = -1;
count=0;
tie = 0;
Divide(customer[i], number,stamp, stampNumber+1,&count, -1,0);
if( (reNumber != -1) && (tie == 0)) //打印结果。
 {
printf("%d (%d):", customer[i], reNumber);
for(j = 0; j < reCount; j++)
printf(" %d",stamp[result[j]]);
printf("\n");
}
else if((reNumber != -1) && (tie == 1))
 {
printf("%d (%d): tie\n",customer[i], reNumber);
}
else
 {
printf("%d ---- none\n",customer[i]);
}
}
}while(1);
return 0;
}
评论:
-
# re: 求助程序
Posted @ 2008-06-29 18:06
我这里可以运行? vs2008 回复 更多评论
-
# re: 求助程序
Posted @ 2008-06-29 18:18
谢谢你!
程序可以运行,但是提交的时候,提示错误,不知道哪里写的不对。 回复 更多评论
-
# re: 求助程序
Posted @ 2008-06-29 23:56
把程序的注释多加些,别人才好帮你看,也方便自己看。
整个程序都没有对输入数据进行合法性检查。 整个程序都没有对缓冲区的溢出进行检查。 全局变量满天飞。
回复 更多评论
-
# re: 求助程序
Posted @ 2008-06-30 11:05
谢谢你的批评,我会尽快改掉这些坏习惯! 回复 更多评论
-
# re: 求助程序
Posted @ 2008-06-30 14:12
看起来有点难受! 回复 更多评论
-
# re: 求助程序
Posted @ 2008-07-01 09:36
哈哈,终于提交成功了!我重新写了一篇,请大家指正!
http://www.cppblog.com/Leon916/archive/2008/07/01/55006.html 回复 更多评论
|