2020-03-23

#include
#include
#include
using namespace std;
#define N 64
bool isPeak(int grid[N][N],int i,int j);
int main(){
int nrows,ncols;
int map[N][N];
string filename;
ifstream file;
cout << “输入文件名” << endl;
cin >> filename;
file.open(filename.c_str());
if(file.fail()){
cout << “打开文件出错” << endl;
exit(1);
}
file >> nrows >> ncols;
if(nrows > N || ncols > N){
cout << “网格太大,调整程序” << endl;
exit(1);
}
//从数据文件中读取数据
for(int i=0;i for(int j=0;j file>>map[i][j];
}
}
//判断并打印峰值位置
for(int i=1;i for(int j=1;j if(isPeak(map,i,j)){ cout << “峰点出现在行” << i << “列” << j << endl;
}
}
}
system(“pause”);
file.close();
return 0;
}
bool isPeak(int grid[N][N],int i,int j){
if((grid[i][j]>grid[i-1][j])&&
(grid[i][j]>grid[i+1][j])&&
(grid[i][j]>grid[i][j+1])&&
(grid[i][j]>grid[i][j-1]))
return true;
else{
return false;
}
}

你可能感兴趣的:(笔记)