哈哈 你给我 我也不知道怎么花
那 疯子 来了..你期待吧...看他有没有好方法~!
[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
[此贴子已经被作者于2006-5-20 23:51:33编辑过]
看看这个
[CODE]
#include "stdio.h"
#include "malloc.h"
int main()
{
int *array,data,i,j,k;
printf("please input data:\n"); /*输入一个自然数,例如8*/
scanf("%d",&data);
if(data <= 2||data==5||data==6||data>8) /*这些没法排的*/
exit(-1);
array=(int *) malloc ( sizeof(int)*2*data);
for(i=2*data-1,j=(data+1)/2;j <= data;i--,j++)
array[i]=array[i-j-1]=j;
k=j=i-j+1;
for(i=0;i<k && k>0;i++,j++)
array[i]=array[i+j+1]=j;
array[i+j+1]=array[i+j+3]=1;
for(i=0;i<2*data;i++) /*结果两个,正序,倒序*/
printf("%d ",array[i]);
printf("\n");
for(i=2*data-1;i>=0;i--) /*两结果其实一样的*/
printf("%d ",array[i]);
free(array);
return 0;
}
[/CODE]
[此贴子已经被作者于2006-5-21 0:35:07编辑过]
和12楼16楼的基本想法查不多,但由于可能的顺序太多,如果不找到特殊的规律算法复杂度就是N!量级。算到12后就很慢了。
很多数没有解。
#include <stdio.h>
#include <malloc.h>
struct Link
{
int intValue;
struct Link * next;
};struct LinkList
{
Link * head;
};
bool GenerateSequence(int intCurrentNumber, int intMaxNumber, LinkList *list, bool *pbAnswer);
bool ValidateSequence(LinkList *list, int intMaxNumber);int main()
{
int intMax;
bool bAnswer;
LinkList list;
list.head = NULL;for (intMax = 3; intMax < 13; intMax++)
{
bAnswer = false;
printf(\"Solution for %d: \", intMax);
GenerateSequence(1, intMax, &list, &bAnswer);
if (!bAnswer)
printf(\"No solution\n\");
}
return 0;
}
bool GenerateSequence(int intCurrentNumber, int intMaxNumber, LinkList *list, bool *pbAnswer)
{
int intInsertPosition;
struct Link *linkInsert;// insert new link to list
for (int intPosition = 0; intPosition < intCurrentNumber; intPosition++)
{
// duplicate the linklist first.
struct LinkList listCurrent;
struct Link *linkCurrent, *linkCurrentLast, *linkTmp = list->head;
listCurrent.head = NULL;if (*pbAnswer)
return true;while (linkTmp != NULL)
{
linkCurrent = (struct Link *)malloc (sizeof(struct Link));
linkCurrent->intValue = linkTmp->intValue;
linkCurrent->next = NULL;if (listCurrent.head == NULL)
listCurrent.head = linkCurrent;
else
linkCurrentLast->next = linkCurrent;
linkCurrentLast = linkCurrent;linkTmp = linkTmp->next;
}linkCurrent = (struct Link *)malloc (sizeof(struct Link));
linkCurrent->intValue = intCurrentNumber;
linkCurrent->next = NULL;// if the first one, insert to the head
if (intPosition == 0)
{
linkCurrent->next = listCurrent.head;
listCurrent.head = linkCurrent;
}
else
{
// else insert after the number intPosition
for (linkInsert = listCurrent.head, intInsertPosition = 1; intInsertPosition < intPosition; linkInsert = linkInsert->next, intInsertPosition++)
;linkCurrent->next = linkInsert->next;
linkInsert->next = linkCurrent;
}if (intCurrentNumber < intMaxNumber)
GenerateSequence(intCurrentNumber+1, intMaxNumber, &listCurrent, pbAnswer);
else if (ValidateSequence(&listCurrent, intMaxNumber))
{
*pbAnswer = true;
}// FreeListMemory
while (listCurrent.head != NULL)
{
linkCurrent = listCurrent.head;
listCurrent.head = linkCurrent->next;
free(linkCurrent);
}
}
return true;}
bool ValidateSequence(LinkList *list, int intMaxNumber)
{
int intCount, intarNumbers[100], intPosition;
Link *linkCurrent;if (!list)
return false;for (intCount = 0; intCount < intMaxNumber*2; intCount++)
intarNumbers[intCount] = 0;for (intCount = 0, linkCurrent = list->head;
linkCurrent; intCount++, linkCurrent = linkCurrent->next)
{
intPosition = intCount;
while (intarNumbers[intPosition] > 0)
intPosition ++;
intarNumbers[intPosition] = linkCurrent->intValue;
if (intarNumbers[intPosition + linkCurrent->intValue + 1] > 0)
return false;
else if (intPosition + linkCurrent->intValue + 1>= intMaxNumber * 2)
return false;intarNumbers[intPosition + linkCurrent->intValue + 1] = linkCurrent->intValue;
}for (intCount = 0; intCount < intMaxNumber*2; intCount++)
printf(\"%d \", intarNumbers[intCount]);
printf(\"\n\");
return true;
}
[此贴子已经被作者于2006-5-21 9:34:33编辑过]