用C语言和数据结构编写一个简单的程序(求源代码)

2024-12-17 06:20:33
推荐回答(6个)
回答1:

/*使用递归,理论上说可以对任意多位数组合,但位数太多了,可能发生堆栈溢出.
以下程序在VC++6.0中编译通过.
*/
#include
#include

#define MAX_NUMBER 6 //修改这个参数来允许最大的位数,现设为6位

void GetZhe (const char * preStr,const char * strNum)
{
char newPreStr[MAX_NUMBER];
char tmpStr[MAX_NUMBER];
int i,j,k,iCnt;

k = strlen(strNum);
if (k>MAX_NUMBER) {
printf("超过最大允许的位数:%d!",MAX_NUMBER);
return ;
}

switch(k)
{
case 0:
return;
case 1:
strcpy(tmpStr,preStr);
strcat(tmpStr,strNum);
printf("%s\r\n",tmpStr);
break;
default:
tmpStr[0] = 0;
for (i=0;i {
strcpy(newPreStr,preStr);
j = strlen(newPreStr);
newPreStr[j] = strNum[i];
newPreStr[j+1] = 0;

iCnt = 0;
for (j=0;j {
if(j != i)
{
tmpStr[iCnt] = strNum[j];
iCnt ++ ;
}
}
tmpStr[iCnt] = 0;

GetZhe(newPreStr,tmpStr);
}
}
}

int main(int argc, char* argv[])
{
GetZhe("","123456");
return 0;
}

回答2:

排列不对,如果有0呢?

回答3:

我是用递归写的
代码如下
如果合适的话我可以添加注释

#include
 
void Combinate(char* str, char* strHead)
{
        int i;
        char _tmp[100] = {NULL}, _tmp2[100] = {NULL}, _ch[2] = {NULL};
        if(!str)
                return;
        if(strlen(str) == 1)
        {
                strcpy(_tmp, strHead);
                strcat(_tmp, str);
                printf("%s\n", _tmp);
                return;
        }
        strcpy(_tmp, "");
        for(i = 0; i < strlen(str); i++)
        {
                strcpy(_tmp, str);
                _tmp[i] = NULL;
                strcat(_tmp, _tmp + i + 1);
                _ch[0] = str[i];
                strcpy(_tmp2, strHead);
                strcat(_tmp2, _ch);
                Combinate(_tmp, _tmp2);
        }
}
 
int main()
{
        char str[100] = {NULL};
        scanf("%s", str);
        str[6] = NULL;
        printf("You have typed %s\nAnd the result is:\n\n", str);
        Combinate(str, "");
}

比如当输入123时
输出为
You have typed 123
And the result is:

123
132
213
231
312
321

输入1234
输出为
You have typed 1234
And the result is:

1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

回答4:

#include "stdio.h"
void main()
{
char a[6];
int i=0,j,k,s,p;
printf("please intput num:\n");
while((ch=getch())!='\r')
{
a[++i]=ch;
if(i==6)
{ printf("only can input 6 wei shu!\n"); break;}
}
for(i=0;i<6;i++)
for(j=0;j!=i;j++)
for(k=0;k!=i&&k!=j;k++)
for(s=0;s!=i&&s!=j&&s!=k;s++)
for(p=0;p!=i&&p!=j&&p!=k&&p!=s;p++)
printf("%d%d%d%d%d%d ",a[i],a[j],a[k],a[s],a[p]);
}

回答5:

/*
使用单链表
输入时数字以空格隔开,以回车结束
*/
#include"stdio.h"
typedef struct node
{
int num;
node *next;
int checked;
}lnode,*llist;
void main()
{
char ch;
llist l=NULL;
lnode *t;
int checkedcount,rcount;
int a[6],i,j,k,record[1000]={0},n=1;
int b[6]={1,10,100,1000,10000,100000};
ch=' ';
i=0;
printf("输入数字:");
while(ch!='\n')
{
lnode *p=new lnode;
scanf("%d",&p->num);
p->next=l;
l=p;
i++;
ch=getchar();
}
rcount=0;
for(j=1;j n*=10;
for(j=n;j<10*n;j++)
{
checkedcount=0;
for(t=l;t!=NULL;t=t->next)
t->checked=0;
for(k=0;k {
a[k]=(j/b[k])%10;
for(t=l;t!=NULL;t=t->next)
if(t->num==a[k]&&t->checked==0)
{
t->checked=1;
checkedcount++;
break;
}
}
if(checkedcount==i)
{
record[rcount]=j;
rcount++;
}
}
for(j=0;j printf("%-6d ",record[j]);
printf("\n");
}

回答6:

有难度