蛇行矩阵的解法
来源:互联网 发布:mysql sql语句编写 编辑:程序博客网 时间:2024/04/30 13:21
题目来源: http://www.programfan.com/acm/show.asp?qid=72#comment
蛇行矩阵
Problem
蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。
Input
本题有多组数据,每组数据由一个正整数N组成。(N不大于100)
Output
对于每一组数据,输出一个N行的蛇形矩阵。两组输出之间不要额外的空行。
矩阵三角中同一行的数字用一个空格分开。行尾不要多余的空格。
Sample Input
5
Sample Output
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
解法如下:
核心在于:顺次给相应的 a[row--][col++] 位置填上i++;
#include<iostream>#include<stdlib.h>using namespace std;int a[10][10]={0};void show(int n)//显示蛇形矩阵{ for(int i=0;i<n;i++) { for(int j=0;j<n-i;j++) { if(a[i][j]) { cout<<a[i][j]<<" "; } } cout<<endl; }}void snake(int n){ int cnt=1; int i=1; for(int j=0;j<n;j++) {int row=cnt-1;int col=0; while(row>=0&&col<=cnt) { a[row--][col++]=i++; //每次都是从a[cnt][0] 开始斜向上递增 直至row=0位置 } cnt++;}}int main(int argc, char* argv[]) { int n; cin>>n; cout<<endl; snake(n); show(n);system("PAUSE"); return 0;}
- 蛇行矩阵的解法
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵
- 蛇行矩阵算法
- fjnu 1304 蛇行矩阵
- 蛇行矩阵 [ 天羽 转载]
- 3281:蛇行矩阵
- hpuoj 1193:蛇行矩阵
- Problem I: 蛇行矩阵
- 1032: 蛇行矩阵
- 打印 蛇行矩阵
- 问题 1097: 蛇行矩阵
- 问题 : 蛇行矩阵
- [入门]蛇行矩阵
- How to choose WPF container controls
- 7种网络编程I/O模型代码实现实例
- TCMalloc
- 【读书笔记】【linux kernel development】【进程管理】
- 任正非与华为“诺亚方舟实验室”科学家的会谈纪要
- 蛇行矩阵的解法
- [OS] 进程地址空间与虚拟存储空间的理解 很好!!!
- adb push与adb install的区别
- 从数据库里查询数据,显示在jsp的下拉列表框里
- Android 错误 The method onClick(View) of type new View.OnClickListener(){} must override a superclas
- android4.0.3 修改启动动画和开机声音
- Android读取其他应用的SharedPreferences
- openssl交叉编译
- zju2005开门人和关门人