首页
/
每日頭條
/
職場
/
c語言算法如何提高
c語言算法如何提高
更新时间:2026-04-10 00:49:24

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
推荐阅读
月薪9k的工作有哪些
月薪9k的工作有哪些
馬雲說“996是福報”;薛兆豐說“996是為自己的簡曆打工”;李誕說“工作才是人生的意義”……就像奇葩說的辯題一樣“喜歡的工作都要996,我們到底要不要996呢?”加班or薪資?@石頭211财經本,統計專業。秋招到現在隻拿到一個平安産險總部...
2026-04-10
基本工資是指哪些
基本工資是指哪些
基本工資是指哪些?,下面我們就來聊聊關于基本工資是指哪些?接下來我們就一起去了解一下吧!基本工資是指哪些勞動者基本工資是根據勞動合同約定或國家及企業規章制度規定的工資标準計算的工資。也稱标準工資。在一般情況下,基本工資是職工勞動報酬的主要部...
2026-04-10
微信如何将工作溝通轉至企業微信
微信如何将工作溝通轉至企業微信
商業洞察丨作者/殷思源自從有了微信之後,好像我們的生活就變得立體了。生活、工作、學習、娛樂、支付等等都可以在微信上完成。就微信對工作的影響而言,有人說微信的出現讓自己的社交面積變大了;有人說微信讓工作變成了24小時standby;有人說微信...
2026-04-10
wps文檔表格100個常用技巧
wps文檔表格100個常用技巧
加粗單個單元格及自定義邊框線條的方法我們在利用Excel表格進行日常辦公時,經常需要制作各種各樣的表格,之前我們都是對表格批量進行邊框更改,那麼如果我們想要單獨對某一個單元格進行邊框加粗,該如何操作呢,今天我們就來教大家如何在Excel表格...
2026-04-10
平均工資統計表
平均工資統計表
根據網絡資料,國家統計局發布2021年全國平均工資數據。2021年全國城鎮非私營單位就業人員年平均工資為106837元,城鎮私營單位就業人員年平均工資為62884元,規模以上企業就業人員年平均工資為88115元。截至6月5日,至少已有15個...
2026-04-10
Copyright 2023-2026 - www.tftnews.com All Rights Reserved