剑指offer面试28

// wordCombination.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

#define N 255;

void combination(char* pStr,char* tarStr,int strLen,int nNowlen,int ncombinLen,int nbeginPos){
	if(nNowlen>=ncombinLen){
		tarStr[nNowlen]='\0';
		printf("%s\n",tarStr);
	}
	else
	{
		for (int i=nbeginPos;i<strLen;++i)
		{
			tarStr[nNowlen]=pStr[i];
			combination(pStr,tarStr,strLen,nNowlen+1,ncombinLen,i+1);
		}
	}
	
}

void combination(char* pStr){
	if (pStr==NULL)
	{
		return;
	}
	int len=strlen(pStr);
	char* tarStr =new char[len+1];
	for (int i=1;i<=len;++i)
	{
		combination(pStr,tarStr,len,0,i,0);
	}

}

int _tmain(int argc, char* argv[])
{
	char test[]="abc";
	combination(test);
	system("pause");
	return 0;
}

立方体相关题目

// cube.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

void cubevertex(int* pint,int begin){
	if(begin>8){
		std::cout<<"invalid input";
		return;
	}


	if(begin==8){
		if(pint[0]+pint[1]+pint[2]+pint[3]==pint[4]+pint[5]+pint[6]+pint[7]&&
			pint[0]+pint[2]+pint[5]+pint[7]==pint[1]+pint[3]+pint[5]+pint[7]&&
			pint[0]+pint[1]+pint[4]+pint[6]==pint[2]+pint[3]+pint[6]+pint[7])
		{
			for (int i=0;i<8;i++)
			{
				std::cout<<pint[i]<<" ";
			}
			std::cout<<std::endl;
		}
	}

	for (int i=begin;i<8;i++)
	{
		int tmp;
		tmp=pint[i];
		pint[i]=pint[begin];
		pint[begin]=tmp;
		cubevertex(pint,begin+1);

		tmp=pint[i];
		pint[i]=pint[begin];
		pint[begin]=tmp;
	}
	
}

void cubevertex(int* pint){
	if (pint==NULL)
	{
		return;
	}

	cubevertex(pint,0);
}

int _tmain(int argc, _TCHAR* argv[])
{
	int num[8]={1,2,3,4,5,6,7,8};
	//int num[8]={1,1,1,1,1,1,1,1};
	cubevertex(num);
	system("pause");
	return 0;
}


你可能感兴趣的:(剑指offer面试28)