亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何利用隨機函數 rand() 產生一個 5 × 5 的二維數組 array?

如何利用隨機函數 rand() 產生一個 5 × 5 的二維數組 array?

C++ C
皈依舞 2022-04-15 18:11:49
利用隨機函數 rand() 產生一個 5 × 5 的二維數組 array ,且元素值不大于 10 ;找出該數組中的鞍點,即該位置上的元素在該行上最大,在該列上最小。也可能沒有鞍點。注:隨機函數 rand() 在頭文件 stdlib.h 中。
查看完整描述

2 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

//呃……你還跑到這里問來著……
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{

struct sl
{
int num;
int p;
int q;
};
int t=0;

srand(unsigned (time(NULL)));

int arrary[5][5];
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
arrary[i][j]=rand()%11;

cout<<"隨機生成的5*5的數列為"<<endl;
cout<<endl;

for(i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<arrary[i][j]<<"\t";
if(j==4)
{
cout<<endl;
cout<<endl;
}
}
}

//取每一行最大數為big,每一列最小數為small。
sl big[5];
sl small[5];
for(i=0;i<5;i++)
{
big[i].num=0;
small[i].num=11;
}
for(i=0;i<5;i++)
{

for(int j=0;j<5;j++)
{
if(big[i].num<arrary[i][j])
{
big[i].num=arrary[i][j];
big[i].p=i+1;
big[i].q=j+1;
}

if(small[i].num>arrary[j][i])
{
small[i].num=arrary[j][i];
small[i].p=j+1;
small[i].q=i+1;
}
}
}
int s;
for(i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if((small[i].p==big[j].p)&&(small[i].q==big[j].q))
cout<<"第"<<small[i].p<<"行"<<"\t"<<"第"<<small[i].q<<"列"<<endl;
else
s=1;
}
}
if(s)
cout<<"不存在鞍點!"<<endl;

cout<<endl;

cout<<"每行最大值"<<"\t"<<"所在行數"<<"\t"<<"所在列數"<<endl;
for(i=0;i<5;i++)
{
cout<<big[i].num<<"\t\t"<<big[i].p<<"\t\t"<<big[i].q;
cout<<endl;
}
cout<<"每列最小值"<<"\t"<<"所在行數"<<"\t"<<"所在列數"<<endl;
for(i=0;i<5;i++)
{
cout<<small[i].num<<"\t\t"<<small[i].p<<"\t\t"<<small[i].q;
cout<<endl;
}
return 0;

}



查看完整回答
反對 回復 2022-04-19
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

給,已經編譯運行確認:

#include<conio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;

#define N 5

int main()
{
int i,j;
int row,colum,flag=1,max_colum,max,dummy_row;
int array[N][N]={0};
srand((unsigned)time(NULL));

for(i=0;i<N;i++)
for(j=0;j<N;j++)
array[i][j]=rand();

cout<<"The Array is: "<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
cout<<array[i][j]<<'\t';

cout<<endl;
}

for(row=0;row<N;row++)
{
max=array[row][0];
max_colum=0;
flag=1;
for(colum=0;colum<N;colum++)
{
if(max<array[row][colum])
{
max=array[row][colum];
max_colum=colum;
}
}
for(dummy_row=0;dummy_row<N;dummy_row++)
if(max>array[dummy_row][max_colum])
{
flag=0;
}
if(flag)
{
cout<<"鞍點為"<<max<<",在"<<row+1<<"行"<<max_colum+1<<"列"<<endl;
break;
}
}
if(flag==0) cout<<"此矩陣無鞍點"<<endl;

getch();
return 1;
}



查看完整回答
反對 回復 2022-04-19
  • 2 回答
  • 0 關注
  • 306 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號