国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

c語言判斷是否素數程序代碼

2020-01-26 15:40:56
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請輸入一個大于1的自然數:/n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素數/n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素數/n",x);
    printf("是否繼續(y/n):/n");
    scanf("%c", &ch);
    }while ('y'== ch || 'Y' == ch);
    return 0;
}

但是,運行的時候,出現了一個問題.

本來寫do...while是為了省事,重復判斷.但是,卻愣是不起效果.

后來,修改了一下:

復制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請輸入一個大于1的自然數:/n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素數/n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素數/n",x);
    printf("是否繼續(y/n):/n");
    scanf(" %c", &ch);//在這里加一個空格
    }while ('y'==ch || 'Y' == ch);
    return 0;
}

這樣,問題就神奇地解決了.

再后來,再這么改:

復制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請輸入一個大于1的自然數:/n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素數/n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素數/n",x);
    printf("是否繼續(y/n):/n");
    scanf("/n%c", &ch);//在這里加一個換行符 '/n'
    }while ('y'==ch || 'Y' == ch);
    return 0;
}

也沒問題.

所以,總結一下,問題出現在我們在輸入數字按下回車的似乎,'/n'還保存在輸入流中

因此,看第一個代碼:

復制代碼 代碼如下:

int main(void)
{
    int x;
    char ch;
    do{
    printf("請輸入一個大于1的自然數:/n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素數/n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素數/n",x);
    printf("是否繼續(y/n):/n");
    scanf("%c", &ch);  //因為 '/n'還在輸入流中  所以 就變成了  ch = '/n';
    }while ('y'==ch || 'Y' == ch);  // 'y' != '/n'  && 'Y' != '/n';
    return 0;   //因此程序就return了
}


另外,也是可以使用fflush(stdin)清空輸入緩存區.

復制代碼 代碼如下:

#include <stdio.h>

bool isPrimeNum(int x)
{
    if (x == 1)
        return false;
    else if (x <= 0)
        return false;
    else if (x == 2)
        return true;
    else
    {
        for (int i = 2; i < x; i++)
        {
            if (x%i == 0)
                return false;
        }
        return true;

    }
}

int main(void)
{
    int x;
    char ch;
    do{
    printf("請輸入一個大于1的自然數:/n");
    scanf("%d", &x);
    if (isPrimeNum(x) == false)
        printf("%d不是素數/n",x);
    else if (isPrimeNum(x) == true)
        printf("%d是素數/n",x);
    printf("是否繼續(y/n):/n");
    fflush(stdin);  //清空輸入緩存區
    scanf("%c", &ch);
    }while ('y'==ch || 'Y' == ch);
    return 0;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 乐业县| 富阳市| 绥化市| 呼图壁县| 永宁县| 湘乡市| 葫芦岛市| 黄石市| 正宁县| 田阳县| 汉寿县| 天峻县| 周至县| 武川县| 上林县| 英山县| 古田县| 安平县| 台江县| 华安县| 房山区| 伊川县| 白河县| 漳平市| 九寨沟县| 黄浦区| 兴宁市| 琼中| 桂东县| 富宁县| 古浪县| 建湖县| 北流市| 长乐市| 太湖县| 灌南县| 清远市| 阿瓦提县| 灌阳县| 南郑县| 亳州市|