排列组合

hdu  题目:http://acm.split.hdu.edu.cn/showproblem.php?pid=1015

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void quickSort(int start,int end,string &s){   //快排 
	if(start<end){
		int l=start,r=end;
		char ch=s[start];

		while(l<r){
			while(s[r]>ch&&l<r) r--;
			if(l<r){
				s[l++]=s[r];
				}
			while(s[l]<ch&&l<r) l++;
			if(l<r){
				s[r--]=s[l];
			}
		}

		s[l]=ch;
		quickSort(start,l-1,s);
		quickSort(l+1,end,s);
	}
}
int main(){
	string s;
	int target;

	while(cin>>target>>s){
		if(target==0&&s=="END"){	
			break;
		}
		else{
			quickSort(0,s.length()-1,s);
		
			char temp;
			string s1;

			for(int a=0;a<s.length();a++){    //排列组合 
				temp=s[0];
				s[0]=s[a];
				s[a]=temp;

				for(int b=1;b<s.length();b++){
					temp=s[1];
					s[1]=s[b];
					s[b]=temp;

					for(int c=2;c<s.length();c++){
						temp=s[2];
						s[2]=s[c];
						s[c]=temp;

						for(int d=3;d<s.length();d++){
							temp=s[3];
							s[3]=s[d];
							s[d]=temp;

							for(int e=4;e<s.length();e++){
								 if(((int)s[0]-64) - (int)pow((int)s[1]-64,2) + (int)pow((int)s[2]-64,3) - (int)pow((int)s[3]-64,4) + (int)pow((int)s[e]-64,5)==target){
								 	s1=s.substr(0,4)+s[e];


								 }
							}
							temp=s[3];
							s[3]=s[d];
							s[d]=temp;
						}
						temp=s[2];
						s[2]=s[c];
						s[c]=temp;
					}
					temp=s[1];
					s[1]=s[b];
					s[b]=temp;
				}

			}
		if(s1==""){
			cout<<"no solution"<<endl; 
		}
        else cout<<s1<<endl;
		}

	}
}

  

posted @ 2017-10-18 22:42  bear_ge  阅读(178)  评论(0编辑  收藏  举报