64,648
社区成员
发帖
与我相关
我的任务
分享
//qplw.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int v=0;
int w=0;
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
void Make_Map(char *str) {//统计字符串的相关信息
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
if (w<1 || m<w) w=m;
while(*str) {
s[*str]++;
str++;
}
n=0;
for (i=0;i<256;i++)
if (s[i]) {
map[n]=i;
count[n]=s[i];
n++;
}
}
void Find(int depth) {//递归式回溯法生成全排列
if (depth==w) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
} else {
int i;
if (v && depth>0) {
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
}
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main(int argc,char**argv) {
if (argc<2) {
printf("%s 要产生全排列的字符串 [限定长度|-1]\n",argv[0]);
return;
}
if (argc>=3) w=atoi(argv[2]);
if (-1==w) v=1;
Make_Map(argv[1]);
Find(0);
}
//C:\test>qplw
//qplw 要产生全排列的字符串 [限定长度|-1]
//
//C:\test>qplw 123
//123
//132
//213
//231
//312
//321
//
//C:\test>qplw 123 2
//12
//13
//21
//23
//31
//32
//
//C:\test>qplw 122333 3
//122
//123
//132
//133
//212
//213
//221
//223
//231
//232
//233
//312
//313
//321
//322
//323
//331
//332
//333
//
//C:\test>qplw 123 -1
//1
//12
//123
//13
//132
//2
//21
//213
//23
//231
//3
//31
//312
//32
//321
//
//这个结果本身就是对的!!!
int main()
{
int count = 0;
int n = 3;//要排列的个数
int a[5] = {0,1,2,3,4};
for(int i = 0; i < sizeof(a)/4; i++)
{
for (int j = 0; j < sizeof(a)/4; j++)
{
for (int m = 0; m < sizeof(a)/4; m++)
{
printf("%d: %d%d%d\r\n", count++, a[i], a[j], a[m]);
}
}
}
system("pause");
return 0;
}
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <sys/stat.h>
/***************************************************************************************/
//对于一个只由0,1,2,3,4组成的三位字符串,可能的组合是000到444 //
//我的解法是将000到444看成一个五进制数,那么数值的范围就是0 ~ 124 //
//我只需要借循环输出0~124对应的五进制数的三位字符表示就行了 //
/***************************************************************************************/
char * ConvertInt2Str(unsigned int nBeConvrt, unsigned int nDesCharLen = 3/*输出字符串位数即长度*/)
{
assert(nBeConvrt < pow((double)5, (int)nDesCharLen));
char *pCh;
unsigned int nTemp = nBeConvrt;
pCh = new char(nDesCharLen+1);
assert(pCh != NULL);
for (int i = nDesCharLen ; i > 0; i--)
{
pCh[nDesCharLen - i] = nTemp/pow((double)5,i-1) + 48;
nTemp = nTemp%(unsigned int)pow((double)5,i-1);
}
pCh[nDesCharLen] = '\0';
return pCh;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *pCh2Print;
int ac = 0;
for (;ac < 125; ac++)
{
pCh2Print = ConvertInt2Str(ac);
printf("%s\n", pCh2Print);
}
system("pause");
return 0;
}
大家有指教直说,别骂我就行。。。#pragma warning(disable:4786)
#include <iostream>
#include <windows.h>
using namespace std;
struct Int
{
int a;
int b;
int c;
};
Int Itest[1000];
bool duibi(int a, int b, int c);
int main()
{
int count = 0;
int test[15] = {0,0,0,1,1,1,2,2,2,3,3,3,4,4,4};
for (int i = 0; i<15; i++)
{
for (int j = 0; j<15; j++)
{
for(int n = 0; n<15; n++)
{
if (0 != count)
{
if (!duibi(test[i], test[j], test[n]))//如果不重复,继续添加
{
Itest[count].a = test[i];
Itest[count].b = test[j];
Itest[count].c = test[n];
count++;
}
}
else
{ //第一个直接添加
Itest[count].a = test[i];
Itest[count].b = test[j];
Itest[count].c = test[n];
count++;
}
}
}
}
for (i = 0; i <count; i++)
{
printf("%d: %d%d%d\r\n", i, Itest[i].a, Itest[i].b, Itest[i].c);
}
system("pause");
return 0;
}
bool duibi(int a, int b, int c)
{
for (int i = 0; i < 1000; i++)
{
if (Itest[i].a == a &&
Itest[i].b == b &&
Itest[i].c == c)
{
return true;
}
}
return false;
}
#pragma warning(disable:4786)
#include <iostream>
#include <set>
#include <windows.h>
using namespace std;
struct Int
{
int a;
int b;
int c;
};
typedef set<Int> SET_INT;
set<Int>::iterator it_set;
Int Itest;
SET_INT set_int;
int main()
{
int count = 0;
int test[15] = {0,0,0,1,1,1,2,2,2,3,3,3,4,4,4};
for (int i = 0; i<15; i++)
{
for (int j = 0; j<15; j++)
{
for(int n = 0; n<15; n++)
{
Itest.a = test[i];
Itest.b = test[j];
Itest.c = test[n];
set_int.insert(Itest);
}
}
}
for (it_set = set_int.begin(); it_set!=set_int.end(); it_set++)
{
Itest = *it_set;
cout << Itest.a << Itest.b << Itest.c;
}
system("pause");
return 0;
}
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int count = 0;
int test[15] = {0,0,0,1,1,1,2,2,2,3,3,3,4,4,4};
for (int i = 0; i<15; i++)
{
for (int j = 0; j<15; j++)
{
for(int n = 0; n<15; n++)
{
printf("%d: %d%d%d\r\n", count, test[i], test[j], test[n]);
count++;
}
}
}
system("pause");
return 0;
}