gpt4 book ai didi

c - 欧拉项目'8 : solution works for example but can't find answer

转载 作者:行者123 更新时间:2023-11-30 18:56:13 25 4
gpt4 key购买 nike

我正在学习 C 语言,并试图提高解决欧拉计划练习的技能。我被 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 来更改测试的相邻数字数量。如果您使用 4 来运行它(如问题示例中所示),则乘积为 5832(这是正确的乘积),但如果您使用 13 来运行它,则乘积不正确。

谢谢。

最佳答案

第 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" 的内容。

关于c - 欧拉项目'8 : solution works for example but can't find answer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com