c语言:现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。

如题所述

第1个回答  2012-06-11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int N;
bool *mark;
int *output, pos = 0;

void AddNumber(int i)
{
bool finish = true;
mark[i] = true;
output[pos++] = i;

int j;
for (j = 0; j < N; j++)
{
if (!mark[j])
{
finish = false;
AddNumber(j);
}
}

if (finish)
{
for (j = 0; j < pos; j++)
printf("%d", output[j] + 1);
printf("\n");
}

pos--;
mark[i] = false;
}

int main()
{
printf("please input you want results\n");
scanf("%d", &N);
mark = (bool *)malloc(sizeof(bool) * N);
output = (int *)malloc(sizeof(int) * N);

memset(mark, (bool)0, sizeof(bool) * N);

int i;
for (i = 0; i < N; i++)
AddNumber(i);

free(mark);
free(output);
system("pause");
return 0;
}本回答被提问者采纳