squarePatternPrint
来源:互联网 发布:电子图书数据库有哪些 编辑:程序博客网 时间:2024/06/18 04:59
//给定一个整数n, 以下列方式打印n行://如果n = 4, 生成的阵列为//1 * 2 * 3 * 4//9 * 10 * 11 * 12//13 * 14 * 15 * 16//5 * 6 * 7 * 8//每行只能输出数字和*,不能有空格#include <iostream>#include <vector>#include <queue>using namespace std;int main(){int n = 0;cin >> n;queue<int> q;for (int i = 1; i <= n*n; i++)q.push(i);//把1 2 3 .....n*n放到一个队列里面vector <vector <int>> vv;for (int i = 0; i < n; i++)vv.push_back(vector<int>());//填充vv的大小为nbool flag = true;int x = 0, y = n - 1;for (int i = 0; i < n; i++){vector<int> v;if (flag){for (int i = 0; i < n; i++){int t = q.front();q.pop();v.push_back(t);}vv[x] = v;x++;flag = false;}else{for (int i = 0; i < n; i++)//每一行的数据也是n个{int t = q.front();q.pop();v.push_back(t);}vv[y] = v;y--;flag = true;}}for (int i = 0; i < n; i++)//打印输出vv{for (int j = 0; j < n - 1; j++){cout << vv[i][j] << "*";}cout << vv[i][n - 1] << endl;}return 0;}
0 0
- squarePatternPrint
- 类(class)简单总结
- [LeetCode] 125. Valid Palindrome
- 关于springmvc 刷新action的方法重复提交的问题 重复执行
- 《从零开始学Swift》学习笔记(Day 22)——闭包那些事儿!
- Android10--Android之异步任务加载网页和图片
- squarePatternPrint
- ios developer tiny share-20160829
- 淘宝TFS安装
- toLowerCase和toLocaleLowerCase的区别
- 在 Linux 虚拟机中手动安装或升级 VMware Tools
- js实现标准无缝滚动
- 场景示例 Nginx 访问日志
- 使用js实现带有停顿效果的图片滚动(按钮控制)
- 记录学习的点滴(Eclipse+Spring+Mybatis搭建问题总结)