急!用c语言实现一个小程序

有0,1,2,3,4,5,6,7,8,9十个数据,要按照每两个数为一组把它们分成五组,然后再把这五个分组进行排序。
排序后的具体要求:

每一组中的两个数所限制的数据不能在与其相邻的下一组出现,当然第五组中的两个数所限制的数据也不能在第一组中出现。

例如,0限制1;1限制3、5、6;2限制0、1、6、7;3限制1、2;5限制1、2;6限制2;7限制8、9;8限制5、6、7;9限制2、8。

满足这种限制的一种情况是:第一组为(0,7);第二组(2,5);第三组(3,8);第四组(4,9);第五组(1,6)。

用c语言实现满足上述要求的分组与排序,而且要求程序能列出所有满足条件的分组情况。另外,限制条件是根据实际情况决定的,因而要求程序能给出让用户输入限制条件的提示。

回答争取的话我再加一百分给你~!!!!!!!

#include<stdio.h>

#define N 10 //十个数据
#define M 5 //五个组

int limit[N][N]=
{
{1,N}, //0限制1;
{3,5,6,N}, //1限制3、5、6;
{0,1,6,7,N},//2限制0、1、6、7;
{1,2,N}, //3限制1、2;
{N}, //
{1,2,N}, //5限制1、2;
{2,N}, //6限制2;
{8,9,N}, //7限制8、9;
{5,6,7,N}, //8限制5、6、7;
{2,8,N} //9限制2、8。
};

//判断m是否限制n
bool m_limit_n(int m, int n)
{
int i;

if(m>=N || n>=N)
{
return false;
}
for(i=0;i<N && limit[m][i]<N;i++)
{
if(limit[m][i]==n)
{
return true;
}
}
return false;
}

bool valid(int *arr)
{
int i,j;

for (i = 0; i < N; i=i+2)
{
if(arr[i]>arr[i+1])
{
return false;
}
if(m_limit_n(arr[i],arr[(i+2)%N])
||m_limit_n(arr[i],arr[(i+3)%N])
||m_limit_n(arr[i+1],arr[(i+2)%N])
||m_limit_n(arr[i+1],arr[(i+3)%N]))
{
return false;
}
}
return true;
}

void exchange(int *arr, int n, int m)
{
int temp;

temp=arr[n];
arr[n]=arr[m];
arr[m]=temp;
}

int a[]={0,1,2,3,4,5,6,7,8,9};
void perm(int* arr, int n, int curr)
{
int i;

if (curr>=n-1)
{
if(valid(arr))
{
for (i = 0; i < N; i=i+2)
{
printf("%d,%d ", arr[i],arr[i+1]);
}
printf("\b\n");
}
}
else
{
for (i = curr; i < n; i=i+1)
{
if(i==curr)
{
perm(arr, n, curr+1);
}
else
{
exchange(arr,i,curr);
perm(arr, n, curr+1);
exchange(arr,i,curr);
}
}
}
}

int main()
{
perm(a, sizeof(a)/sizeof(a[0]), 0);
return 0;
}

“限制条件是根据实际情况决定的,因而要求程序能给出让用户输入限制条件的提示”很容易,自己看着办吧。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-11-05
马上回去写,包你满意阿…

“高金山 - 首席运营官 十三级”的回答似乎没有看到“限制条件是根据实际情况决定的,因而要求程序能给出让用户输入限制条件的提示”这句话阿…
第2个回答  2008-11-05
我搞出来1207项

#include <stdio.h>
#include <assert.h>
#include <conio.h>

#define NUM 10
#define GROUP 5
#define NUM_IN_GROUP (NUM / GROUP)

int checkConstraint(int m, int n)
{
static int constraint[NUM][NUM] = {
{ 1, NUM },
{ 3, 5, 6, NUM },
{ 0, 1, 6, 7, NUM },
{ 1, 2, NUM },
{ NUM },
{ 1, 2, NUM },
{ 2, NUM },
{ 8, 9, NUM },
{ 5, 6, 7, NUM },
{ 2, 8, NUM }
};

int i = 0;

while (constraint[m][i] != NUM)
{
if (constraint[m][i] == n)
{
return 1;
}
i++;
}

return 0;
}

int numberList[NUM] = { 0 };
int listTop = 0;

int isNumberInList(int n)
{
int i;

for (i = 0; i < listTop; i++)
{
if (numberList[i] == n)
{
return 1;
}
}

return 0;
}

int all = 0;

void process()
{
int i, begin, end;

if (listTop == NUM)
{
for (i = 0; i < NUM; i++)
{
printf("%d ", numberList[i]);
}
putchar('\n');
all++;

return;
}

begin = (listTop % NUM_IN_GROUP) * GROUP;
end = begin + GROUP;
for (i = 0; i < NUM; i++)
{
int curg, re, j;

if (isNumberInList(i))
{
continue;
}

curg = listTop / NUM_IN_GROUP;
re = listTop % NUM_IN_GROUP;
for (j = 0; j < re; j++)
{
if (numberList[curg * NUM_IN_GROUP + j] > i)
{
break;
}
}
if (j < re)
{
continue;
}

if (curg = listTop / NUM_IN_GROUP)
{
curg = (curg - 1) * NUM_IN_GROUP;
for (j = 0; j < NUM_IN_GROUP; j++)
{
if (checkConstraint(numberList[curg + j], i))
{
break;
}
}
if (j < NUM_IN_GROUP)
{
continue;
}
}

numberList[listTop++] = i;
process();
listTop--;
}
}

void main()
{
process();
getch();
}
第3个回答  2008-11-06
#include<stdio.h>
char reject[10][10]={0};
int checklist(char *list)
{
int i,j,k;
char *p;
/*每组中,第一个数小于第二个数*/
for(i=0;i<5;i++)
{
if(list[2*i]>list[2*i+1]) return 0;
}
/*不同组的第一个数,排在后面的比排在前面的大*/
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(list[2*i]>list[2*j]) return 0;
}
}
/*验证限制条件*/
for(i=0;i<10;i++)
{
p=reject[list[i]-'0'];
if(i%2==0) while(*p && *p++==list[i+1]) return 0;
else while(*p && *p++==list[i-1]) return 0;
}
return 1;
}

/*全排列递归算法*/
#define SWAP(a,b,c) do{c=a;a=b;b=c;}while(0)
void proPerm(char *list,int i,int n)
{
int j,temp,k;
if(i==n)
{
/*
for(j=0;j<=n;j++)
printf("%c",list[j]);
printf("\n");
*/
if(checklist(list))
{
for(k=0;k<5;k++)
printf("{%c,%c} ",list[2*k], list[2*k+1]);
printf("\n");
}
}
else
{
for(j=i;j<=n;j++)
{
SWAP(list[i],list[j],temp);
proPerm(list,i+1,n);
SWAP(list[i],list[j],temp);
}
}
}

void main()
{
int i;
char s[]="0123456789";

printf("Please input reject condition:\n");
printf("example:\n");
printf("0 :12 -- 0 reject 1 and 2\n");
printf("1 :* -- 1 reject nobody\n");
printf("------------------------------\n");
for(i=0;i<10;i++)
{
printf("%d :",i);
scanf("%s",reject[i]);
}

for(i=0;i<10;i++)
{
printf("%d:%s\n",i,reject[i]);
}
proPerm(s,0,9);
getch();
}
第4个回答  2008-11-05
尝试帮你做,留名先
相似回答