用C语言和数据结构编写一个简单的程序(求源代码)
用C语言和数据结构编写一个:一个不大于六位的数字,列出这个数字数位的任意组合。例如:输入1,2,3三个数,输出的三位数组就是:123,132,213,231,321,31...
用C语言和数据结构编写一个:一个不大于六位的数字,列出这个数字数位的任意组合。
例如:输入1,2,3三个数,输出的三位数组就是:123,132,213,231,321,312。
程序的要求是输入数字个数必须不超过六个(可以少于六个),然后显示所有的组合形式。
急求源代码!!!
非常感谢!!! 展开
例如:输入1,2,3三个数,输出的三位数组就是:123,132,213,231,321,312。
程序的要求是输入数字个数必须不超过六个(可以少于六个),然后显示所有的组合形式。
急求源代码!!!
非常感谢!!! 展开
9个回答
展开全部
C语言大多数编译好EXE文件都需要在命令行模式(CMD模式)执行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这应该是排列吧:
#include <stdio.h>
void Print(int* beg, int* end)
{
while(beg < end)
printf("%d ", *beg++);
putchar('\n');
}
void Swap(int* lhs, int* rhs)
{
int tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}
void Reverse(int* beg, int* end)
{
while(beg < end)
Swap(beg++, --end);
}
// 方法一:
///////////////////////////////////////////
void P1(int* const cbeg, int* beg, int* end)
{
if(beg == end - 1)
Print(cbeg, end);
else
for(int* p = beg; p != end; ++p)
{
Swap(beg, p);
P1(cbeg, beg + 1, end);
Swap(beg, p);
}
}
// 方法2:
///////////////////////////////////////////
void P2(int* beg, int* end)
{
int* prev = end;
if(beg == end || --prev == beg)
return;
Print(beg, end);
for(;;)
{
int* next = prev;
if(*--prev < *next)
{
int* exc = end;
while(*--exc < *prev);
Swap(exc, prev);
Reverse(next, end);
Print(beg, end);
prev = end - 1;
}
if(prev == beg)
{
Reverse(beg, end);
return;
}
}
}
int main()
{
int a[] = {1, 2, 3};
puts("method 1:");
P1(a, a, a + 3);
puts("\nmethod 2:");
P2(a, a + 3);
}
#include <stdio.h>
void Print(int* beg, int* end)
{
while(beg < end)
printf("%d ", *beg++);
putchar('\n');
}
void Swap(int* lhs, int* rhs)
{
int tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}
void Reverse(int* beg, int* end)
{
while(beg < end)
Swap(beg++, --end);
}
// 方法一:
///////////////////////////////////////////
void P1(int* const cbeg, int* beg, int* end)
{
if(beg == end - 1)
Print(cbeg, end);
else
for(int* p = beg; p != end; ++p)
{
Swap(beg, p);
P1(cbeg, beg + 1, end);
Swap(beg, p);
}
}
// 方法2:
///////////////////////////////////////////
void P2(int* beg, int* end)
{
int* prev = end;
if(beg == end || --prev == beg)
return;
Print(beg, end);
for(;;)
{
int* next = prev;
if(*--prev < *next)
{
int* exc = end;
while(*--exc < *prev);
Swap(exc, prev);
Reverse(next, end);
Print(beg, end);
prev = end - 1;
}
if(prev == beg)
{
Reverse(beg, end);
return;
}
}
}
int main()
{
int a[] = {1, 2, 3};
puts("method 1:");
P1(a, a, a + 3);
puts("\nmethod 2:");
P2(a, a + 3);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写个main方法使用循环的方式调用banksimsystem中的方法即可模拟整个排队和叫号过程。
class
banksimsystem
{
private:
queuemanager
mqmanager;
public:
bankqueuesystem()
{
mqmanager
=
new
queuemanager();
}
void
getonenum()
{
int
nrand
=
random(5);
int
nnum;
if
nrand
<=
2)
{
nnum
=
mqmanager.getonevipnum();
printf("one
people
enqueue
:
n%d"
nnum);
}
else
{
nnum
=
mqmanager.getonenormalnum();
printf("one
people
enqueue
:
v%d"
nnum);
}
}
void
callonenum()
{
char
*snum
=
mqmanager.callonenum();
if
(nnum
==
-1)
{
printf("none
in
queue");
}
printf("call
the
people
:
%s",
snum);
}
}
class
queuemanager
{
private:
queue
mnormal;
queue
mvip;
public:
queuemanager()
{
mnormal
=
new
queue();
mvip
=
new
queue();
}
char
getonenormalnum()
{
int
mnormal.enqueue();
}
int
getonevipnum()
{
return
mvip.enqueue();
}
char*
callonenum()
{
char
snum[10];
if
(!mvip.isempty())
{
sprintf(snum,
"v%d",mvip.outqueue())
return
snum;
}
if
(!mnormal.isempty())
{
sprintf(snum,
"n%d",mnormal.outqueue())
return
snum;
}
return
null;
}
};
class
queue
{
private:
int
mnum;
struct
index{
int
num;
struct
index
*next;
}
*mhead,
*mtail;
public:
queue()
{
mnum
=
0;
mhead
=
null;
mtail
=
null;
}
int
enqueue()
{
struct
index
*tmpindex
=
(struct
index
*)malloc(sizeof(struct
index));
tmpindex->num
=
mnum;
tmpindex->next
=
null;
mnum++;
if
(mtail
==
null)
{
mhead
=
tmpindex;
}
else
{
mtail->next
=
tmpindex;
}
mtail
=
tmpindex;
}
int
outqueue()
{
struct
index
*tmpindex;
int
nreturn;
if
(mhead
==
null)
{
return
-1;
}
tmpindex
=
mhead;
mhead
=
mhead->next;
if
(mhead
==
null)
{
mtail
==
null;
}
nreturn
=
tmpindex->num;
free(tmpindex);
return
nreturn;
}
}
class
banksimsystem
{
private:
queuemanager
mqmanager;
public:
bankqueuesystem()
{
mqmanager
=
new
queuemanager();
}
void
getonenum()
{
int
nrand
=
random(5);
int
nnum;
if
nrand
<=
2)
{
nnum
=
mqmanager.getonevipnum();
printf("one
people
enqueue
:
n%d"
nnum);
}
else
{
nnum
=
mqmanager.getonenormalnum();
printf("one
people
enqueue
:
v%d"
nnum);
}
}
void
callonenum()
{
char
*snum
=
mqmanager.callonenum();
if
(nnum
==
-1)
{
printf("none
in
queue");
}
printf("call
the
people
:
%s",
snum);
}
}
class
queuemanager
{
private:
queue
mnormal;
queue
mvip;
public:
queuemanager()
{
mnormal
=
new
queue();
mvip
=
new
queue();
}
char
getonenormalnum()
{
int
mnormal.enqueue();
}
int
getonevipnum()
{
return
mvip.enqueue();
}
char*
callonenum()
{
char
snum[10];
if
(!mvip.isempty())
{
sprintf(snum,
"v%d",mvip.outqueue())
return
snum;
}
if
(!mnormal.isempty())
{
sprintf(snum,
"n%d",mnormal.outqueue())
return
snum;
}
return
null;
}
};
class
queue
{
private:
int
mnum;
struct
index{
int
num;
struct
index
*next;
}
*mhead,
*mtail;
public:
queue()
{
mnum
=
0;
mhead
=
null;
mtail
=
null;
}
int
enqueue()
{
struct
index
*tmpindex
=
(struct
index
*)malloc(sizeof(struct
index));
tmpindex->num
=
mnum;
tmpindex->next
=
null;
mnum++;
if
(mtail
==
null)
{
mhead
=
tmpindex;
}
else
{
mtail->next
=
tmpindex;
}
mtail
=
tmpindex;
}
int
outqueue()
{
struct
index
*tmpindex;
int
nreturn;
if
(mhead
==
null)
{
return
-1;
}
tmpindex
=
mhead;
mhead
=
mhead->next;
if
(mhead
==
null)
{
mtail
==
null;
}
nreturn
=
tmpindex->num;
free(tmpindex);
return
nreturn;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询