有意思的题哦! 在m个数中求n个数的所有组合(n<m)!怎么整?我都不知道怎么下手,哎,太菜了,高手指点啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 主 题: 一个看似简单,可是越想越不知下手! a[n][m] 当n=1;m=2; (1)a[1][1]=x; (2)a[1][2]=x 当n=2;m=2 (1){a[1][1]=x,a[2][1]=x}; (2){a[1][1]=x,a[1][2]=x}; (3){a[1][2]=x,a[2][1]=x}; (4){a[1][2]=x,a[2][2]=x} 当n=3;m=2 (1){a[1][1]=x,a[2][1]=x,a[3]a[1]=x};(2){a[1][1]=x,a[2][1]=x,a[3][2]=x;} (3){a[1][1]=x,a[2][2]=x,a[3]a[1]=x};(4){a[1][1]=x,a[2][2]=x,a[3][2]=x;} (5){a[1][2]=x,a[2][1]=x,a[3]a[1]=x};(6){a[1][2]=x,a[2][1]=x,a[3][2]=x;} (7){a[1][2]=x,a[2][2]=x,a[3]a[2]=x};(8){a[1][2]=x,a[2][2]=x,a[3][2]=x;} 当n=4;m=2; ................... 当n=5;m=2 ..................... 现假设M不变为2减少难度,N是可以改变的也就是有2^N的组合赋值方式请问这种算法 这样解决啊,这样编! //如若你要对M^N个数据赋值, //可以用一维数组来模拟多维数组。 //比如 //int a[2][2][2]; //你可以用 b[2*2*2]来代替, //而使用的时候 a[1][0][1]就是b[1*2*2+0*2+1] //举例:M=每维下标 N=维数 #define M 3 #define N 4 long* pData; // data[3][3][3][3] #include <iostream> #include <math.h> //#include <stdio.h> using namespace std; int main() { char str[100]; char strtemp1[100]; char strtemp2[100]; int len=pow(M,N); pData=new long[len]; int i; for(i=0;i<len;i++)pData[i]=i;//赋值 //显示 for(i=0;i<len;i++) { int j; int temp=i; strcpy(str,""); strcpy(strtemp1,""); strcpy(strtemp2,""); for(j=0;j<N;j++) { itoa(temp%M,strtemp2,sizeof(strtemp2)); strcat(strtemp1,"["); strcat(strtemp1,strtemp2); strcat(strtemp1,"]"); temp/=M; strcat(strtemp1,str); strcpy(str,strtemp1); strcpy(strtemp1,""); } strcpy(strtemp1,"\nData"); strcat(strtemp1,str); cout<<strtemp1<<"="<<pData[i];//数值 } cout<<endl; return 0; } 运行结果 Data[0][0][0][0]=0 Data[0][0][0][1]=1 Data[0][0][0][2]=2 Data[0][0][1][0]=3 //省略 Data[2][2][2][0]=78 Data[2][2][2][1]=79 Data[2][2][2][2]=80 最后还应该 delete[] pData; 求一个正则表达式, 大于0 的非负数或非负小数,小数点后保留4位 急 JAVA多线程 Mina编码协议问题 [求助]关于JAVA远程通信 java冒泡法的疑问 跪求!!JAVA迷茫 SWT如何从一个界面调用另外一个界面? 关于java中sql插入语句的参数疑问 java 简单正则表达式 如何使继承于JLabel的一renderer能接受鼠标事件? //照书上的示例抄下来,编译错误,但我又找不出误,急急急........ 为什么要了解对象的生命周期?
a[n][m]
当n=1;m=2;
(1)a[1][1]=x;
(2)a[1][2]=x
当n=2;m=2
(1){a[1][1]=x,a[2][1]=x};
(2){a[1][1]=x,a[1][2]=x};
(3){a[1][2]=x,a[2][1]=x};
(4){a[1][2]=x,a[2][2]=x}
当n=3;m=2
(1){a[1][1]=x,a[2][1]=x,a[3]a[1]=x};(2){a[1][1]=x,a[2][1]=x,a[3][2]=x;}
(3){a[1][1]=x,a[2][2]=x,a[3]a[1]=x};(4){a[1][1]=x,a[2][2]=x,a[3][2]=x;}
(5){a[1][2]=x,a[2][1]=x,a[3]a[1]=x};(6){a[1][2]=x,a[2][1]=x,a[3][2]=x;}
(7){a[1][2]=x,a[2][2]=x,a[3]a[2]=x};(8){a[1][2]=x,a[2][2]=x,a[3][2]=x;}
当n=4;m=2;
...................
当n=5;m=2
.....................
现假设M不变为2减少难度,N是可以改变的也就是有2^N的组合赋值方式请问这种算法
这样解决啊,这样编!
//如若你要对M^N个数据赋值,
//可以用一维数组来模拟多维数组。
//比如
//int a[2][2][2];
//你可以用 b[2*2*2]来代替,
//而使用的时候 a[1][0][1]就是b[1*2*2+0*2+1]
//举例:M=每维下标 N=维数
#define M 3
#define N 4
long* pData; // data[3][3][3][3]
#include <iostream>
#include <math.h>
//#include <stdio.h>
using namespace std;
int main()
{
char str[100];
char strtemp1[100];
char strtemp2[100];
int len=pow(M,N);
pData=new long[len];
int i;
for(i=0;i<len;i++)pData[i]=i;//赋值
//显示
for(i=0;i<len;i++)
{
int j;
int temp=i;
strcpy(str,"");
strcpy(strtemp1,"");
strcpy(strtemp2,"");
for(j=0;j<N;j++)
{
itoa(temp%M,strtemp2,sizeof(strtemp2));
strcat(strtemp1,"[");
strcat(strtemp1,strtemp2);
strcat(strtemp1,"]");
temp/=M;
strcat(strtemp1,str);
strcpy(str,strtemp1);
strcpy(strtemp1,"");
}
strcpy(strtemp1,"\nData");
strcat(strtemp1,str);
cout<<strtemp1<<"="<<pData[i];//数值
}
cout<<endl;
return 0;
}
运行结果
Data[0][0][0][0]=0
Data[0][0][0][1]=1
Data[0][0][0][2]=2
Data[0][0][1][0]=3
//省略
Data[2][2][2][0]=78
Data[2][2][2][1]=79
Data[2][2][2][2]=80
最后还应该
delete[] pData;