• 算法练习之1数字填充 下


    * http://acm.buaa.edu.cn/problem/167/
    *Problem Description
    * 晴天小猪喜欢玩数字游戏,但数独这样的游戏对他来说太难了,于是他准备玩一个容易点的游戏。
    * 游戏规则是在一个N*N的表格里填数,规则只有一句话:总是以对角线为起点,先横着填,再竖着填。
    * 游戏给了一些样例,请在样例里面找到规律并把这个表格打印出来吧。

    *Input
    *多组测试数据(数据量在100组以内)。每组测试数据只有一行为一个整数n(1<=n<=30),表示表格的大小。

    *Output

    * 对于每组输入数据,输出填完的表格(n行,每行n个整数,两两之间用空格隔开,注意不要在最后打印多余空格)。
    * 每两个表格之间用空行隔开,注意不要在第一行或者最后打印出多余的空行。

    *Sample Input
    *3
    *5
    *Sample Output
    *1 2 3
    *4 6 7
    *5 8 9

    *1 2 3 4 5
    *6 10 11 12 13
    *7 14 17 18 19
    *8 15 20 22 23
    *9 16 21 24 25

    C#代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Console_ShuZiTianChong
    {
        class Program
        {
     
            static void Main(string[] args)
            {
                Console.Write("请输入要测试的次数:");
                int num = Convert.ToInt32(Console.ReadLine());
                for (int i = 0; i < num; i++)
                {
                    Console.Write("请输入测试数字:");
                    int val = Convert.ToInt32(Console.ReadLine());
                    putInfo(val);
                }
                Console.ReadLine();
                Console.ReadLine();
            }
            static void putInfo(int val)
            {
                int[,] arr = new int[val, val];
                for (int i = 0; i < val; i++)
                {
                    for (int j = 0; j < val; j++)
                    {
                        arr[i, j] = GetVal(ref arr, i, j, val);
                    }
                }
    
                for (int i = 0; i < val; i++)
                {
                    for (int j = 0; j < val; j++)
                    {
                        Console.Write(arr[i, j] + " ");
                    }
                    Console.Write("\n");
                }
            }
    
            static int GetVal(ref int[,] arr, int x, int y, int len)
            {
                if (arr[x, y] != 0)
                {
                    return arr[x, y];
                }
                if (x == 0 && y == 0)
                {
                    return 1;
                }
                else if (x == y)
                {
                    return GetVal(ref  arr, len - 1, y - 1, len) + 1;
                }
                else if (x < y)
                {
                    return GetVal(ref  arr, x, y - 1, len) + 1;
                }
                else if (x == y + 1)
                {
                    return GetVal(ref  arr, x - 1, len - 1, len) + 1;
                }
                else
                {
                    return GetVal(ref  arr, x - 1, y, len) + 1;
                }
            }
        }
    }
  • 相关阅读:
    2013年工作中遇到的20个问题:81-100
    2013年工作中遇到的20个问题:81-100
    码农:客户是恶魔
    码农:客户是恶魔
    C# DataGridView 使用
    Java实现 LeetCode 203 移除链表元素
    Java实现 LeetCode 203 移除链表元素
    Java实现 LeetCode 202 快乐数
    Java实现 LeetCode 202 快乐数
    Java实现 LeetCode 202 快乐数
  • 原文地址:https://www.cnblogs.com/haowuji/p/2888877.html
Copyright © 2020-2023  润新知