首页
/
每日頭條
/
職場
/
c語言算法如何提高
c語言算法如何提高
更新时间:2025-07-02 08:32:48

c語言算法如何提高?算法是一個程序和軟件的靈魂,作為一名優秀的程序員,隻有對一些基礎的算法有着全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手本文是近百個C語言算法系列的第二篇,包括了經典的Fibonacci數列、簡易計算器、回文檢查、質數檢查等算法也許他們能在你的畢業設計或者面試中派上用場,現在小編就來說說關于c語言算法如何提高?下面内容希望能幫助到你,我們來一起看看吧!

c語言算法如何提高(10大C語言基礎算法)1

c語言算法如何提高

算法是一個程序和軟件的靈魂,作為一名優秀的程序員,隻有對一些基礎的算法有着全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。本文是近百個C語言算法系列的第二篇,包括了經典的Fibonacci數列、簡易計算器、回文檢查、質數檢查等算法。也許他們能在你的畢業設計或者面試中派上用場。

1、計算Fibonacci數列

Fibonacci數列又稱斐波那契數列,又稱黃金分割數列,指的是這樣一個數列:1、1、2、3、5、8、13、21。

C語言實現的代碼如下:

/* Displaying Fibonacci sequence up to nth term where n is entered by user. */ #include <stdio.h> int main() { int count, n, t1=0, t2=1, display=0; printf("Enter number of terms: "); scanf("%d",&n); printf("Fibonacci Series: %d %d ", t1, t2); /* Displaying first two terms */ count=2; /* count=2 because first two terms are already displayed. */ while (count<n) { display=t1 t2; t1=t2; t2=display; count; printf("%d ",display); } return 0; }

結果輸出:

Enter number of terms: 10 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

也可以使用下面的源代碼:

/* Displaying Fibonacci series up to certain number entered by user. */ #include <stdio.h> int main() { int t1=0, t2=1, display=0, num; printf("Enter an integer: "); scanf("%d",&num); printf("Fibonacci Series: %d %d ", t1, t2); /* Displaying first two terms */ display=t1 t2; while(display<num) { printf("%d ",display); t1=t2; t2=display; display=t1 t2; } return 0; }

結果輸出:

Enter an integer: 200 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144

2、回文檢查

源代碼:

/* C program to check whether a number is palindrome or not */ #include <stdio.h> int main() { int n, Reverse=0, rem,temp; printf("Enter an integer: "); scanf("%d", &n); temp=n; while(temp!=0) { rem=temp; reverse=reverse*10 rem; temp/=10; } /* Checking if number entered by user and it's reverse number is equal. */ if(reverse==n) printf("%d is a palindrome.",n); else printf("%d is not a palindrome.",n); return 0; }

結果輸出:

Enter an integer: 12321 12321 is a palindrome.

3、質數檢查

注:1既不是質數也不是合數。

源代碼:

/* C program to check whether a number is prime or not. */ #include <stdio.h> int main() { int n, i, flag=0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=2;i<=n/2; i) { if(n%i==0) { flag=1; break; } } if (flag==0) printf("%d is a prime number.",n); else printf("%d is not a prime number.",n); return 0; }

結果輸出:

廣告商務合作,請聯系0755-33248146

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
杜拉拉升職記中的穿幫鏡頭
杜拉拉升職記中的穿幫鏡頭
2021年以來,友誼時光動作不斷,女性仙俠手遊《此生無白》和國風社交手遊《淩雲諾》的陸續曝光。今日,友誼時光旗再辟蹊徑,旗下正版都市職場養成手遊《杜拉拉升職記》正式公開亮相。這是友誼時光在女性手遊市場的一次全新嘗試,進一步探索品類的拓展,為...
2025-07-02
常見的工作崗位類型
常見的工作崗位類型
2023銀行秋招帷幕已經拉開!交通銀行和彙豐銀行正式啟動那下半年報考的崗位類别以及晉升之路有哪些呢?小王老師帶大家了解詳情~1銀行招聘崗位類别主流的崗位類别分為以下幾類:一般對外招聘的主流崗位:櫃員崗位、營銷類、信息科技類、産品支持類、風險...
2025-07-02
浙江十大美食名菜
浙江十大美食名菜
浙江,一個富有詩情畫意、山水秀麗之地。若是你來到此處,那“淡妝濃抹總相宜”的西湖,如詩如畫,如夢如幻,讓人沉迷其中流連忘返。而那有着“詩書之鄉”稱謂的南浔古鎮,更是散發着曆史的氣息,帶你走進了那個時代。而除了這些美景之外,美食的出現,也為這...
2025-07-02
excel怎樣每頁都有頁碼
excel怎樣每頁都有頁碼
一般在excel中插入的頁眉頁腳都是默認位置的,但是我們是可以更改它的位置的,來看一下具體步驟吧1、點擊頁面設置擴展按鈕——頁眉頁腳——自定義頁眉——選擇右部——輸入頁碼樣式——确定附上演示教程2、點擊頁邊距——将頁眉的數值改為6.5,具體...
2025-07-02
深圳龍崗今日防疫最新通告
深圳龍崗今日防疫最新通告
深圳龍崗今日防疫最新通告?6月22日上午,深圳市龍崗區橫崗街道接區裡指令,集結抗疫突擊隊支援南灣街道當天中午,由52人組成的支援隊伍在街道迅速集結,火速馳援南灣街道,現在小編就來說說關于深圳龍崗今日防疫最新通告?下面内容希望能幫助到你,我們...
2025-07-02
Copyright 2023-2025 - www.tftnews.com All Rights Reserved