题目描述
编写一个函数void CollectItem(const int pInput[], const int M, const int N, int pOutput[]),pInput是通过一维数组承载的M行N列的二维矩阵(使用行优先顺序存放), 该函数的作用是找出该矩阵每列中的最小元素,并依次放入pOutput数组中。 void CollectItem(const int pInput[], const int M, const int N, int pOutput[]); 【输入】 pInput: 待处理的二维矩阵,使用一维数组承载, M: 二维矩阵行数 N: 二维矩阵列数 【输出】 pOutput: 矩阵每列的最小元素,空间已经开辟好,与二维矩阵列数等长; 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
输入
输入为二维矩阵的行数,列数,二维数组元素值。如 2,3,23,45,56,34,44,65。表示输入的二维矩阵行数为2,列数为3,各元素按行优先依次为23,45,56,34,44,65。
输出
输出结果为矩阵每列中的最小元素23, 44, 56。
样例输入
2,3,23,45,56,34,44,65
样例输出
23, 44, 56
提示
1.请实现本函数,并编写main函数完成完整输入/输出功能。
地区
南京研究所
public static void CollectItem(final int pInput[],final int M,final int N,int pOutput[]){
for(int i=0;i<N;i++){
int[] tempArr=new int[M];
for(int j=0;j<M;j++){//提取每列数据临时存储
tempArr[j]=pInput[i+j*N];
}
for(int j=0;j<M;j++){//冒泡排序(升序)
for(int k=0;k<M-j-1;k++){
if(tempArr[k]>tempArr[k+1]){
int temp=tempArr[k];
tempArr[k]=tempArr[k+1];
tempArr[k+1]=temp;
}
}
}
pOutput[i]=tempArr[0];//取最小的数据
}
for(int i=0;i<N;i++){
if(i==N-1){
System.out.print(pOutput[i]);
}else{
System.out.print(pOutput[i]+" ");
}
}
}
}
public static void main(String[] args) {
String input="2,3,23,45,56,34,44,65";
String[] inputArr=input.split(",");
int M=Integer.parseInt(inputArr[0]);
int N=Integer.parseInt(inputArr[1]);
int pInput[]=new int[M*N];
int pOutput[]=new int[N];
for(int i=0;i<N*M;i++){
pInput[i]=Integer.parseInt(inputArr[i+2]);
}
CollectItem(pInput,M,N,pOutput);
}