#include<cstdlib>
#include<cmath>
#include<ctime>
using namespace std;
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{const int m=5,n=6;
int a[m][n];
srand(time(NULL));
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]=rand()%100;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<'\t';
}
cout<<endl;
}
cout<<endl;
cout<<endl;
// /选择排序
for(int i=0;i<m;i++)
for(int j=0;j<n-1;j++)
{
for(int p=i;p<m;p++)
for(int q=j+1;q<n;q++)
{
if(a[i][j]>a[p][q])
swap(&a[i][j],&a[p][q]);
}
}
//show
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<'\t';
}
cout<<endl;
}
return 0;
}
7 个解决方案
#1
自己顶下 求排序
#2

参考大版主的:
#include <stdio.h>
#include <stdlib.h>
void sort(int *num)
{
int i, j, t;
for(i=0; i<5*6-1; i++)
for(j=i+1; j<5*6; j++)
if(num[i]>num[j])
{
t = num[i];
num[i] = num[j];
num[j] = t;
}
}
int main()
{
int num[5][6];
int c, r;
/*Imput*/
printf("Please Input the Numbers:\n");
for(r=0; r<5; r++)
for(c=0; c<6; c++)
scanf("%d", &num[r][c]);
/*Sort*/
sort(num);
/*Output*/
printf("\nAfter Sort, the Numbers:\n");
for(r=0; r<5; r++)
{
for(c=0; c<6; c++)
printf("%d\t", num[r][c]);
printf("\n");
}
system("PAUSE");
return 0;
}
#3

#4
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp||In function ‘int main()’:|
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp|29|错误:不能将‘void sort(int*)’的实参‘1’从‘int (*)[6]’转换到‘int*’|
||=== 已完成构建: 1 个错误, 0 个警告 ===|
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp|29|错误:不能将‘void sort(int*)’的实参‘1’从‘int (*)[6]’转换到‘int*’|
||=== 已完成构建: 1 个错误, 0 个警告 ===|
#5
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp||In function ‘int main()’:|
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp|29|错误:不能将‘void sort(int*)’的实参‘1’从‘int (*)[6]’转换到‘int*’|
||=== 已完成构建: 1 个错误, 0 个警告 ===|
大神怎么搞
#6
不保证对,仅供参考:
#include <stdio.h>
#include <stdlib.h>
int num[5][6];
void sort() {
int i, j, t;
int *n;
n=(int *)num;
for(i=0; i<5*6-1; i++)
for(j=i+1; j<5*6; j++)
if(n[i]>n[j]) {
t = n[i];
n[i] = n[j];
n[j] = t;
}
}
int main() {
int c, r;
/*Imput*/
printf("Please Input the Numbers:\n");
for(r=0; r<5; r++)
for(c=0; c<6; c++)
scanf("%d", &num[r][c]);
/*Sort*/
sort();
/*Output*/
printf("\nAfter Sort, the Numbers:\n");
for(r=0; r<5; r++) {
for(c=0; c<6; c++)
printf("%d\t", num[r][c]);
printf("\n");
}
system("PAUSE");
return 0;
}
#7
自己解决了哈哈哈
int *p=&a[0][0];
#1
自己顶下 求排序
#2

参考大版主的:
#include <stdio.h>
#include <stdlib.h>
void sort(int *num)
{
int i, j, t;
for(i=0; i<5*6-1; i++)
for(j=i+1; j<5*6; j++)
if(num[i]>num[j])
{
t = num[i];
num[i] = num[j];
num[j] = t;
}
}
int main()
{
int num[5][6];
int c, r;
/*Imput*/
printf("Please Input the Numbers:\n");
for(r=0; r<5; r++)
for(c=0; c<6; c++)
scanf("%d", &num[r][c]);
/*Sort*/
sort(num);
/*Output*/
printf("\nAfter Sort, the Numbers:\n");
for(r=0; r<5; r++)
{
for(c=0; c<6; c++)
printf("%d\t", num[r][c]);
printf("\n");
}
system("PAUSE");
return 0;
}
#3

#4
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp||In function ‘int main()’:|
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp|29|错误:不能将‘void sort(int*)’的实参‘1’从‘int (*)[6]’转换到‘int*’|
||=== 已完成构建: 1 个错误, 0 个警告 ===|
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp|29|错误:不能将‘void sort(int*)’的实参‘1’从‘int (*)[6]’转换到‘int*’|
||=== 已完成构建: 1 个错误, 0 个警告 ===|
#5
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp||In function ‘int main()’:|
/home/hacker/workspace/erweishuzhupaixu /secondtry.cpp|29|错误:不能将‘void sort(int*)’的实参‘1’从‘int (*)[6]’转换到‘int*’|
||=== 已完成构建: 1 个错误, 0 个警告 ===|
大神怎么搞
#6
不保证对,仅供参考:
#include <stdio.h>
#include <stdlib.h>
int num[5][6];
void sort() {
int i, j, t;
int *n;
n=(int *)num;
for(i=0; i<5*6-1; i++)
for(j=i+1; j<5*6; j++)
if(n[i]>n[j]) {
t = n[i];
n[i] = n[j];
n[j] = t;
}
}
int main() {
int c, r;
/*Imput*/
printf("Please Input the Numbers:\n");
for(r=0; r<5; r++)
for(c=0; c<6; c++)
scanf("%d", &num[r][c]);
/*Sort*/
sort();
/*Output*/
printf("\nAfter Sort, the Numbers:\n");
for(r=0; r<5; r++) {
for(c=0; c<6; c++)
printf("%d\t", num[r][c]);
printf("\n");
}
system("PAUSE");
return 0;
}
#7
自己解决了哈哈哈
int *p=&a[0][0];