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 |
why wrong?#include <iostream.h> #include <stdio.h> #include <string.h> #include <stdlib.h> long target; char s[50]; int l; int used[50]; int flag; char buf[10]; long pow(long a,long b) { long t=1; while(b--) t=t*a; return t; } int compare(const void *arg1,const void *arg2) { char a=*(char *)arg1; char b=*(char *)arg2; return b-a; } int search(long current,int k) { int sgn; int i; if (current==target){ flag=1; return 0; } if (flag) return 0; if (k>5) return 0; if (k%2==0) sgn=-1;else sgn=1; for(i=0;i<l;i++){ if (used[i]==0){ used[i]=1; search(current+sgn*pow(s[i]-'A'+1,k),k+1); if (flag){ buf[k-1]=s[i];return 0;} used[i]=0; } } return 0; } int main() { while(cin>>target>>s){ if (target==0 && strcmp(s,"END")==0) return 0; memset(used,0,sizeof(used)); l=strlen(s); qsort(s,l,sizeof(char),compare); flag=0; buf[5]=0; search(0,1); if (flag) cout<<buf<<endl; else cout<<"no solution"<<endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator