帮忙看下这段代码哪里有错,该如何修改,非常感谢!代码改错(语言-c语言)
1条回答 默认 最新
- a5156520 2022-11-19 19:11关注
应该是交换数组元素那里错了,然后再用一个临时变量存储其中一个的值,然后在交换他们的值即可,修改如下:
#include <iostream> using namespace std; const int MAX_SIZE = 10; int main(void){ int i,j,m,n; int mat[MAX_SIZE+1][MAX_SIZE]; cout<<"Please input m,n:"; cin>>m>>n; cout<<"Input array:\n"; for(i=0;i<m;i++){ for(j=0;j<n;j++){ cin>>mat[i][j]; } } int temp; for(i=m-1;i>0;i--){ for(j=0;j<n;j++){ //交换两个变量的值,用一个临时变量存储其中一个的值,然后再交换它们的值即可 temp= mat[(i+1)%m][j]; mat[(i+1)%m][j] = mat[i][j] ; mat[i][j] = temp; } } for(i=0;i<m;i++){ for(j=0;j<n;j++){ cout<<mat[i][j]<<" "; } cout<<endl; } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用