算法。

1,.N(1<=N<=9)个小熊分一堆苹果,第一只小熊将苹果分成N份,多了一个,扔掉,然后拿走自己的那一份。第二只熊将剩余的苹果分成N份,又多了一个,扔掉,然后拿走自己的那一份,第三只.....,直到第N只熊;问最初的苹果有多少个?

        public static int n =6;
        static void Main(string[] args)
        {
            for (int i = 15550; i <= Int32.MaxValue; i++)
            {
                if (solve(i*3+1))
                {
                    Console.WriteLine(n + " " + (i * 3 + 1));
                    break;
                }
            }
        }
        public static bool solve(int apple)
        {
            int b1 = (apple - 1) / 3;
            for (int i = 2; i < n + 1; i++)
            {
                if (!(((Math.Pow(n - 1, i - 1)) * (apple + n - 1)) % (Math.Pow(n, i)) == 0))
                { return false; }
            }
            return true;
        }

算法。_第1张图片

2.6*6的矩阵,从左上方开始,只经过向下或向右的步骤,到达右下方,找出经过的位置的最大价值;
200,120,400,150,180,300
150,250,360,120,200,130
350,300,250,100,500,260
100,150,260,320,100,150
500,130,260,100,200,170
160,100,250,200,600,200

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

namespace test2
{
    class Program
    {
        static void Main(string[] args)
        {
          int[][] value = new int[][] {new int[]{200,120,400,150,180,300},
               new int[] {150,250,360,120,200,130},
               new int[] {350,300,250,100,500,260},
               new int[] {100,150,260,320,100,150},
               new int[] {500,130,260,100,200,170},
               new int[] {160,100,250,200,600,200}};
              work(value);
            //show(value);
        }
     public static int work(int[][] value){
        int[][] maxValue = new int[][]{
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
        };
        int[][] path = new int[][]{
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
        };
        maxValue[0][0] = value[0][0];
        for (int i = 1; i < 6; i++) {
            maxValue[0][i] = value[0][i]+maxValue[0][i-1];
            path[0][i] =1;
        }
        for (int i = 1; i < 6; i++) {
            maxValue[i][0] = value[i][0]+maxValue[i-1][0];
            path[0][i] = 1;
        }
         
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 6; j++) {
                maxValue[i][j] =Math.Max(maxValue[i-1][j], maxValue[i][j-1])+value[i][j];
                if (maxValue[i - 1][j] > maxValue[i][j - 1])
                { path[i][j] = 0; }
                else { path[i][j] = 1; }
            }
        }
        show(maxValue);
        Console.WriteLine();
        show(path);
        Console.WriteLine();
        showstr(path);
        return maxValue[5][5];
    }
        public static void show(int[][] mar)
        {
            foreach (int[] i in mar)
            {
                foreach (int x in i)
                {
                    Console.Write(x.ToString() + "  ");
                }
                Console.WriteLine();
            }
        }
        public static void showstr(int[][] mar)
        {
           int[][] path = new int[][]{
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
            new int[]{0,0,0,0,0,0},
        };
            mar[0][0] = 1; mar[5][5] = 1;
            int i = 5; int j = 5;
            while (true)
            {
                if (!(i == 0 & j == 0))
                {
                    if (mar[i][j] == 0)
                    {
                       path[--i][j] = 1;
                    }
                    else
                    {
                        path[i][--j] = 1;
                    }
                }
                else { break; }
            }
            show(path);
        }
    }
}

算法。_第2张图片

思路最上,最左向右累加,其他部分取上方左方最大,累加下去。

路径思路,上面向右设为1,向下设为0,在第二个矩阵里进值,路径自最右下开始按方向开始,本题左左左上上上上上左左。结果为矩阵三

你可能感兴趣的:(算法。)