读书人

小程序的写法

发布时间: 2012-02-20 21:18:23 作者: rapoo

求一个小程序的写法
用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





[解决办法]

C# code
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();
[解决办法]
C# code
            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替换为数学算法,直接计算出每个位置的数字。

读书人网 >C#

热点推荐