这是一道非常水的排序题,只不过是将原来我们经常做的一维数组问题转化为二维数组,把一般的数值排序问题转化为绝对值的排序问题,但是题目相对来说没什么难度,可是第一次写的这个WA了3次,不知道是怎么错的,请大家帮我看看,,知道的M下,谢谢了~
# include<cstdio> # include<iostream> # include<cstring> # include<cmath> using namespace std; # define MAX 10000 # define xh1 int i = 1;i <= n;i++ # define xh2 int j = 1;j <= m;j++ long a[MAX+10][MAX+10]; int main(void) { int n,m; int flag = 0; memset(a,0,sizeof(a)); while ( cin>>n>>m ) { for ( xh1 ) { for ( xh2 ) { cin>>a[i][j]; } } int max = abs(a[0][0]); for ( xh1 ) { for ( xh2 ) { for ( int k = j;k <= m;k++ ) { if ( max <= abs(a[i][k]) ) { max = abs(a[i][j]); } } } } int max2 = -max; for ( xh1 ) { for ( xh2 ) { if ( a[i][j]== max2 ) { cout<<i<<" "<<j<<" "<<max2<<endl; flag = 1; break; } if ( a[i][j] == max ) { cout<<i<<" "<<j<<" "<<max<<endl; flag = 1; break; } } if ( flag ) break; } //cout<<max2<<endl; } return 0; }
于是转化思路,用结构体的思想来处理这个问题,下面是代码:
# include<cstdio> # include<iostream> # include<algorithm> using namespace std; # define MAX 10000 # define xh1 int i = 1;i <= n;i++ # define xh2 int j = 1;j <= m;j++ struct node { int m; int x; int y; }w[MAX+10]; bool compare( node x2,node y2 ) { if ( abs(x2.m)>abs(y2.m) ) return true; if ( abs(x2.m)==abs(y2.m)&&x2.x<y2.x ) return true; if ( abs(x2.m)==abs(y2.m)&&x2.x==y2.x&&x2.y<y2.y ) return true; return false; } int main(void) { int n,m,q; while ( cin>>n>>m ) { int ans = 0; for ( xh1 ) { for ( xh2 ) { cin>>q; w[ans].x = i; w[ans].y = j; w[ans].m = q; ans++; } } sort(w,w+ans,compare); cout<<w[0].x<<" "<<w[0].y<<" "<<w[0].m<<endl; } return 0; }