12345的排列和组合

vic_xx 2010-04-17 04:52:49
写2个函数
1,输出12345的排列
2,由用户输入一个整数(n),n<5,从12345选取n个数的组合...
...全文
9658 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
vic_xx 2010-04-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 blueink_200451 的回复:]
C/C++ code

#include <stdio.h>
#define MAX 10

char used[MAX];
char out[MAX];
int set[MAX] = {1,2,3,4,5};

void C(int position, int h, int n, int m)
{
int i;
if(position == m)
……
[/Quote]

不是很懂哦,能否详细解释一下啊!
yuandei 2010-04-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#define MAX 10

char used[MAX];
char out[MAX];
int set[MAX] = {1,2,3,4,5};

void C(int position, int h, int n, int m)
{
int i;
if(position == m)
{
for(i=0; i<m; i++)
printf("%d ", out[i]);
printf("\n");
return;
}
for(i=h; i<=n-m+position; i++)
if (!used[i])
{
out[position] = set[i];
used[i]++;
C(position+1, i+1, n, m);
used[i]--;
}
}

int main()
{
C(0,0,5,5);//第一题

C(0,0,5,1);//第二题
C(0,0,5,2);
C(0,0,5,3);
C(0,0,5,4);
C(0,0,5,5);
return 0;
ithiker 2010-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 blueink_200451 的回复:]
C/C++ code

#include <stdio.h>
#define MAX 10

char used[MAX];
char out[MAX];
int set[MAX] = {1,2,3,4,5};

void C(int position, int h, int n, int m)
{
int i;
if(position == m)
……
[/Quote]
5个数的排列好像没有...
十八道胡同 2010-04-17
  • 打赏
  • 举报
回复
c#和c++是差不多的语法
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int n = 40;
int m = 6;
string[] set = new string[n];
for (int i = 0; i < n; i++)
{
set[i] = i.ToString().PadLeft(2, '0');
}
List<string> result;

GC.Collect();
Stopwatch watch = new Stopwatch();
using (new AutoWatch(watch))
{
//result = GetCombinationF1(set, m);//n=20 about 130ms;
//result = GetCombinationF2(set, m); //n=20 about 44ms, n=40 about 6s
//result = GetCombinationF3(set, m);//n=20 about 16ms n=40 about 2.5s
result = GetCombinationF4(set, m);//n=20 about 95ms n=40 about 13s
}
Console.WriteLine(watch.ElapsedMilliseconds);

//print output
foreach (string s in result)
{
//Console.WriteLine(s);
}
}

static List<string> GetCombinationF3(string[] data, int count)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
List<string> output = new List<string>();
for (int i = 0; i < data.Length; i++)
{
dic.Add(data[i], i);
}
SelectN(dic, data, count, 1, ref output);
return output;
}

static void SelectN(Dictionary<string, int> dd, string[] data, int count, int times, ref List<string> output)
{
Dictionary<string, int> dic = new Dictionary<string, int>();

foreach (KeyValuePair<string, int> kv in dd)
{
for (int i = kv.Value + 1; i < data.Length; i++)
{
if (times < count - 1)
{
dic.Add(kv.Key + "\t" + data[i], i);
}
else
{
output.Add(kv.Key + "\t" + data[i]);//不考虑输出,将此句注释掉
}
}
}
times++;
if (dic.Count > 0) SelectN(dic, data, count, times,ref output);
}


static List<string> GetCombinationF1(string[] set,int m)
{
int n = set.Length;
int min = (0x01 << m) - 1;//00111111
int max = min << (n - m);//11111100
int j;
int k;
List<string> output = new List<string>();
string s;

for (int i = min; i <= max; i++)
{
j = 0;
k = i;
while (k > 0)
{
j += (int)(k & 0x01);
k >>= 1;
if (j > m)
{
break;
}
}
if (j == m)
{
s = "";
k = 0x01;
for (int l = n - 1; l >= 0; l--)
{
if ((k & i) == k)
{
s+=set[l] + "\t";
}
k <<= 1;
}
output.Add(s);
}

}
return output;
}


static List<string> GetCombinationF2(string[] strArray, int selectCount)
{
int totalCount = strArray.Length;
int[] currentSelect = new int[selectCount];
int last = selectCount - 1;
List<string> output = new List<string>();
string s;

//付初始值
for (int i = 0; i < selectCount; i++)
currentSelect[i] = i;

while (true)
{
s = "";
//输出部分,生成的时候从0计数,所以输出的时候+1
for (int i = 0; i < selectCount; i++)
{
s += strArray[currentSelect[i]]+"\t";
}
output.Add(s);

//如果不进位
if (currentSelect[last] < totalCount - 1)
currentSelect[last]++;
else
{
//进位部分
int position = last;

while (position > 0 && currentSelect[position - 1] == currentSelect[position] - 1)
position--;

if (position == 0)
break ;

currentSelect[position - 1]++;

for (int i = position; i < selectCount; i++)
currentSelect[i] = currentSelect[i - 1] + 1;
}
}
return output;
}

static List<string> GetCombinationF4(string[] data, int count)
{
List<string> output = new List<string>();
int len = data.Length;
string start = "1".PadRight(count, '1').PadRight(len, '0');
string s;
while (start != string.Empty)
{
s = "";
for (int i = 0; i < len; i++)
if (start[i] == '1') s+=data[i] + "\t";
output.Add(s);
start = GetNext(start);
}
return output;

}

static string GetNext(string str)
{
string next = string.Empty;
int pos = str.IndexOf("10");
if (pos < 0) return next;
else if (pos == 0) return "01" + str.Substring(2);
else
{
int len = str.Length;
next = str.Substring(0, pos).Replace("0", "").PadRight(pos, '0') + "01";
if (pos < len - 2) next += str.Substring(pos + 2);
}
return next;
}


public sealed class AutoWatch : IDisposable
{
private Stopwatch watch;

public AutoWatch(Stopwatch watch)
{
this.watch = watch;
watch.Start();
}

void IDisposable.Dispose()
{
watch.Stop();
}
}

}
}
liyanlin122 2010-04-17
  • 打赏
  • 举报
回复

慢慢看
blueink_200451 2010-04-17
  • 打赏
  • 举报
回复

#include <stdio.h>
#define MAX 10

char used[MAX];
char out[MAX];
int set[MAX] = {1,2,3,4,5};

void C(int position, int h, int n, int m)
{
int i;
if(position == m)
{
for(i=0; i<m; i++)
printf("%d ", out[i]);
printf("\n");
return;
}
for(i=h; i<=n-m+position; i++)
if (!used[i])
{
out[position] = set[i];
used[i]++;
C(position+1, i+1, n, m);
used[i]--;
}
}

int main()
{
C(0,0,5,5);//第一题

C(0,0,5,1);//第二题
C(0,0,5,2);
C(0,0,5,3);
C(0,0,5,4);
C(0,0,5,5);
return 0;
}

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧