7-3 最小回文素数
本帖最后由 学鸣 于 2022-3-19 17:14 编辑求大佬们帮个忙
大一刚学,自己想了半天,输入格式不会搞,请各位解一下题,能完整解答更好
#include <stdio.h>
int is_prime(int x)
{
for (int i = 2; i < x / 2; i++)
{
if (x % i == 0)
{
return 0;
}
}
return 1;
}
int is_palindromes(int x)
{
int m = 0, s = 0, n;
s = x;
while (s != 0)
{
n = s % 10;
m = m * 10 + n;
s = s / 10;
}
return x == m;
}
int main()
{
int T;
scanf("%d", &T);
int array;
int result;
for (int i = 0; i < T; i++)
{
scanf("%d", &array);
if (array > 65535)
{
printf("ERROR");
return 0;
}
int j = 0;
while (!is_prime(array + j) || !is_palindromes(array + j))
{
j++;
}
result = array + j;
}
for (int i = 0; i < T; i++)
{
printf("%d\n", result);
}
return 0;
} ckblt 发表于 2022-3-19 17:32
谢谢 本帖最后由 jhq999 于 2022-3-19 22:15 编辑
int outmin(int innum)
{
innum++;
int len=0,num=innum,i=0;
while (num)len++,num/=10;
int *fj=new int;
num=innum;
for (i=0;i<len;i++)
{
fj=num%10;
num/=10;
}
a=len/2+len%2;
for (i = 0; i < len/2; i++)
{
if(fj<fj)
{
fj++;
break;
}
}
for (i = 0; i < len/2; i++)fj=fj;
for (i = 0,num=0; i < len; i++)num=num*10+fj;
delete[] fj;
return num;
}
int main()
{
int n=0,*innum=NULL,i=0;
scanf("%d",&n);
innum=new int;
for (i = 0; i < n; i++)
{
scanf("%d",innum+i);
innum=outmin(innum);
}
for (i = 0; i < n; i++)
{
printf("%d\n",innum);
}
delete[] innum;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int outmin(int innum)
{
innum++;
int len=0,num=innum,a=0,b=0,c=0,i=0;
while (num)len++,num/=10;
num=innum;
b=1;
for (i=0;i<len/2;i++)
{
num/=10;
b*=10;
}
c=b;
b=innum-num*b;
innum=len%2?num/10:num;
a=0;
for (i=0;i<len/2;i++)
{
a=a*10+innum%10;
innum/=10;
}
if (b>a)
{
num++;
}
num=num*c+a;
return num;
}
int main()
{
int n=0,*innum=NULL,i=0;
scanf("%d",&n);
innum=new int;
for (i = 0; i < n; i++)
{
scanf("%d",innum+i);
innum=outmin(innum);
}
for (i = 0; i < n; i++)
{
printf("%d\n",innum);
}
delete[] innum;
return 0;
}
页:
[1]