首页
/
每日頭條
/
職場
/
c語言算法如何提高
c語言算法如何提高
更新时间:2026-07-24 11:18:07

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
推荐阅读
地下城與勇士手遊排位怎麼加點
地下城與勇士手遊排位怎麼加點
狂戰士在DNF中的地位是不可撼動的,是各位勇士們關注的焦點,那麼DNF手遊狂戰士PK怎麼連招?DNF手遊狂戰士PK怎麼加點?下面就跟着小編一起來了解下。精彩内容不要錯過!1、狂戰士基礎連招①、首先是起手技能,玩家應盡量以崩山擊為起手,範圍攻...
2026-07-24
綜藝集團注資貴州醇
綜藝集團注資貴州醇
2月24日,貴州醇酒業有限公司、湖北枝江酒業股份有限公司、貴州青酒酒廠有限公司董事長兼總經理朱偉在社交媒體上發布的疑似未成年人“飲酒”照片引起争議。28日,貴州醇方面回應南都記者稱,照片的發布初衷僅僅是因為朱偉在朋友圈看到員工子女拍的古裝創...
2026-07-24
上海震旦職業學院退費
上海震旦職業學院退費
近日,備受關注的上海震旦職業學院教師“不當言論事件”發酵後,有網友發現,在多個工商信息查詢網站上,“上海震旦職業學院”公司狀态顯示已注銷。潇湘晨報(報料微信xxcbbaoliao)記者查詢多個工商信息查詢網站發現,上海震旦職業學院已依法注銷...
2026-07-24
導遊證的面試部分考的是哪些
導遊證的面試部分考的是哪些
各位準備參加2019年度全國導遊資格考試面試的朋友請注意!2019年度全國導遊資格考試秦皇島地區面試考試有關工作和要求通知來了!全力備考固然重要,但也千萬不要忘了看考試通知哦~考試時間安排2019年12月3日至12月5日(具體時間見準考證)...
2026-07-24
管理人員的工作職責有哪些
管理人員的工作職責有哪些
管理人員的工作職責有哪些?負責制定并保持公司的質量、環境、職業健康安全方針、目标;并決定有關質量、環境、職業健康安全方針和目标的措施,現在小編就來說說關于管理人員的工作職責有哪些?下面内容希望能幫助到你,我們來一起看看吧!管理人員的工作職責...
2026-07-24
Copyright 2023-2026 - www.tftnews.com All Rights Reserved