| ||||||||||
| 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 | |||||||||
我的程序那位帮出个数据测测。很简单的递归阿In Reply To:那个给个测试数据吧,实在看不错我的程序哪里有问题啊 Posted by:first at 2005-12-29 17:53:34 #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int price[100];
int type[10];
int result[10];
int appear[100];
int resappear[100];
int tempapp[100];
int n;
int maxtype;
int ind;
int resind;
int maxprice;
bool tie;
void print(int);
void work(int, int);
vector <int> input;
int main()
{
int p;
while(cin>>p)
{
n = 0;
while(p)
{
price[n ++] = p;
scanf("%d",&p);
}
sort(price, price + n);
scanf("%d",&p);
input.clear();
while(p)
{
input.push_back(p);
scanf("%d",&p);
}
for(int num = 0; num < input.size(); num ++)
{
p = input[num];
ind = 0;
maxtype = 0;
resind = 0;
tie = false;
memset(appear, 0, sizeof(appear));
memset(type, 0, sizeof(type));
int i, j;
for( i = 0; i <= 4; i ++)
{
if( p - i*price[0] >= 0)
{
for( j = 0; j < i; j ++)
{
type[ind ++] = 0;
appear[0] ++;
}
work(p - i*price[0], 1);
for( j = 0; j < i; j ++)
{
appear[0] --;
ind --;
}
}
}
print(p);
}
}
return 0;
}
void work(int p, int nth)
{
int i, j;
if( ind > 4)
return ;
if(p == 0)
{
int max = 0;
int tempprice = 0;
memcpy( tempapp, appear, sizeof(appear) );
for( i = 0; i < ind; i ++)
{
if( tempapp[type[i]] > 0 )
{
max ++;
tempprice = tempprice > price[type[i]] ? tempprice : price[type[i]];
tempapp[type[i]] = 0;
}
}
if( max > maxtype )
{
memcpy(result, type, sizeof(type));
memcpy(resappear, appear, sizeof(appear));
resind = ind;
maxprice = tempprice;
maxtype = max;
tie = false;
}
else if( max == maxtype )
{
if( ind < resind )
{
memcpy(result, type, sizeof(type));
memcpy(resappear, appear, sizeof(appear));
resind = ind;
maxprice = tempprice;
tie = false;
}
else if( ind == resind )
{
if( tempprice > maxprice )
{
memcpy(result, type, sizeof(type));
memcpy(resappear, appear, sizeof(appear));
maxprice = tempprice;
tie = false;
}
else if( tempprice == maxprice)
{
tie = true;
}
}
}
return;
}
if( nth >= n )
return;
if( p < 0 )
return;
if( p > 0 )
{
if( ind > 3 )
return;
else
{
for( i = 0; i <= 4 - ind; i ++)
{
if( p - i*price[nth] >= 0)
{
for( j = 0; j < i; j ++)
{
type[ind ++] = nth;
appear[nth] ++;
}
work(p - i*price[nth], nth + 1);
for( j = 0; j < i; j ++)
{
appear[nth] --;
ind --;
}
}
}
}
}
}
void print(int p)
{
int i;
if(maxtype == 0)
printf("%d --- none\n", p);
else if(tie == true)
printf("%d (%d): tie\n", p, maxtype);
else
{
printf("%d (%d): ", p, maxtype);
for( i = 0; i < resind - 1; i++)
printf("%d ",price[result[i]]);
printf("%d\n",price[result[resind - 1]]);
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator