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

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

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

我试图找出由两个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);
}

c math palindrome
1个回答
0
投票

您对“不起作用”是什么意思?

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

1)long int a[1000000], palindrome[1000000]

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

#include <stdio.h>

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

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

2)printf("\ngreatest palindrome of 3 digit multiple is : ", great);

我将通过:更改它

printf("\ngreatest palindrome of 3 digit multiple is %li: ", great);

问候

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