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 |
为什么老wa递归搜索不该有问题啊 代码如下 #include <iostream> #include <algorithm> 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 index; int resind; int maxprice; bool tie; void print(int); void work(int, int); void main() { int p; while(scanf("%d",&p) != EOF) { n = 0; while(p) { price[n ++] = p; scanf("%d",&p); } scanf("%d",&p); while(p) { index = 0; maxtype = 0; resind = 0; tie = false; memset(appear, 0, sizeof(appear)); int i; for( i = 0; i <= 4; i ++) { if( p - i*price[0] >= 0) { for( int j = 0; j < i; j ++) { type[index ++] = 0; appear[0] ++; } work(p - i*price[0], 1); for( j = 0; j < i; j ++) { appear[0] --; index --; } } } print(p); scanf("%d",&p); } } } void work(int p, int nth) { if(p == 0) { int i; int max = 0; int tempprice = 0; memcpy( tempapp, appear, sizeof(appear) ); for( i = 0; i < index; 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 = index; maxprice = tempprice; maxtype = max; tie = false; } else if( max == maxtype ) { if( index < resind ) { memcpy(result, type, sizeof(type)); memcpy(resappear, appear, sizeof(appear)); resind = index; maxprice = tempprice; tie = false; } else if( index == 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( index > 3 ) return; else { for(int i = 0; i <= 4 - index; i ++) { if( p - i*price[nth] >= 0) { for( int j = 0; j < i; j ++) { type[index ++] = nth; appear[nth] ++; } work(p - i*price[nth], nth + 1); for( j = 0; j < i; j ++) { appear[nth] --; index --; } } } } } } 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