输出全组合问题

  1. 网易:题意很简单,写一个程序,打印出以下的序列。
    (a),(b),(c),(d),(e)........(z)
    (a,b),(a,c),(a,d),(a,e)......(a,z),(b,c),(b,d).....(b,z),(c,d).....(y,z)
    (a,b,c),(a,b,d)....(a,b,z),(a,c,d)....(x,y,z)
    ....
    (a,b,c,d,.....x,y,z)(思路:全组合问题
#include "stdafx.h"
#include <iostream>
using namespace std;

#define TABLE_LEN 26
char a_table[TABLE_LEN];
char pstr[TABLE_LEN];
int current = 0;

bool pick_array(char *table, int pick_len, int len)
{
	if (table == NULL)
		return false;
	else if(pick_len == 0 || len == 0){
		pstr[current] = '\0';
		printf("(%s),", pstr);
		return true;
	}
	for (int i = 0; i < len - pick_len + 1; i++){
		pstr[current++] = table[i];
		if (!pick_array(table + i + 1, pick_len - 1, len - i - 1))
			return false;
		current--;
	}
	return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
	for (int i = 0; i < TABLE_LEN; i++){
		a_table[i] = 'a' + i;
	}
	for (int len = 1; len <= TABLE_LEN; len++){
		current = 0;
		pstr[current] = '\0';
		if (!pick_array(a_table, len, TABLE_LEN)){
			printf("error\n");
			return -1;
		}
		printf("\n");
	}

	std::cin.get();
	return 0;
}

取abcde做测试



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值