蛇形矩阵(百度2016实习招聘)
2016-04-04 11:52
211 查看
#include "stdafx.h" #include <iostream> #include <vector> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int m,n; cin >>m >> n; vector<vector<int> > vec(m, vector<int>(n)); int count = 1; int startx = 0; int starty = 0; int endx = m - 1; int endy = n - 1; int i, j; while (startx <= endx && starty<=endy){ for (j = starty; j <= endy; j++){ vec[startx][j] = count++; } for (i = startx+1; i <= endx; i++){ vec[i][endy] = count++; } for (j = endy - 1; j >= starty; j--){ vec[endx][j] = count++; } for (i = endx - 1; i > startx; i--){ vec[i][starty] = count++; } startx++; starty++; endx--; endy--; } for (i = 0; i < m; i++){ for (j = 0; j < n; j++){ cout << vec[i][j] << ends; } cout << endl; } return 0; }
相关文章推荐
- JavaWeb - Http协议
- 实现单点登录的思路
- Mysql db
- java动态代理技术
- java.util.concurrent包 以及 线程池的使用。
- RMI、RPC、SOAP通信技术介绍及比对
- 2014 打印图形
- 冒泡选择插入快速,四种最基础排序算法实现
- 做个毕设也不容易之购物车模块遇到java.lang.IllegalArgumentException
- SWUST oj 2478 最短路问题
- Android开发实用技巧之四:自定义ActivityCollector来管理活动
- boost::format小结
- 使用Jsoup抓取页面的数据
- DAY2:leetcode #2 Add Two Numbers
- spring学习--4
- 重构
- 清明节感想
- Python中排序sort 、sorted和argsort函数
- 解决time_wait过多的问题
- 封装了一个类,可生成验证码,缩略图,及水印图