c#输出螺旋矩阵的一种拙解
来源:互联网 发布:js怎么禁止点击事件 编辑:程序博客网 时间:2024/04/20 04:36
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n=0 ; //矩阵大小
Console.Write("请输入矩阵行列的大小(N*N):");
do
{
try
{
n = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.Write("输入有误,请重新输入一个正整数:");
continue;
}
if (n <= 0)
{
Console.Write("输入有误,请重新输入一个正整数:");
continue;
}
}
while(n<=0);
int num=n; //方向上的个数
int x=0, y=0; //座标
int number= 1; //起始数据
int[,] arraya = new int[n, n];
int c;
if ((n + 1) % 2 == 0)
c = (n + 1) / 2;
else
c = n / 2;
for (int i = 1; i <= c; i++)
{
for(int j=1;j<=num;j++)
arraya[x++,y]=number++;
x--;
y++;
num--;
for(int j=1;j<=num;j++)
arraya[x,y++]=number++;
y--;
x--;
for(int j=1;j<=num;j++)
arraya[x--,y]=number++;
x++;
y--;
num--;
for(int j=1;j<=num;j++)
arraya[x,y--]=number++;
y++;
x++;
}
for (int i = 0; i <= n-1; i++)
{
for (int j = 0; j <= n-1; j++)
{
if (arraya[i, j] < 10)
Console.Write(" " + arraya[i, j].ToString() + " ");
else if(arraya[i, j] < 100)
Console.Write(" " + arraya[i, j].ToString() + " ");
else
Console.Write(arraya[i, j].ToString() + " ");
}
Console.Write("/n");
}
Console.ReadKey();
}
}
}
- c#输出螺旋矩阵的一种拙解
- 【每日面试题】矩阵的螺旋输出
- 输出螺旋矩阵(一)
- 输出螺旋矩阵(二)
- 输出螺旋矩阵(三)
- 矩阵式螺旋输出
- 矩阵式螺旋输出
- 矩阵式螺旋输出
- 矩阵式螺旋输出
- 输出螺旋矩阵
- 输出螺旋矩阵
- 输出螺旋矩阵
- 输出螺旋矩阵
- JavaScript输出螺旋矩阵
- 螺旋矩阵输出
- 矩阵螺旋输出
- 螺旋输出N*N矩阵
- C语言输出螺旋矩阵
- static和final修饰类属性变量及初始化
- 面试题及答案 (JAVA)
- 链接服务器的配置和使用(SQL Server)
- C#做文件內容搜索
- 栈
- c#输出螺旋矩阵的一种拙解
- 3-OSWorkflow源码分析
- Cxgrid编辑后不能自动保存
- How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings
- 别了,理想的需求管理
- 最近学习(2.26)
- 链表
- 程序操控浏览器控件(IE)填写网页表单
- HeadFirst Java Appendix A(打鼓器1.0-照书搬代码,还需要优化)