weixin_40071324
世事随风11
2017-10-18 15:43

C++程序修改,关于布尔矩阵的。

  • c++

下面这段程序会自动创建所有n阶(暂定最大6阶)布尔矩阵,判断并输出在矩阵的t次⊙乘后得到的布尔矩阵满足有n个元素为1。
程序除主函数外各个小函数已测试,现在就是真正输出的矩阵可能有k≠n个元素为1,怎么办?

#include
using namespace std;

void VectorX(int *a,int *b,int *c,int size) {
int final[36]={0,};
for(int x=0;x<size;x++){
for(int y=0;y<size;y++){
for(int z=0;z<size;z++){
if(a[x*size+z]==1&&b[z*size+y]==1){
final[x*size+y]=1;}}}}
for(int i=0;i<size*size;i++)
c[i]=final[i];
}

void PackageX(int *a,int *b,int *c,int size,int times=2) {
for(int i=1;i<times;i++) {
VectorX(a,b,c,size);}
}

int CountTrue(int *a,int size,int &count) {
for(int i=1;i<size;i++) {
if(a[i]==1) {
count=count+1;}}

return count;

}

void PrintVector(int a,int size) {
static int count=0;
count=count+1;
cout<<" 第"<<count<<"个满足条件矩阵"<<endl;
cout<<"
***********************************************"<<endl;
for(int i=0;i<size;i++){
for(int j=0;j<size;j++)
cout<<a[i*size+j]<<" ";
cout<<endl;}
}

void MakeAndJudge(int size,int road,int times,int needroad=0,int sign=0) {
static int result[36]={0,};
static int Temp[36]={0,};
if(roadsize*size) {}

else if(!(sign==size*size)){
    result[sign]=0;
    Temp[sign]=0;
    MakeAndJudge(size,road,times,needroad,sign+1);
    result[sign]=1;
    Temp[sign]=1;
    MakeAndJudge(size,road-1,times,needroad,sign+1);}

if(sign==size*size&&road==0){
    PackageX(result,Temp,result,size,times);

    int flag=0;
    CountTrue(result,size,flag);
    if(flag==needroad) {
        PrintVector(Temp,size);
        cout<<endl;}
}
else {}

}

int main() {
/*
MakeAndJudge(6,6,6,1);
*/
int a[36]={0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,0};
int b[36]={0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,0};
VectorX(b,a,b,6);
VectorX(b,a,b,6);
VectorX(b,a,b,6);
VectorX(b,a,b,6);
VectorX(b,a,b,6);
PrintVector(b,6);

int c[36]={0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,0};
int d[36]={0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,0};
PackageX(c,d,c,6,6);
PrintVector(c,6);

return 0;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答