current position:Home>There was a problem calling the custom function

There was a problem calling the custom function

2022-02-03 01:04:32 CSDN Q & A

The phenomenon and background of the problem

In writing, judge the number of palindrome prime numbers in an interval . However, after calling the function, it is found that no single test of the function can be carried out. Once judged, it will not work . Just want to join printf Take a look at the output value of the function alone . It turns out that the final result has changed

#include<stdio.h>int huiwen(int n){    if(n==0)    {    return 1;}else{    int y=n,s;    while(n!=0)   {    s=s*10+n%10;    n=n/10; }if(s==y)return 1;elsereturn 0;}}   int prime(int n){    int s;    if(n<=1)    return 0;    for(s=2;s*s<=n;s++)    {        if(n%s==0)        break;    }        if(s*s<=n)        return 0;        return 1;}        int main() {        int l,r,i,p,q;        while(scanf("%d%d",&l,&r)!=EOF)        {        for( ;l<=r;l++)        {            p=huiwen(l);            q=prime(l);            if(p==1&&q==1)            {                i++;            }        }        printf("%d\n",i);    }        return 0;    }
Operation results and error reporting contents

img


img

img

My solution ideas and tried methods
What I want to achieve

Do not join printf The output is OK .




Refer to the answer 1:

See notes for modifications , For reference :

#include<stdio.h>int huiwen(int n){    if (n == 0)    {        return 1;    }    else {        int y = n, s = 0;// modify         while (n != 0)        {            s = s * 10 + n % 10;            n = n / 10;        }        if (s == y)            return 1;        else            return 0;    }}int prime(int n){    int s;    if (n <= 1)        return 0;    for (s = 2; s * s <= n; s++)    {        if (n % s == 0)            return 0;//break;  modify     }          //if (s * s <= n)  modify           // return 0;    return 1;}int main(){    int l, r, i, p, q;    while (scanf("%d%d", &l, &r) != EOF)    {        for (i=0; l <= r; l++) // modify         {            p = huiwen(l);            q = prime(l);            if (p == 1 && q == 1)            {                i++;            }        }        printf("%d\n", i);    }    return 0;}



Refer to the answer 2:



Refer to the answer 3:

The first 9 Yes s It should be initialized to 0
The first 29 Line can be directly return 0, Unwanted break
32-34 Direct change to return 1;
The first 42 That's ok , increase i=0; For each group of test data ,i It should be initialized to 0, Otherwise, it's cumulative




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030104300692.html

Random recommended