问题标题: 酷町堂:4701 螺旋输出矩阵 60分 急急急!!!

0
0
已解决
李显晨
李显晨
中级启示者
中级启示者
4701   螺旋输出矩阵
经验值:0
题目描述 Description
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

输入描述 Input Description
第一行,输入正整数m和n,
接下来m行,
每行输入n个正整数。

输出描述 Output Description
输出一行,按照顺时针螺旋顺序,输出矩阵中的所有元素,元素之间用空格隔开。

样例输入 Sample Input
3 3
1 2 3 
4 5 6 
7 8 9
样例输出 Sample Output
1 2 3 6 9 8 7 4 5
数据范围及提示 Data Size & Hint
n,m<=9 ,1<= 数组元素<=100

以下是我的错误代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
int a[110][110]; 
int main(){
    int m,n,s=1;
    cin>>m>>n;
    int ht=1,hw=m,lt=1,lw=n;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j]; 
        }
    }
    while(s<=m*n){
        for(int i=lt;i<=lw;i++){
            cout<<a[ht][i]<<" ";
            s++;
        }
        ht++;
        for(int i=ht;i<=hw;i++){
            cout<<a[i][lw]<<" ";
            s++;
        }
        lw--;
        for(int i=lw;i>=lt;i--){
            cout<<a[hw][i]<<" ";
            s++;
        }
        hw--;
        for(int i=hw;i>=ht;i--){
            cout<<a[i][lt]<<" ";
            s++;
        }
        lt++;
    }
    return 0;
}

请大佬们找错 急急急!!!


0
已采纳
蔡乐毅
蔡乐毅
高级光能
高级光能

来喽!

在21,26,31,36这几行后要加一个if

  • if(hw-ht==-1){
    • return 0;
  • }
  • 21 31后面要加的判断
  • if(lw-lt==-1){
    • return 0;
  • }
  • 26 36后面要加的判断

望采纳

0
0
徐紫尘
徐紫尘
高级光能
高级光能

我跟你用的不是一种方法

0
徐紫尘
徐紫尘
高级光能
高级光能

我跟你用的不是一种方法

0
徐紫尘
徐紫尘
高级光能
高级光能

我跟你用的不是一种方法

0
徐紫尘
徐紫尘
高级光能
高级光能
#include<iostream>
using namespace std;
int a[15][15];
int main(){
    int m,n,cnt=0;
    cin>>m>>n;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            cin>>a[i][j];
    定义 x=1,y=0;
    while(cnt<n*m){
        while(y<n&&a[x][y+1]!=0){
            输出a[x][++y]
            a[x][y]=0;
            计数器++
        }

    while(x<m&&a[x+1][y]!=0){
        输出[++x][y]<<" ";
        a[x][y]=0;
        计数器++
    }
    while(y>1&&a[x][y-1]!=0){
        输出a[x][--y]<<" ";
        a[x][y]=0;
        计数器++
    }
        while(x>1&&a[x-1][y]!=0){
            输出<a[--x][y]<<" ";
            a[x][y]=0;
            cnt++;
        }
    }
    return 0;
}

 

我要回答