题解 | #"之"字形打印矩阵#

"之"字形打印矩阵

http://www.nowcoder.com/practice/d2efe600e73d47a2ba1533dc926cbb46

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int arr[n][m];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)
            cin>>arr[i][j];
    }
    int x1=0,y1=0,x2=0,y2=0;//分别代表行目前的坐标和列目前的坐标
    bool flag=true;//true是从左下到右上方向,false是右上到左下方向
    for(int i=0;i<m+n-1;i++){//总的循环次数是边长之和减一因为顶点处重合了
        if(flag){
            int tempx=x1,tempy=y1;
            while(tempx>=x2){
                cout<<arr[tempx][tempy]<<" ";
                tempx--;
                tempy++;
            }
        }
        else{
            int tempx=x2,tempy=y2;
            while(tempx<=x1){
                cout<<arr[tempx][tempy]<<" ";
                tempx++;
                tempy--;
            }
        }
        flag=!flag;//方向取反
        if(x1==n-1){
            y1++;
        }
        else x1++;
        if(y2==m-1)
            x2++;
        else y2++;
    }
}
全部评论

相关推荐

zzzzhz:兄弟你先猛猛投简历至少三百家,能约到面试就去面。最近可以速成智能小车,智慧家居烂大街的项目,不需要自己写,只需要把里面的代码讲解看明白就行。把其中涉及到的八股文都拿出来单独背一下,我去年找工作就一个智能小车智慧家居找了10k差不多。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-24 13:32
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务