| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:加了简单注释的In Reply To:加了简单注释的 Posted by:swuroyalty at 2011-03-22 19:13:04 表示代码太长了。说说的我做法吧,枚举搜索了所有情况,三个结构体,best存储最优情况,now用来搜索,init_node,用来初始化结构体,结构体直接赋值很方便。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
int stamp[26];
int sum;
int type;
int maxv;
int num;
bool tie;
};
struct node best, init_node;
int stamp[26];
int cus;
int ns;
int search(struct node now);
int output();
int compare(struct node now);
int main()
{
struct node now;
memset(init_node.stamp, 0, sizeof(init_node.stamp));
init_node.sum = init_node.num = init_node.type = init_node.maxv = 0;
init_node.tie = false;
ns = 0;
while(scanf("%d", &stamp[++ns]) != EOF)
{
while(scanf("%d", &stamp[++ns]), stamp[ns] != 0);
while(scanf("%d", &cus), cus != 0)
{
now = init_node;
best = init_node;
search(now);
output();
}
ns = 0;
}
return 0;
}
int search(struct node now)
{
int i;
struct node tmp;
tmp = now;
if(now.num == 4)
return 0;
for(i = 1; i < ns; ++i)
{
if(now.sum + stamp[i] > cus)
continue;
if(now.sum + stamp[i] == cus)
{
now.sum += stamp[i];
if(now.stamp[i] == 0)
++now.type;
++now.stamp[i];
if(stamp[i] > now.maxv)
now.maxv = stamp[i];
++now.num;
compare(now);
now = tmp;
continue;
}
if(now.sum + stamp[i] < cus)
{
now.sum += stamp[i];
if(now.stamp[i] == 0)
++now.type;
++now.stamp[i];
if(stamp[i] > now.maxv)
now.maxv = stamp[i];
++now.num;
search(now);
now = tmp;
continue;
}
}
return 0;
}
int compare(struct node now)
{
if(now.type > best.type)
{
best = now;
best.tie = false;
}
else if(now.type == best.type)
{
if(now.num < best.num)
{
best = now;
best.tie = false;
}
else if(now.num == best.num)
{
if(now.maxv > best.maxv)
{
best = now;
best.tie = false;
}
else if(now.maxv == best.maxv)
{
bool flag = true;
for(int i = 1; i < 26; ++i)
if(now.stamp[i] != best.stamp[i])
{
flag = false;
break;
}
if(flag == false)
best.tie = true;
}
}
}
return 0;
}
int output()
{
int i;
if(best.type == 0)
printf("%d ---- none\n", cus);
else
{
printf("%d (%d):", cus, best.type);
if(best.tie == true)
printf(" tie\n");
else
{
for(i = 1; i < 26; ++i)
while(best.stamp[i])
{
printf(" %d", stamp[i]);
--best.stamp[i];
}
printf("\n");
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator