1010 STAMPS


#include <iostream.h>
#include <strstrea.h>
#include <string.h>
int stamp[1006]; // from 1 to 25, stamp[0] means none
int s[4]; // stamps sold
int n; // number of stamp types
int m; // customer request
int k; // number of stamps sold
int t; // types of stamps sold
int max; // highest single-value stamp value
int curK; // numbers of stamps sold of current best solution
int curT; // types of stamps sold of current best solution
int curM; // highest single value of current best solution
int curS[4]; // current best solution
bool curTie; // if there is tie best solutions
int temp;

bool ok(int temp) // there are no maore than 4 stamps value=temp
{
int i,j;
for (i=1,j=0;i<=n;i++) if (stamp[i]==temp) j++;
return (j<5);
}

void Dodo(){
for ( s[0]=0; s[0]<n; s[0]++)
for ( s[1]=s[0]; s[1]<n; s[1]++)
for ( s[2]=s[1]; s[2]<n; s[2]++)
for ( s[3] = s[2]; s[3]<n; s[3]++) {
if ( stamp[s[0]]+stamp[s[1]]+stamp[s[2]]+stamp[s[3]] != m ) continue;
k = 0;
t = 0;
max = 0;
if ( stamp[s[0]] ) {k++; t++; if ( stamp[s[0]]>max ) max = stamp[s[0]];}
if ( stamp[s[1]] ) {k++; if ( s[1]>s[0] ) t++; if ( stamp[s[1]]>max ) max = stamp[s[1]];}
if ( stamp[s[2]] ) {k++; if ( s[2]>s[1] ) t++; if ( stamp[s[2]]>max ) max = stamp[s[2]];}
if ( stamp[s[3]] ) {k++; if ( s[3]>s[2] ) t++; if ( stamp[s[3]]>max ) max = stamp[s[3]];}
if ( t<curT ) continue;
if ( t>curT ) {
curT = t;
curK = k;
curM = max;
curS[0]=s[0]; curS[1]=s[1];curS[2]=s[2];curS[3]=s[3];
curTie = false;
continue;
}
if ( k>curK ) continue;
if ( k<curK ) {
curT = t;
curK = k;
curM = max;
curS[0]=s[0]; curS[1]=s[1];curS[2]=s[2];curS[3]=s[3];
curTie = false;
continue;
}
if ( max<curM ) continue;
if ( max>curM ){
curT = t;
curK = k;
curM = max;
curS[0]=s[0]; curS[1]=s[1];curS[2]=s[2];curS[3]=s[3];
curTie = false;
continue;
}
curTie = true;
}
if ( curT == -1 ) {
cout<<m<<" ---- none"<<endl;
return;
}
cout<<m<<" ("<<curT<<"):";
if ( curTie ) {
cout<<" tie"<<endl;
return;
}
for ( int i=0; i<4; i++)
if ( curS[i]>0 ) cout<<' '<<stamp[curS[i]];
cout<<endl;
}
int main() {
char strs[255];
cin.getline(strs,255);
while ( strlen(strs) ) {
istrstream in1(strs);
s[0] = 0;
n = 1;
while ( in1 ) {
in1>>temp; //modify by duoshute
if (ok(temp)) // there are no maore than 4 stamps value=temp
{
stamp[n++]=temp;
if ( stamp[n-1] == 0 ) break;
}
}
n--;
cin.getline(strs,255);
istrstream in2(strs);
while ( in2 ) {
in2>>m;
if ( m == 0 ) break;
curK = 100;
curT = -1;
curM = -1;
curTie = false;
Dodo();
}
cin.getline(strs,255);
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值