三角形中非质数的最大路径总和

问题描述 投票:1回答:1

最终结果是什么!!!我的8219和我被告知这是错误的!!

您将从文件中输入正交三角形,您需要根据下面的给定规则找到数字的最大和;

  1. 您将从顶部开始,然后向下移动到下面的相邻数字。
  2. 只允许您对角向下走。
  3. 您只能走过非主要数字。
  4. 您必须尽可能地到达金字塔的尽头。
  5. 您必须将输入视为金字塔。

根据上述规则,在下面的示例中,从上到下的数字最大和为24。

   *1
  *8 4
 2 *6 9
8 5 *9 3

您可以看到,这条路径符合NOT PRIME NUMBERS的规则; 1> 8> 6> 9,1> 4> 6> 9,1> 4> 9> 91 + 8 + 6 + 9 =24。如您所见,1、8、6、9都不是主要数字,遍历这些将得出最大的和。根据3中的分配,您实现了以下输入的最大和是多少?这意味着请将此输入(作为文件或常量直接在代码内部)用于您的实现,并使用它来解决。

                             215
                           193 124
                         117 237 442
                       218 935 347 235
                     320 804 522 417 345
                   229 601 723 835 133 124
                 248 202 277 433 207 263 257
               359 464 504 528 516 716 871 182
             461 441 426 656 863 560 380 171 923
           381 348 573 533 447 632 387 176 975 449
         223 711 445 645 245 543 931 532 937 541 444
       330 131 333 928 377 733 017 778 839 168 197 197
    131 171 522 137 217 224 291 413 528 520 227 229 928
  223 626 034 683 839 053 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace final_assignment
{
class Program
{


    static void Main(string[] args)
    {

        Console.WriteLine("the maxmium sum is : {0}", func(0, 0));
        Console.ReadLine();
    }

    //function to read the string that contain the numbers
    static public string read()
    {
        const string x = @"     215
                               193 124
                              117 237 442
                            218 935 347 235
                          320 804 522 417 345
                        229 601 723 835 133 124
                      248 202 277 433 207 263 257
                    359 464 504 528 516 716 871 182
                  461 441 426 656 863 560 380 171 923
                 381 348 573 533 447 632 387 176 975 449
               223 711 445 645 245 543 931 532 937 541 444
             330 131 333 928 377 733 017 778 839 168 197 197
            131 171 522 137 217 224 291 413 528 520 227 229 928
          223 626 034 683 839 053 627 310 713 999 629 817 410 121
        924 622 911 233 325 139 721 218 253 223 107 233 230 124 233";

        return x;
    }

    //function to convert the string text into list of list<int>
    static public List<List<int>> ConvertSArrngToListOfIntList(string str)
    {
        //calculate the number of rows inside the string
        int Rows = 1;
        foreach (var item in str)
        {
            if (item == '\n') Rows++;
        }
        //create an array by line characters
        string[] arr = str.Split('\n');
        List<List<int>> b = new List<List<int>>();
        for (int i = 0; i < Rows; i++)
        {

            MatchCollection mc = Regex.Matches(arr[i], @"\d{3}");
            List<int> aa = new List<int>();
            foreach (Match item in mc)
            {
                int y = int.Parse(item.Value);
                aa.Add(y);
            }

            b.Insert(i, aa);
        }
        //complete every row to 15 values by adding zero
        for (int i = 0; i < Rows; i++)
        {
            if (b[i].Count <= Rows)
            {
                for (int j = i + 1; j < Rows; j++)
                {
                    b[i].Add(0);
                }

            }

        }

        return b;
    }
    //function to check if the number is prime or not and return boolean value
    static public bool PrimeChecker(int num)
    {
        if (num == 0 || num == 1)
        {
            return false;
        }
        else
        {
            for (int a = 2; a <= (int)Math.Floor(Math.Sqrt(num)); a++)
            {
                if (num % a == 0)
                {

                    return false;
                }

            }
            return true;
        }
    }
    private static int func(int x, int y)
    {
        List<List<int>> arrlist = ConvertSArrngToListOfIntList(read());
        if (x >= arrlist.Count)
            return 0;
        int self = arrlist[x][y];
        if (PrimeChecker(self))
            return 0;
        else

            return self + Math.Max(func(x + 1, y), func(x + 1, y + 1));
    }



}
 }
recursion path max primes
1个回答
0
投票

我还发现了使用递归算法的8219。您确定答案是错误的吗?

© www.soinside.com 2019 - 2024. All rights reserved.