用C/C++实现 列出所有可能

zwh50687695 2009-07-09 08:24:51
比如
void A(int n,int m)


eg:1)
调用A(2,5)
输出:
1、1
1、2
1、3
1、4
1、5
2、1
2、2
2、3
2、4
2、5
……
5、1
5、2
5、3
5、4
5、5

eg:2)
调用A(3,4)
输出:
1、1、1
1、1、2
1、1、3
1、1、4
1、2、1
1、2、2
……
4、4、1
4、4、2
4、4、3
4、4、4

这样的代码可以实现吗?
...全文
128 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwh50687695 2009-07-09
  • 打赏
  • 举报
回复
hairetz 没理解到我的意思 还是谢谢了哈
zwh50687695 2009-07-09
  • 打赏
  • 举报
回复
谢谢各位了 我分不多 真的谢谢了哈 尤其是leng_que 谢谢你的耐心哈
leng_que 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zwh50687695 的回复:]
谢谢leng_que 哈
但当m的值为两位数的时候会出错哈 貌似只支持个位数
[/Quote]

呵呵,确实是这样的,没问题,马上动手写,来个加强版的,再试试


/*
* Author: Leng_que
* Date: 2009年7月9日
* E-mail: leng_que@yahoo.com.cn
* Description: 我的博客 http://blog.csdn.net/leng_que/
*/

#include <vector>
#include <iostream>
using namespace std;

void A(int n,int m)
{
int i,k;
vector<int> num;

for ( i=0; i<n; i++ )
{
num.push_back(1);
}

int len=num.size();
int off_len = len-1;

m++;

while (1)
{
//输出结果
for ( i=0; i<len; i++ )
{
cout<<num[i];
if ( i!=off_len )
{
cout<<"、";
}
}
cout<<endl;

num[off_len]++;

//进位判断及调整
for ( k=off_len; k>0; --k )
{
if ( num[k] == m )
{
num[k] = 1;
num[k-1]++;
if ( num[0] == m )
{
return ;
}
}
}
}
}

int main(void)
{
A(3,11);

return 0;
}
daidodo 2009-07-09
  • 打赏
  • 举报
回复
相当于输出n位的m进制数,总个数为:m^n(m的n次方)
代码如下:
#include <iostream>
#include <vector>

using namespace std;

void output(const std::vector<int> & arr)
{
for(std::vector<int>::const_reverse_iterator i = arr.rbegin();i != arr.rend();++i)
cout<<*i<<" ";
cout<<endl;
}

void A(int n,int m)
{
if(n < 1 || m < 1)
return;
std::vector<int> arr(n,1);
output(arr);
for(;;){
int i = 0;
for(;i < n;++i){
if(++arr[i] > m)
arr[i] -= m;
else{
break;
}
}
if(i >= n)
break;
output(arr);
}
}

int main()
{
A(3,11);
}
Walf_ghoul 2009-07-09
  • 打赏
  • 举报
回复
一组数中,值是可以重复出现的。。。
  • 打赏
  • 举报
回复
就是一种全排列的实现。


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void swap(char *str1,char *str2)
{
char temp;
temp=*str1;
*str1=*str2;
*str2=temp;
}

void permStr(char *str,int i)
{
//printf("%d",i);
if(i==strlen(str)-1)
printf("%s\n",str);
else
{
for(int j=i;j<strlen(str);j++)
{
//printf("i %d,j %d",i,j);
swap(&str[i],&str[j]);
permStr(str,i+1);
swap(&str[i],&str[j]);
}
}
}

void main()
{
char str[]={"abcde"};

permStr(str,0);

}



Walf_ghoul 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 luwenzuo 的回复:]
顶一下还是有效果()
[/Quote]
呵呵。。。
zwh50687695 2009-07-09
  • 打赏
  • 举报
回复
谢谢leng_que 哈
但当m的值为两位数的时候会出错哈 貌似只支持个位数
大前置 2009-07-09
  • 打赏
  • 举报
回复
顶一下还是有效果()
leng_que 2009-07-09
  • 打赏
  • 举报
回复

/*
* Author: Leng_que
* Date: 2009年7月9日
* E-mail: leng_que@yahoo.com.cn
* Description: 我的博客 http://blog.csdn.net/leng_que/
*/

#include <string>
#include <iostream>
using namespace std;

void all_order(string str, int base)
{
int n;
char cBase = base+48;
int len = str.size()-1;

while(1)
{
//
cout<<str<<endl;
//
str[len] += 1;

for ( n=len; n>0; --n )
{
if ( str[n] == cBase )
{
str[n] = '1'; //遇到进位时变为1
str[n-1] += 1;
}
if ( n==1 )
{
if ( str[0] == cBase )
{
return ;
}
}
}
}
}

void A(int n,int m)
{
//逢m+1进1
int base=m+1;

//初始位
string str;

for ( int i=0; i<n; i++ )
{
str += "1";
}

all_order(str,base);
}

int main(void)
{
A(3,4);

return 0;
}
zwh50687695 2009-07-09
  • 打赏
  • 举报
回复
就是这个意思哈 主要是想知道怎么实现循环嵌套次数可变的编码 要用递归吗?
Walf_ghoul 2009-07-09
  • 打赏
  • 举报
回复
先揣摩下楼主的意思,是说void A(int n,int m)
实现某种排列组合:n表示每组组合的数目,m表示数的范围是1~m。
可以这样理解的吧。。。。
Walf_ghoul 2009-07-09
  • 打赏
  • 举报
回复
先帮顶下。。。。
jixingzhong 2009-07-09
  • 打赏
  • 举报
回复
搜索一下以前的帖子
大前置 2009-07-09
  • 打赏
  • 举报
回复
帮顶~~
zwh50687695 2009-07-09
  • 打赏
  • 举报
回复
都没人看吗? 太让我伤心了。。。。。。。。。

69,369

社区成员

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

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