溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用C語言求素數(shù)大于1只能被1跟本身除的數(shù)

發(fā)布時間:2021-12-08 14:27:55 來源:億速云 閱讀:163 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“怎么用C語言求素數(shù)大于1只能被1跟本身除的數(shù) ”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用C語言求素數(shù)大于1只能被1跟本身除的數(shù) ”吧!

#if 1
#include <iostream>
using namespace std;

void move(int n,char A, char B, char C)
{
    if(1==n)
    {
        cout<<A<<"-->"<<C<<endl;
    }
    else
    {
        move(n-1, A, C, B);
        cout<<A<<"-->"<<C<<endl;
        move(n-1, B, A, C);
    }
}

int main()
{
    cout <<"please input the  hanoi number:";
    int num;
    cin>>num;
    char A='a';
    char B='b';
    char C='c';
    move(num, A, B, C);
    return 0;
}
#else
#include <iostream>
using namespace std;
int fun(int n)
{
    if(n==1)
        return 1;
    if(n==0)
        return 0;
    return fun(n-1)+fun(n-2);
}

int CalAge(int n)
{
    if(1==n)
    {
      return 10;
    }
    return CalAge(n-1)+2;
}
int main()
{
    int i;
    int a[12];
    a[0]=0;
    a[1]=1;
    cout<<a[0]<<endl;
    cout<<a[1]<<endl;
    for(i=2; i<=12;i++)
    {
        a[i]=a[i-1]+a[i-2];
        cout<<a[i]<<endl;
    }
    cout<<"aaa"<<endl;
    cout<<fun(12)<<endl;
    cout<<" age 5"<<endl;
     cout<<CalAge(5)<<endl;
    return 0;
}
#endif

求素數(shù) 大于1 只能被1跟本身除的數(shù)      

#include <iostream>
#include <cmath>
using namespace std;
int fun(int temp)
{
    for(int j=2; j<temp; j++)
   // for(int j=2; j<=sqrt(temp); j++)  
    {
        if(temp%j == 0)
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int n;
    cout<<"please intput n n must > 1:";
    cin>>n;
    int out=0;
    for(int i=2;i<=n;i++)
    {
        if(1==fun(i))
        {
            out++;
            if(out>10)
            {
               out=1;
               cout<<endl;
            }
            cout<<i<<" ";
        }
    }
    cout<<endl;
    return 0;
}

題目二:一只青蛙一次可以跳上一級臺階,也可以跳上2級臺階,求該青蛙跳上n級臺階的共有多少種跳法。

思路:當(dāng)只有一級臺階的時候,青蛙的跳法也只有一種。當(dāng)有兩級臺階的時候,青蛙的跳法有兩種(一是:一下跳兩級臺階,二是:一級一級的跳)。當(dāng)有n級臺階的時候,青蛙在第一次起跳的時候只跳了一級臺階,則還剩下n-1級臺階的跳法,如果在第一次起跳的時候跳了兩級臺階,則還剩下n-2級臺階的跳法。整個題目正好是一個斐波拉契數(shù)列。公式如下:

怎么用C語言求素數(shù)大于1只能被1跟本身除的數(shù)

int Frog(int n)
{
    if(1==n || 2==n)
    {
      return n;
    }
    return Frog(n-1)+Frog(n-2);
}

八皇后問題 回溯 

#include <iostream>
using namespace std;
int ChessBoard[8][8];    /*棋盤*/
void InitsChessBorad()  // 初始化
{
    for(int row = 0;row<8;row++)
    {
        for(int column = 0; column<8; column++)
        {
            ChessBoard[row][column] = 0;
        }
    }
}
void PrintChessBoard() //打印棋盤
{
    static int Num = 0;
    Num = Num + 1;
    cout<<"the "<<Num <<" is:"<<endl;
    for(int row = 0; row<8;row++)
    {
        for(int column =  0; column <8; column ++)
        {
            if(ChessBoard[row][column])
            {
                cout<<"Q";
            }
            else
            {
                cout<<"+";
            }
        }
        cout<<endl;
    }
    cout<<endl;
}
bool Conflicts(int row, int column) // 判斷沖突
{
    for(int i=1; i<8; i++)
    {
        if(row-i>=0 && ChessBoard[row-i][column])
        {
            return true;
        }
        if(column-i>=0 && ChessBoard[row][column-i])
        {
            return true;
        }
        if(column-i>=0 && row-i>=0 && ChessBoard[row-i][column-i])
        {
            return true;
        }
        if(row-i>=0 && column+i<8 && ChessBoard[row-i][column+i])
        {
            return true;
        }
    }
    return false;
}
/* place_queen
** 放置皇后
** 第一行不需要檢查,因為只有一個皇后?;屎蠓胖玫奈恢迷O(shè)置為true。
** 如果某M行的所有列放置皇后都存在互相攻擊,那么需要返回到M-1行中放置皇后的位置,
** 將M-1行的皇后位置設(shè)置為false,尋找這行的下一個可以放置皇后的位置,如果存在再檢查第M行可以放置皇后的位置。
** 如果不存在,則返回到第M-2行。
** 如果8個皇后成功放置完畢,則打印棋盤.
*/
void QueenPlace(int row) // 棋盤放置
{
    for(int column = 0;column<8;column++)
    {
        ChessBoard[row][column] = 1;
        if(row ==0 || !Conflicts(row, column))
        {
            if(row <7)
            {
                QueenPlace(row+1);
            }
            else
            {
                PrintChessBoard();
            }
        }
        ChessBoard[row][column] = 0;
    }
}
int main()
{
    InitsChessBorad();
    QueenPlace(0);
    return 0;
}

到此,相信大家對“怎么用C語言求素數(shù)大于1只能被1跟本身除的數(shù) ”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI