#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; }