欧拉计划' 8:解决方案起作用,但无法找到答案

时间:2014-07-27 19:30:12

标签: c

我正在学习C并试图提高我解决Project Euler练习的技能。我被problem 8

困住了

这是我的代码:

  #include <stdio.h>
  #include <string.h>
  #include <stdin
  #define ADJ_GRAB 4

  uint64_t max_prod;

  //Takes a char array and returns converted int array
  int *arr_atoi(const char nos[]){

   static int c_ints[ADJ_GRAB];
   int nos_len = strlen(nos);

   int z;
   for (z = 0; z < nos_len; z++)
    c_ints[z] = nos[z] - '0';

   return c_ints;
  }

  //Returns the sum of the adjacent digits
  void check_sum(const char nos[]){

      extern uint64_t max_prod;
      uint64_t c_prod = 1;
      int *tc_ints = arr_atoi(nos);

      int z;
      for (z = 0; z < ADJ_GRAB; z++)
          c_prod *= *(tc_ints + z);

      max_prod = c_prod > max_prod ? c_prod : max_prod;
  }

  int main(void)
  {
      //define the number list, its length and a buffer to hold adjacent digits.
    char *no_list =
       "73167176531330624919225119674426574742355349194934"
       "96983520312774506326239578318016984801869478851843"
       "85861560789112949495459501737958331952853208805511"
       "12540698747158523863050715693290963295227443043557"
       "66896648950445244523161731856403098711121722383113"
       "62229893423380308135336276614282806444486645238749"
       "30358907296290491560440772390713810515859307960866"
       "70172427121883998797908792274921901699720888093776"
       "65727333001053367881220235421809751254540594752243"
       "52584907711670556013604839586446706324415722155397"
       "53697817977846174064955149290862569321978468622482"
       "83972241375657056057490261407972968652414535100474"
       "82166370484403199890008895243450658541227588666881"
       "16427171479924442928230863465674813919123162824586"
       "17866458359124566529476545682848912883142607690042"
       "24219022671055626321111109370544217506941658960408"
       "07198403850962455444362981230987879927244284909188"
       "84580156166097919133875499200524063689912560717606"
       "05886116467109405077541002256983155200055935729725"
       "71636269561882670428252483600823257530420752963450";
    int list_len = strlen(no_list);
    char tmp_buff[ADJ_GRAB+1];

    //iterate over the number list
    int z;
    for (z = 0; z < list_len; z++){
        //Take ADJ_GRAB adjacent numbers and check their product
        if ((z + ADJ_GRAB) < list_len){
            strncpy_s(tmp_buff, ADJ_GRAB + 1, no_list + z, ADJ_GRAB);
            check_sum(tmp_buff);
        }
      }

      printf("%u\n", max_prod);

      system("PAUSE");
      return 0;
  }

您可以通过修改ADJ_GRAB来更改测试的相邻位数。如果你用四个运行它,就像在问题的例子中那样,产品是5832(这是正确的产品),但如果用13运行它,产品就不正确。

谢谢。

1 个答案:

答案 0 :(得分:2)

第1部分:位数

您的解决方案遭遇整数溢出。您正在int上进行数学运算,这可能是32位数。

从刚刚扫描问题空间开始,我遇到了:

9*7*5*3*6*9*7*8*1*7*9*7*7 == 8,821,658,160 == 0x2_0DCF_D230

这是一个34位(无符号)数字。

第2部分:编译器应提供更好的警告

blue.cc:34:33: warning: format specifies type 'unsigned int' but the argument has type 'uint64_t' (aka 'unsigned long long') [-Wformat]
        printf("%u\n", max_prod);
                ~~     ^~~~~~~~
                %llu

你的printf格式字符串与参数不匹配。您可能想要"%llu"