四重循环爆菊之!
#include <iostream>
#include <algorithm>
using namespace std;
int x,num,tot,kind,kk,kmax,hmax,n,stamp[110],best[5];
bool tie,ans,v[110],hash[110];
int cmp() {
if (kind>kk) return 1;
else if (kind==kk && tot<num) return 1;
else if (kind==kk && tot==num && hmax>kmax) return 1;
else if (kind==kk && tot==num && hmax==kmax) return 0;
return -1;
}
int main() {
while (~scanf("%d",&x)) {
n=0;
while (x!=0) {
stamp[++n]=x;
scanf("%d",&x);
}
stamp[n+1] = 0;
while (1) {
scanf("%d",&x);
if (x==0) break;
memset(best,0,sizeof(best));
num = 0;
kk = 0;
kmax = 0;
memset(v,0,sizeof(v));
memset(hash,0,sizeof(hash));
ans = false;
tie = false;
for (int i=1;i<=n+1;i++)
for (int j=i;j<=n+1;j++)
for (int k=j;k<=n+1;k++)
for (int r=k;r<=n+1;r++) {
int total = 0, t1, t2, t3, t4, sol[5];
int h1, h2, h3, h4;
h1=h2=h3=h4=t1=t2=t3=t4=0;
tot = 0; kind = 0; hmax = 0;
if (i!=n+1) {
total += stamp[i];
sol[++tot] = stamp[i];
h1 = i;
hmax = max(hmax,stamp[i]);
if (!hash[h1]) {
kind++;
hash[h1] = true;
}
}
if (j!=n+1) {
total += stamp[j];
sol[++tot] = stamp[j];
h2 = j;
hmax = max(hmax,stamp[j]);
if (!hash[h2]) {
kind++;
hash[h2] = true;
}
}
if (k!=n+1) {
total += stamp[k];
sol[++tot] = stamp[k];
h3 = k;
hmax = max(hmax,stamp[k]);
if (!hash[h3]) {
kind++;
hash[h3] = true;
}
}
if (r!=n+1) {
total += stamp[r];
sol[++tot] = stamp[r];
h4 = r;
hmax = max(hmax,stamp[r]);
if (!hash[h4]) {
kind++;
hash[h4] = true;
}
}
hash[h1]=hash[h2]=hash[h3]=hash[h4]=false;
if (total == x) {
ans = true;
int p = cmp();
if (p>0) {
tie = false;
for (int q=1;q<=tot;q++)
best[q] = sol[q];
num = tot;
kk = kind;
kmax = hmax;
}
else if (p==0) tie = true;
}
}
printf("%d ",x);
if (!ans) printf("---- none\n");
else if (tie) printf("(%d): tie\n",kk);
else {
printf("(%d):",kk);
sort(best+1,best+num+1);
for (int i=1;i<=num;i++) printf(" %d",best[i]);
printf("\n");
}
}
}
return 0;
}