費氏數列

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

            static void Main(string[] args)
            {
                int n;
                Console.WriteLine("give me a number:");
                n = int.Parse(Console.ReadLine());
                Console.WriteLine(F(n));
                Console.Read();
            }

        static int F(int n)
          {
            if (n == 0)
                return 0;
            else if (n == 1)
                return 1;
            else
                return F(n-1)+F(n-2);

          }
        }
    }


魔術矩陣


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class MagicM
    {
        static void Main(string[] args)
        {
            do
            {
                int[,] mm;
                MagicM myClass1 = new MagicM();
                int n;
                Console.WriteLine("請輸入矩陣大小 : ");
                n = int.Parse(Console.ReadLine());

                if (n == 0)
                {
                    Console.WriteLine("請按 ENTER 離開 !! ");
                    break;
                }
                if (n % 2 == 0)
                {
                    Console.WriteLine("請輸入奇數 : ");
                    continue;
                }
                mm = new int[n, n];
                myClass1.AssingValue(n, mm);
                myClass1.PrintOut(n, mm);
                Console.WriteLine("\n");
            } while (true);
            Console.ReadLine();

        }

        void AssingValue(int n, int[,] mm)
        {
            int assingValue = 1;
            int p = n - 1;
            int column = p / 2;
            int row = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    mm[i, j] = 0;
                }
            }

            mm[row, column] = assingValue;

            do
            {
                assingValue++;
                column--;
                row--;
                if (column < 0 & row < 0)
                {
                    row += 2;
                    column += 1;
                }
                else
                {
                    if (column < 0)
                        column = p;

                    if (row < 0)
                        row = p;
                }
                if (mm[row, column] != 0)
                {
                    column += 1;
                    row += 2;
                }
                mm[row, column] = assingValue;

            } while (assingValue < n * n);

        }
        void PrintOut(int n, int[,] mm)
        {
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine();
                for (int j = 0; j < n; j++)
                {
                    Console.Write(mm[i, j] + "\t");
                }
            }
        }
        }
    }


arrow
arrow
    全站熱搜

    Joy 發表在 痞客邦 留言(0) 人氣()