gpt4 book ai didi

c - 由两个 3 位数字的乘积组成的最大回文数。使用 c 。我的代码有什么问题吗?

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

程序无法运行,没有输出,我不知道该怎么办,问题出在哪里。

我正在尝试找出由两个 3 位数乘积组成的最大回文数。

#include <stdio.h>

main() {
int i, k, j, x;
long int a[1000000], palindrome[1000000], great, sum = 0;
// for multiples of two 3 digit numbers
for (k = 0, i = 100; i < 1000; i++) {
for (j = 100; j < 1000; j++) {
a[k] = i * j; // multiples output
k++;
}
}

for (i = 0, x = 0; i < 1000000; i++) {
// for reverse considered as sum
for (; a[i] != 0;) {
sum = sum * 10 + a[i] % 10;
}
// for numbers which are palindromes
if (sum == a[i]) {
palindrome[x] = a[i];
x++;
break;
}
}
// comparison of palindrome number for which one is greatest
great = palindrome[0];
for (k = 0; k < 1000000; k++) {

if (great < palindrome[k]) {
great = palindrome[k];
}
}
printf("\ngreatest palindrome of 3 digit multiple is : ", great);
}

最佳答案

“不工作”是什么意思?

从我的角度来看,有两件事:

1) long int a[1000000],回文[1000000]

根据您的编译配置,您可能会在编译代码时遇到问题。数组可能太大,无法放入程序的堆栈地址空间。在 C 或 C++ 中,本地对象通常分配在堆栈上。不要在堆栈上本地分配它,而是使用其他地方。这可以通过使对象成为全局对象或将其分配到全局堆上来实现。

#include <stdio.h>

long int a[1000000], palindrome[1000000], great, sum = 0;

main() {
int i, k, j, x;

2) printf("\ng3位倍数的最大回文是:",太棒了);

我将更改它:

printf("\ng3位倍数的最大回文是%li:",太棒了);

问候。

关于c - 由两个 3 位数字的乘积组成的最大回文数。使用 c 。我的代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317491/

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