求一个小程序的写法 用C#写一个程序.如果输入参数为10.则输出.1 9 8 7 2 10 6 3 5 4如果输入是15则输出1 12 11 10 9 2 13 15 8 3 14 7 4 6 5输入的数为(行数的平方+行数)/2 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 本帖最后由 caozhy 于 2012-01-29 23:34:20 编辑 Console.WriteLine("请输入一个整数"); int count = Convert.ToInt32(Console.ReadLine()); int[,] array = new int[count, count]; int size = int.Parse(Math.Sqrt(array.Length).ToString()); int row = 0, column = 0, n = 1, num = 1; bool bo = true; while (bo) { row = column = n - 1; while (column <= size - n) { if (array[row, column] != 0) if (array[row, ++column] != 0) { bo = false; break; } array[row, column] = num++; column++; } if (!bo) break; column--; while (row <= size - n) { if (array[row, column] != 0) if (array[++row, column] != 0 || row == size-1) { bo = false; break; } array[row, column] = num++; row++; } if (!bo) break; row--; while (row == column) { row--; column--; if (array[row, column] != 0) break; array[row, column] = num++; } n++; } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Console.Write(array[i, j].ToString() + " "); } Console.WriteLine(); } Console.Read(); int inputNumber = 6; //输入参数 int row = Convert.ToInt32(Math.Sqrt(inputNumber * 2 + 1 / 4) - 1 / 2); int[,] array = new int[row, row]; int columnIndex = -1; int rowIndex = 0; int mode=0; //0 行走, 1 斜走 , 2 列走 for (int i=1; i<=inputNumber ; i++ ) { switch (mode) { case 0: columnIndex++; if ( columnIndex <row && array[rowIndex, columnIndex] == 0) { array[rowIndex, columnIndex] = i; } else { i--; columnIndex--; mode = 1; } break; case 1: rowIndex++; columnIndex--; if (rowIndex <row && columnIndex >=0 && array[rowIndex, columnIndex] == 0 ) { array[rowIndex, columnIndex] =i; } else { i--; rowIndex--; columnIndex++; mode = 2; } break; case 2: rowIndex--; if (rowIndex>=0 && array[rowIndex, columnIndex] == 0) { array[rowIndex, columnIndex] =i; } else { i--; rowIndex++; mode = 0; } break; } } for (int i = 0; i < row; i++) { for (int j = 0; j < row; j++) { Console.Write((array[j,i].ToString() + " ").ChangeString()); } Console.WriteLine(); } Console.Read(); } 简单总结一下:1、通过一元二次方程求根公式计算出行列数2、根据行列数建立一个数组3、按照以下规则填充数组 a.先向下,直到达到最大行数或者目标位置已经有数字 b.再向右上,直到达到第一行或者目标位置已经有数字 c.最后向左,直到目标位置已经有数字 d.如果还没有填充完,回到步骤a4、打印结果在填充数组的拐点有些小细节要注意。其他没什么难度。这个是通过模拟得到的结果,而不是通过数学计算。我想大部分人工作了几年之后,再做数学题都头疼……真正的算法应该是把上面的步骤3替换为数学算法,直接计算出每个位置的数字。 本帖最后由 caozhy 于 2012-02-02 11:26:20 编辑 设X行总占用格数: (x^2+x)/2 设X行总占用格数: (x^2+x)/2 WINFORM程序局域网访问 出现异常! 求助C# WinApi移动鼠标的操作 ~~~乌拉拉。。排序 System.DateTime.Now的前一天怎么表示 lock的问题!!! C#做个捕捉电脑声音的程序 grideview选择以后,上下移动 怎么用C#把网站某区域截取成图片? 在c# 中 如何在A类中 操作B类中的属性 或方法? 初学c#的问题! 怎么用JS做一下简单的效果 A页面ShowDialog B页面,B页面关闭时,怎么把一个变量给A页面
Console.WriteLine("请输入一个整数");
int count = Convert.ToInt32(Console.ReadLine());
int[,] array = new int[count, count];
int size = int.Parse(Math.Sqrt(array.Length).ToString());
int row = 0, column = 0, n = 1, num = 1;
bool bo = true;
while (bo)
{
row = column = n - 1;
while (column <= size - n)
{
if (array[row, column] != 0)
if (array[row, ++column] != 0)
{
bo = false;
break;
}
array[row, column] = num++;
column++;
}
if (!bo) break;
column--;
while (row <= size - n)
{
if (array[row, column] != 0)
if (array[++row, column] != 0 || row == size-1)
{
bo = false;
break;
}
array[row, column] = num++;
row++;
}
if (!bo) break;
row--;
while (row == column)
{
row--;
column--;
if (array[row, column] != 0)
break;
array[row, column] = num++;
}
n++;
} for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
Console.Write(array[i, j].ToString() + " ");
}
Console.WriteLine();
}
Console.Read();
int inputNumber = 6; //输入参数
int row = Convert.ToInt32(Math.Sqrt(inputNumber * 2 + 1 / 4) - 1 / 2);
int[,] array = new int[row, row];
int columnIndex = -1;
int rowIndex = 0; int mode=0; //0 行走, 1 斜走 , 2 列走 for (int i=1; i<=inputNumber ; i++ )
{
switch (mode)
{
case 0:
columnIndex++;
if ( columnIndex <row && array[rowIndex, columnIndex] == 0)
{
array[rowIndex, columnIndex] = i;
}
else
{
i--;
columnIndex--;
mode = 1;
}
break;
case 1:
rowIndex++;
columnIndex--;
if (rowIndex <row && columnIndex >=0 && array[rowIndex, columnIndex] == 0 )
{
array[rowIndex, columnIndex] =i;
}
else
{
i--;
rowIndex--;
columnIndex++;
mode = 2;
}
break;
case 2:
rowIndex--;
if (rowIndex>=0 && array[rowIndex, columnIndex] == 0)
{
array[rowIndex, columnIndex] =i;
}
else
{
i--;
rowIndex++;
mode = 0;
}
break;
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < row; j++)
{
Console.Write((array[j,i].ToString() + " ").ChangeString());
}
Console.WriteLine();
}
Console.Read();
}
1、通过一元二次方程求根公式计算出行列数
2、根据行列数建立一个数组
3、按照以下规则填充数组
a.先向下,直到达到最大行数或者目标位置已经有数字
b.再向右上,直到达到第一行或者目标位置已经有数字
c.最后向左,直到目标位置已经有数字
d.如果还没有填充完,回到步骤a
4、打印结果在填充数组的拐点有些小细节要注意。其他没什么难度。
这个是通过模拟得到的结果,而不是通过数学计算。我想大部分人工作了几年之后,再做数学题都头疼……
真正的算法应该是把上面的步骤3替换为数学算法,直接计算出每个位置的数字。
总占用格数: (x^2+x)/2
总占用格数: (x^2+x)/2