38の勝利Offer--顔の質問に:文字列の配列

38が刻まれている:文字列の配置
タイトル:文字列を入力し、文字列内のすべての文字を印刷するように配置されました。例えば、入力文字列abcの場合は、ABCのすべての文字列、B、Cが出て配置することができ、ACB、BAC、BCA、CABおよびCBAを印刷します。

#include<iostream>
using namespace std;
void Permutation(char *pStr, char *pBegin){
	if(*pBegin=='\0') printf("%s\n", pStr);
	else{
		for(char* pCh=pBegin;*pCh!='\0';pCh++){
			char temp=*pCh;
			*pCh=*pBegin;
			*pBegin=temp;
			
			Permutation(pStr, pBegin+1);
			
			temp=*pCh;
			*pCh=*pBegin;
			*pBegin=temp;
		}
	}
}
void Permutation(char* pStr){
	if(pStr==NULL) return ;
	Permutation(pStr, pStr);
}
int main() {
	char pStr[]="abc";
	Permutation(pStr);
	return 0;
}

関連トピック:

  • 8を含む数字の配列を入力し、この決意それ可能8数が3~4の頂点のグループと立方体の反対側が等しくなるよう8つの立方体の頂点に配置されています。
    ここに画像を挿入説明
    この対応は、A1、A2、A3、A4、得 A5、A6、A7、A8 すべての順列及び8つの番号、及びその後一定のトピックが決定されるが、すなわち、A1 + A2 + A3 + A4、与えられた条件に沿って配置されています== A5 + A6 + A7 + A8 、A1 + A3 + A5 + A7 == A2 + A4 + A6 + A8、 及びA1 + A2 + A5 + A6 == A3 + A4 + A7 + A8。
#include<iostream>
using namespace std;
bool CubVertex(int *A, int len, int begin){
	if(A==NULL || len!=8) return false;
	bool result=false;
	if(begin==len-1){
		if(A[0]+A[1]+A[2]+A[3]==A[4]+A[5]+A[6]+A[7] && A[0]+A[2]+A[4]+A[6]==A[1]+A[5]+A[3]+A[7] && A[0]+A[1]+A[4]+A[5]==A[2]+A[3]+A[6]+A[7]){
			for(int i=0;i<len;i++) {
				printf("%d ", A[i]);
			}
			printf("\n");
			result=true;
		}
	}else{
		for(int i=begin;i<len;i++){
			int temp=A[begin];
			A[begin]=A[i];
			A[i]=temp;
			
			result=CubVertex(A, len, begin+1);
			if(result) break;
			
			temp=A[begin];
			A[begin]=A[i];
			A[i]=temp;
		}
	}
	return result;
}
int main() {
	int A[8] = {1,2,3,1,2,3,2,2};
	int B[8] = {1,2,3,1,8,3,2,2};
	if(CubVertex(A,8,0))
		printf("Yes\n");
	else
		printf("No\n");
	if(CubVertex(B,8,0))
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}
  • n個の問題をQUEENS
#include<iostream>
using namespace std;
const int maxn=11;
int n,P[maxn],hashTable[maxn]={false},count=0;
void generateP(int index){
	if(index==n+1){
		count++;
		return ;
	}
	for(int x=1;x<=n;x++){
		if(hashTable[x]==false){
			bool flag=true;
			for(int pre=1;pre<index;pre++){
				if(abs(index-pre)==abs(x-P[pre])){
					flag=false;
					break;
				}
			}
			if(flag){
				P[index]=x;
				hashTable[x]=true;
				generateP(index+1);
				hashTable[x]=false;
			}
		}
	}
}
int main() {
	n=8;
	generateP(1);
	printf("%d", count);
	return 0;
}
公開された42元の記事 ウォン称賛43 ビュー1044

おすすめ

転載: blog.csdn.net/qq_35340189/article/details/104433391