用C语言编写程序:有1.2.3.4数字,能组成多少个互不相同且无重复数字的三位数?都是多少?帮忙解

用C语言编写程序:有1.2.3.4数字,能组成多少个互不相同且无重复数字的三位数?都是多少?帮忙解答一下!

#include <stdio.h>



int main()
{
int i,j,k,count=0;
for(i = 1; i <= 4; i++)
for(j = 1; j <= 4; j++)
for(k = 1; k <= 4; k++)
{
if(i != j && i != k && j != k)
{
count++;
printf("%d%d%d ",i,j,k);
}

}
printf("\n");
printf("count:%d\n",count);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-01-07
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 4

struct Node
{
int r[SIZE];
struct Node *next;
} head = {{0}, NULL}, *current = &head;
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void arrange(int m, int n, int *data)
{
//out put a full arrange
if(m == n)
{
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
memcpy(node->r, data, SIZE * sizeof(int));
current->next = node;
current = node;
}
else
{
int i;
for(i = m; i <= n; i++)
{
swap(&data[m], &data[i]);
arrange(m+1, n, data);
swap(&data[m], &data[i]);
}
}
}

int main() {
int data[SIZE] = {1, 2, 3, 4};
arrange(0, 3, data);
struct Node *p;
int count = 0;
for(p = head.next; p; p = p->next) {
printf("%d%d%d\n", p->r[0], p->r[1], p->r[2]);
count++;
}
printf("numbers: %d\n", count);
return 0;
}

本回答被网友采纳
相似回答