c - PALIN 的 spoj 上的 c 代码中出现运行时错误 SIGSEGV

标签 c palindrome

如果从左到右和从右到左读取的正整数在十进制系统中的表示相同,则该正整数称为回文。对于给定的不超过1000000位的正整数K,将大于K的最小回文数的值写入输出。显示的数字始终不带前导零。 首先我尝试保存所有回文,然后检查是否打印数字。

#include<stdio.h>
int array[2000],index=0;
long long n;

void savepalindrome()
{
    long long lim=1000000;
    long long i=1;

    for(i=1;i<lim;i++)
    {
        if(checkpalindrome(i)==1) {
            array[index]=i; index++;
        }
    }
}

int  checkpalindrome(long long i) {
    long long reverse=0, rem,temp;
    temp=i;
    while(temp!=0)
    {
        rem=temp%10;
        reverse=reverse*10+rem;
        temp/=10;
    }
    if(reverse==i) return 1;
    else return 0;
}

int main() {
    int t;
    scanf("%d",&t);
    savepalindrome();
    while(t--) {
        scanf("%d",&n);
        index=0;
        while(array[index]<=n) {
            index++;
        }

        if(index<=1998) printf("%d\n",array[index]);
    }
}

最佳答案

尽管如果 long long int 无法存储 1000000 位数字,它将在 long long int 的范围内溢出。我认为您的索引超出了数组范围。这似乎是 SIGSEGV 的唯一原因。仅当进程访问无效的内存位置时,才会向该进程发送 SIGSEGV。

关于c - PALIN 的 spoj 上的 c 代码中出现运行时错误 SIGSEGV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26501716/

相关文章:

c - 如何在 GTK+ 中启动和停止 GIF 动画 GtkImage

c - 静态常量函数指针 C

c - 在文件中查找单词,检查它们是否是回文

java - 欧拉项目 4 Java

java - 使用java stringbuilder的回文

java - 移动确定字符串是否为回文的逻辑

c - 屏幕保护程序依赖于文件 C

c++ - 制作 Vista/7 User Frame 控件? (WinAPI)

c - 随机选择名字时出现奇怪的字符

python - 改进 Python 回文代码