1.4 对一个int数组进行排序,使得每一趟都有一个最大的元素放在首部。
void maxArray(int a[],int length)
{
for(int i=0;i<length;i++)
{
for(int j=i+1;j<length;j++)
{
if(a[i]<a[j])
swap(a[i],a[j]);
}
for(int q=0;q<length;q++)
{
cout<<a[q]<<" ";
}
cout<<endl;
}
for(i=0;i<length;i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int array[]={3,4,1,2,5,8,7};
int length=sizeof(array)/sizeof(array[0]);
maxArray(array,length);
}
1.5 一个3*3的数组,fun1()输出主对角线元素和,fun()输出副对角线元素和
void fun1(int array[][3])
{
int i,j,sum=0;
for(i=0,j=0;i<3,j<3;i++,j++)
{
if(i==j)
sum+=array[i][j];
}
cout<<"主对角线的和是:"<<sum<<endl;
}
void fun2(int array[][3])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if((i+j)==2)
sum+=array[i][j];
}
}
cout<<"副对角线的和是:"<<sum<<endl;
}
int main()
{
int array[][3]={{1,2,3},{4,5,6},{7,8,9}};
fun1(array);
fun2(array);
return 0;
}
1.6 在一个二维数组中,寻找所有满足以下要求的点:在行中最大,在列中最小(马鞍数)
void fun(int a[][10],int row,int column)
{
for(int i=0;i<row;i++)
{
int max=a[i][0];
int max_column=0;
for(int j=1;j<column;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
max_column=j;
}
}
int flag=0;
for(int k=0;k<row;k++)
{
if(a[k][max_column]<max)
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<"in the "<<i+1<<" row,the maanshu is "<<max<<" in the "<<max_column+1<<" column"<<endl;
}
}
}
int main()
{
int row,column;
cout<<"Please enter the row and column of the matrix:"<<endl;
cin>>row>>column;
int a[10][10];
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cin>>a[i][j];
}
}
cout<<"the matrix is:"<<endl;
for(int i2=0;i2<row;i2++)
{
for(int j2=0;j2<column;j2++)
{
cout<<a[i2][j2]<<" ";
}
cout<<endl;
}
fun(a,row,column);
return 0;
}