【问题标题】:Project Euler #8, I don't understand where I'm going wrongProject Euler #8,我不明白我哪里出错了
【发布时间】:2014-05-23 08:38:17
【问题描述】:

我正在处理euler problem number eight 项目,我在该项目中获得了这么多可笑的数字:

7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

并且应该“在 1000 位数字中找到具有最大乘积的 13 个相邻数字。” EG 前四个相邻数字的乘积是 7 * 3 * 1 * 6。 我的代码如下:

int main()
{
    string num = /* ridiculously large number omitted */;
    int greatestProduct = 0;
    int product;
    for (int i=0; i < num.length() -12; i++)
    {
        product = ((int) num[i] - 48);
        for (int j=i+1; j<i+13; j++)
        {
            product = product * ((int) num[j] - 48);
            if (greatestProduct <= product)
            {
                greatestProduct = product;
            }
        }
    }
    cout << greatestProduct << endl;
}

我不断收到 2091059712,因为项目 euler 告诉我的答案是错误的,我怀疑它太大了。任何帮助将不胜感激。

编辑:更改为 unsigned long int 并且有效。谢谢大家!

【问题讨论】:

  • 您确定您的num 字符串没有任何非数字符号吗?例如,您能否将程序的可编译和可运行版本放到 ideone.com 上?
  • 您是否考虑过该值可能大于有符号整数?我会使用 unsigned long long。连续思考 13 个 9。
  • 任何包含 0 位的解决方案都可能不是是最好的解决方案。
  • 它可能会帮助您知道 23514624000(答案)比大多数平台 INT_MAX 大得多(在 32 位时,它只有 2147483647)。也许使用一个更大的整数类型,也许在你使用它的时候让它无符号。
  • 23514624000 也是我得到的。需要 34 位。

标签: c++


【解决方案1】:

事实上,您的解决方案太小而不是太大。答案是 cmets 中指出的,存在整数溢出,线索在于您的解决方案接近有符号 int 的最大可能值:2147483647。您需要使用不同的类型来存储产品。

请注意,下面的答案仍然是“正确的”,因为您的代码确实做错了,但这不是导致错误值的原因。如果您希望那里的人告诉您在您的方法和编码风格方面可以改进的地方,请尝试将您的(工作)代码发送到 http://codereview.stackexchange.com

上一个答案

您正在内部循环内部而不是外部检查新的最佳产品。这意味着您的最大值包括小于或等于 ton 13 位的所有字符串,而不仅仅是 13 位。

如果您找到的字符串少于 13 位且乘积很大,但两端均为 0,这可能会有所不同。您不应该将其视为最大的,但您的代码确实如此。 (我还没有检查这是否真的发生。)

for (int i=0; i < num.length() -12; i++)
{
    product = ((int) num[i] - 48);
    for (int j=i+1; j<i+13; j++)
    {
        product = product * ((int) num[j] - 48);
    }
    if (greatestProduct <= product)
    {
        greatestProduct = product;
    }
}

【讨论】:

  • 确实如此。一个简单的添加将是for (int j=i+1; product!= 0 &amp;&amp; j&lt;i+13; j++)
  • 虽然理论上2147483647 可能是unsigned int 的最大值,但我认为您的意思是signed int。它只有 31 位。
【解决方案2】:

9^13 ≈ 2.54e12(最大可能值,需要 42 位才能准确表示),这不适合 signed int。您应该使用 int64。

【讨论】:

    【解决方案3】:

    如果您不想弄乱 BigNum 库,您可以只取数字的对数(拒绝 0)并将它们相加。这相当于相同的比较。

    【讨论】:

      【解决方案4】:

      一种没有内部循环的更快方法,但仅适用于输入中没有0 的情况:

      long long greatest(string num)
      {
          if (num.length() < 13)
              return 0;
      
          // Find a product of first 13 numbers.
          long long product = 1;
          unsigned int i;
          for (i=0; i<13; ++i) {
              product *= (num[i]-'0');
          }
          long long greatest_product = product;
          // move through the large number
          for (i=0; i+13<num.length(); ++i) {
              product = product/(num[i]-'0')*(num[i+13]-'0');
              if (greatest_product < product)
                  greatest_product = product;
          }
          return greatest_product;
      }
      

      为了使用包含0 的输入,请将输入字符串拆分为子字符串:

      int main()
      {
          string num = /* input value*/;
      
          long long greatest_product = 0;
          size_t start = -1;
          // Iterate over substrings without zero
          do {
              ++start;
              size_t end = num.find('0', start);
              long long product = greatest(num.substr(start, end-start));
              if (greatest_product < product)
                  greatest_product = product;
              start = end;
          } while (start != string::npos);
      
          cout << greatest_product << endl;
      }
      

      【讨论】:

      • 这里有除零问题。
      • 这很遗憾,因为如果得到纠正,这将是这里的最佳答案。
      • 如果这是您编辑的答案,请标记已编辑的部分。这比 OP 的方法快至少 4 倍。我不明白在 T.C. 的评论之前或之后的答案中是否包含“主要”功能。现在看起来很完美。
      【解决方案5】:

      我的函数式编程解决方案

      def product(number):
          product_result = 1
          for n in number:
              product_result *= int(n)
          return product_result
      
      length = 13
      indices = range(0, len(number) - 13 + 1)
      values = indices
      values = map(lambda index: {"index": index, "number": number}, values)
      values = map(lambda value: value["number"][value["index"]:value["index"] + length], values)
      values = map(product, values)
      values = max(values)
      print values
      

      【讨论】:

        【解决方案6】:
        public static void main(String[] args) {
        
            String val = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
        
            int Sum = 0;
            String adjacent  = null;
            for (int i = 0; i < val.length()-13; i++) {
        
                int total = 1;
                for (int j = 0; j < 13; j++) {
                    total = total * Character.getNumericValue(val.charAt(i+j));
                }
        
                if(total > Sum){
                    Sum = total;
                    adjacent  = val.substring(i, i+13);
                }
        
            }
        
            System.out.println("Sum = " + Sum);
            System.out.println("Adjsc = " + adjacent );
        
        }
        

        【讨论】:

        • 这个答案将受益于它如何解决提问者问题的解释,以及对所咨询的任何来源的参考。
        【解决方案7】:

        我遇到了同样的问题。 int product 和 int bestproduct 具有它可以存储的最大值,因为它们是“int”类型。它们最多只能存储 2147483647 的值。

        使用“long long”类型而不是“int”。

        【讨论】:

          【解决方案8】:
          const thousandDigit = 
          `73167176531330624919225119674426574742355349194934
          96983520312774506326239578318016984801869478851843
          85861560789112949495459501737958331952853208805511
          12540698747158523863050715693290963295227443043557
          66896648950445244523161731856403098711121722383113
          62229893423380308135336276614282806444486645238749
          30358907296290491560440772390713810515859307960866
          70172427121883998797908792274921901699720888093776
          65727333001053367881220235421809751254540594752243
          52584907711670556013604839586446706324415722155397
          53697817977846174064955149290862569321978468622482
          83972241375657056057490261407972968652414535100474
          82166370484403199890008895243450658541227588666881
          16427171479924442928230863465674813919123162824586
          17866458359124566529476545682848912883142607690042
          24219022671055626321111109370544217506941658960408
          07198403850962455444362981230987879927244284909188
          84580156166097919133875499200524063689912560717606
          05886116467109405077541002256983155200055935729725
          71636269561882670428252483600823257530420752963450`;
          
          const productsOfAdjacentNths = num =>
            [...thousandDigit].reduce((acc, _, idx) => {
              const nthDigitAdjX = [...thousandDigit.substr(idx, num)].reduce(
                (inAcc, inCur) => inCur * inAcc,
                1
              );
          
              return acc.concat(nthDigitAdjX);
            }, []);
          
          console.log(Math.max(...productsOfAdjacentNths(13))); //5377010688
          

          【讨论】:

          • 这应该是 C++ 吗?
          【解决方案9】:

          这是我的 O(n) 方法

          function findLargest(digits, n){
            let largest = 0;
            let j = 0;
            let res = 1;
            for(let i = 0; i< digits.length; i ++){
              res = res * parseInt(digits[i]);
              if(res == 0){
                res = 1;
                j = 0;
                continue;
              }
              if(j === n-1){
                if(res > largest)
                  largest = res;
                res = res/parseInt(digits[i - j]);
                j = j - 1;
              }
              j = j + 1;
            }
            return largest;
          }
            let val = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
          
          findLargest(val, 13);

          【讨论】:

            【解决方案10】:

            我用这种方法解决了问题

            1.运行两个循环第一个外循环,迭代字符串中的数字。

            2.内循环之后到达接下来的 13 个数字并将其乘积存储在一个变量中

            3.作为内部循环结束,我们只使用最大值。(我们需要的):>

            看看我的 C++ 代码

            #include <bits/stdc++.h>
            using namespace std;
            int main()
            {
                string s="//thestring is there";
                long long unsigned mainans=1,ans=1;           //initialize the value of mainans and ans to 1 (not 0 as product also become 0)
                for(int i=0;i<s.length()-13;i++)             //run the loop from 0 upto 13 minus string length
                {
                    ans=1;
                    for(int j=i;j<i+13;j++)                //now from that particular digit we check 13 digits afterwards it
                    {
                        int m=s[j]-'0';                   //convert the number from string to integer
                        ans*=m;                           //taking product in every interation for 13 digits 
                    }
                    mainans=max(mainans,ans);            //now taking only max value because that what we need
                }
                cout<<mainans;
                return 0;
            }
            

            【讨论】:

              【解决方案11】:

              虽然上述解决方案很好,但这里有一个完美运行的python实现:

              def main():
                    num=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
                    prod,maxprod=1,0
                    numtostr=str(num)
                    length=len(numtostr)
                    for i in range(1,length-13+1):
                        prod=1
                        for z in range(i,i+13):
                            prod=prod*int(numtostr[z-1:z])
                            if prod>maxprod:
                                maxprod=prod
                    print "Largest 13 digit product is %d" %(maxprod)
              
               if __name__ == '__main__':
                     main()
              

              【讨论】:

                【解决方案12】:
                static long q8(){
                        long max_product = 1;
                        long product = 1;
                        String n = "LONG_INPUT";
                        for(int i=0;i<13;i++){
                            max_product *= Integer.parseInt(n.charAt(i)+"");
                        }
                        product = max_product;
                        for(int i=1;i<n.length()-13;i++){
                            int denom = Integer.parseInt(n.charAt(i-1)+"");
                            if(denom!=0)
                                product = product/denom * Integer.parseInt(n.charAt(i+12)+"");
                            else
                                product = product(i,n);
                            max_product = (max_product>product)?max_product:product;
                        }
                
                        return max_product;
                    }
                    static long product(int index,String n){
                        long pro = 1;
                        for(int i=index;i<index+13;i++){
                            pro *= Integer.parseInt(n.charAt(i)+"");
                        }
                        return pro;
                    }
                

                【讨论】:

                  【解决方案13】:

                  试试这个:

                      {
                          DateTime BeganAt = new DateTime();
                  
                          BeganAt = DateTime.Now;
                  
                          Int64 result = 0;
                  
                          string testNumber = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
                  
                          StringBuilder StringBuilder = new StringBuilder(13);
                  
                          Int64 maxNumber = 0;
                  
                          try
                          {
                              char[] numbers = testNumber.ToCharArray();
                  
                              int tempCounter = 13;
                  
                              for (int i = 0;i < numbers.Length;i++)
                              {
                                  if(i < tempCounter)
                                  {
                                      StringBuilder.Append(numbers[i]);
                                  }
                                  else if (i == tempCounter)
                                  {
                                      if (maxNumber < Convert.ToInt64(StringBuilder.ToString()))
                                      {
                                          maxNumber = Convert.ToInt64(StringBuilder.ToString());
                                      }
                  
                                      StringBuilder.Clear();
                                      StringBuilder.Append(numbers[i]);
                                      tempCounter = tempCounter + n;
                                  }
                              }
                  
                              result = maxNumber;
                          }
                          catch
                          {
                              throw;
                          }
                  
                          DateTime EndedAt = new DateTime();
                  
                          EndedAt = DateTime.Now;
                  
                          TimeSpan TimeItTook = (EndedAt - BeganAt);
                  
                          return Convert.ToString(result) + " - Time took to execute: " + Convert.ToString(TimeItTook.TotalMilliseconds);
                      }
                  

                  【讨论】:

                    【解决方案14】:
                    long long int sum = 1,high=0;
                    
                    for (int i = 0; i < 988; i++)
                    {
                        sum = sum*(arr[i] - 48);
                        for (int j = i + 1; j < i+13; j++)
                        {
                            sum = sum*(arr[j]-48);
                        }
                        if (sum >= high)
                        {
                            high = sum;
                        }
                        sum = 1;
                    }   
                    

                    【讨论】:

                    • 你能在你的回答中多解释一下你的代码吗?
                    【解决方案15】:

                    如果有人在 2018 年仍然在看这个问题,我对上述问题的解决方案。虽然这里有很多解决这个问题的方法,但我的解决方案预先检查了其中包含 0 的单个 13 位数字。因为总是乘以 0 0 所以我们可以去掉这个无用的计算

                    int j = 0, k = 12;
                    long long int mult = 1;
                    long long int max = 0;
                    char *digits = /*Big number*/
                    while(k < 1000){
                        for (int i = j; i <= k; ++i)
                        {
                            /* code */
                            long long int val = digits[i] -'0';
                            /* check if any number in series contains 0 */
                            if(val == 0){
                                break;
                            }
                            mult = mult * val;
                        }
                        printf("mult is %lld\n",mult );
                         /* check for max value */
                        if(max < mult){
                            max = mult;
                        }
                        printf("the new max is %lld\n", max);
                        j += 1;
                        k += 1;
                        mult = 1;
                        printf("%d iteration finished\n", k);
                    }
                    printf("%lld\n", max);
                    

                    【讨论】:

                      【解决方案16】:

                      你应该在'int'的地方使用'int64_t'

                      【讨论】:

                      • 这应该在评论中
                      【解决方案17】:

                      我已经使用 python 're' 模块解决了这个问题

                      找出所有可能的不带零的 13 位数字(总共 988 个带零和 283 个不带零)然后找到每个数字的乘积并检查最大值

                      这里我使用了前瞻正则表达式

                      注意:字符串不能包含任何换行符

                      s = '731671765...'

                      import re
                      
                      
                      def product_13(d):
                          pro = 1
                          while d:
                              pro *= d % 10
                              d //= 10
                          return pro
                      
                      
                      pat = re.compile(r'(?=([1-9]{13}))')
                      
                      all = map(int, re.findall(pat, s))
                      pro = -1
                      for i in all:
                          v = product_13(i)
                          if pro < v:
                              pro = v
                      print(pro)
                      

                      【讨论】:

                      • 找到所有可能的 13 位不带零的数字(在这种情况下总共是 283),然后找到每个数字的乘积并检查最大值
                      • 关于 Stack Overflow 的高质量答案将包含答案本身,说明代码的作用以及它如何解决问题。詹妮弗试图说服你edit答案并添加这个解释。将其添加为评论并没有任何好处,因为我们的大多数用户都不阅读 cmets。如果您还没有,请使用tour 并阅读How to Answer
                      【解决方案18】:
                      **This is JavaScript solution.**
                      
                      
                      greatestProductOfAdjacentDigit = (givenNumber, noOfDigit) => {
                      stringNumberToNumbers = givenNumber => givenNumber.split('').map(each => parseInt(each, 10))
                      
                                  let numbers = mathematicalProblems.stringNumberToNumbers(givenNumber);
                                  return numbers.map(function (each, ind, arr) {
                                      return mathematicalProblems.productOfAll(arr.slice(ind, ind + noOfDigit));
                                  }).reduce(function (accumulator, currentValue) {
                                      return accumulator > currentValue ? accumulator : currentValue;
                                  });
                              };
                      

                      测试

                      const givenNumber = '73167176531330624919225119674426574742355349' +
                                      '194934969835203127745063262395783180169848018694788518438586' +
                                      '156078911294949545950173795833195285320880551112540698747158' +
                                      '523863050715693290963295227443043557668966489504452445231617' +
                                      '318564030987111217223831136222989342338030813533627661428280' +
                                      '644448664523874930358907296290491560440772390713810515859307' +
                                      '960866701724271218839987979087922749219016997208880937766572' +
                                      '733300105336788122023542180975125454059475224352584907711670' +
                                      '556013604839586446706324415722155397536978179778461740649551' +
                                      '492908625693219784686224828397224137565705605749026140797296' +
                                      '865241453510047482166370484403199890008895243450658541227588' +
                                      '666881164271714799244429282308634656748139191231628245861786' +
                                      '645835912456652947654568284891288314260769004224219022671055' +
                                      '626321111109370544217506941658960408071984038509624554443629' +
                                      '812309878799272442849091888458015616609791913387549920052406' +
                                      '368991256071760605886116467109405077541002256983155200055935' +
                                      '72972571636269561882670428252483600823257530420752963450';
                      
                      console.log(greatestProductOfAdjacentDigit(givenNumber, 4)); //5832
                      console.log(greatestProductOfAdjacentDigit(givenNumber, 13)); //23514624000
                      

                      【讨论】:

                        【解决方案19】:

                        我的 2 美分 ... Javascript

                        Largest_product_in_series_13 = g => {
                                         return [...g.matchAll(/(?=([1-9]{13}))/g)]
                                                .reduce((a,c)=>(p=eval(c[1].split``.join`*`),p>a?p:a),0)
                        }
                        

                        首先匹配所有 13 位系列,然后减少到最大的产品。

                        console.log(Largest_product_in_series_13(`7316717653133062491922....
                        

                        > 23514624000 开始:14.292ms

                        【讨论】:

                          【解决方案20】:

                          我用红宝石解决了它。如果

                          x = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

                          然后使用下面的逻辑来解决问题

                          def print_lps(x)
                              p = 1
                              0.upto(x.length-13) do |i|
                                j = i
                                xy = 1
                                while j < i+13 do
                                  xy *= x[j].to_i 
                                  j += 1 
                                end
                                p = xy if xy > p
                              end
                              puts p
                          end
                          
                          print_lps(x) # 23514624000
                          

                          【讨论】:

                            【解决方案21】:

                            这将在 O(n) 时间内找到答案,并且处理为零。用 Dart 写的。

                            /**
                             * Main function
                             *
                             * Find the thirteen adjacent digits in the 1000-digit number that have the greatest product.
                             * What is the value of this product?
                             *
                             */
                            const String DIGITS = "73167176531330624919225119674426574742355349194934"
                                                  "96983520312774506326239578318016984801869478851843"
                                                  "85861560789112949495459501737958331952853208805511"
                                                  "12540698747158523863050715693290963295227443043557"
                                                  "66896648950445244523161731856403098711121722383113"
                                                  "62229893423380308135336276614282806444486645238749"
                                                  "30358907296290491560440772390713810515859307960866"
                                                  "70172427121883998797908792274921901699720888093776"
                                                  "65727333001053367881220235421809751254540594752243"
                                                  "52584907711670556013604839586446706324415722155397"
                                                  "53697817977846174064955149290862569321978468622482"
                                                  "83972241375657056057490261407972968652414535100474"
                                                  "82166370484403199890008895243450658541227588666881"
                                                  "16427171479924442928230863465674813919123162824586"
                                                  "17866458359124566529476545682848912883142607690042"
                                                  "24219022671055626321111109370544217506941658960408"
                                                  "07198403850962455444362981230987879927244284909188"
                                                  "84580156166097919133875499200524063689912560717606"
                                                  "05886116467109405077541002256983155200055935729725"
                                                  "71636269561882670428252483600823257530420752963450";
                            
                            main(List<String> args) {
                            
                              const int MAX_DIGITS = 13;
                              int len = DIGITS.length;
                            
                              int digits = 0;
                              int largest = 0;
                              int current = 0;
                            
                              int index = 0;
                              while (index < len) {
                            
                                int value = int.parse(DIGITS[index]);
                            
                                // if we get a zero then rebuild the MAX_DIGITS consecutive digits
                                if (value == 0) {
                                  current = 0;
                                  digits = 0;
                                } else {
                                  // Multiply consecutive digits up to target set
                                  if (digits < MAX_DIGITS) {
                                    if (current == 0) {
                                      current = value;
                                    } else
                                      current *= value;
                                    digits++;
                                  } else {
                                    int divisor = int.parse(DIGITS[index - MAX_DIGITS]);
                            
                                    if (current == 0) {
                                      current = value;
                                    } else {
                                      current = (current ~/ divisor);
                                      current *= value;
                                    }
                                  }
                            
                                  if (current > largest) {
                                    largest = current;
                                  }
                                }
                            
                                index++;
                              }
                            
                              print('Num  $largest');
                            }
                            

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 2016-08-02
                              • 1970-01-01
                              • 2022-08-02
                              • 1970-01-01
                              • 2023-04-01
                              • 1970-01-01
                              • 2011-03-14
                              相关资源
                              最近更新 更多