如果从左到右和从右到左读取的正整数在十进制系统中的表示相同,则该正整数称为回文。对于给定的不超过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/