首页
/
每日頭條
/
職場
/
c語言算法如何提高
c語言算法如何提高
更新时间:2026-03-02 12:01:03

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
推荐阅读
樂視到底能不能翻身(沒有老闆和)
樂視到底能不能翻身(沒有老闆和)
     樂融大廈仍有超四百位員工在此辦公。 (南方周末記者 梅嶺/圖)   “樂視輝煌時、欠債時,我們天天在你們大廈底下排隊趴活,現在樂視還在嗎?”樂視網(400084.OC)員工馬賽打車上下班時,經常會遭到出租車司機調侃。   “我們就沒有倒下過,隻是現在還在爬。”馬賽對南方周末記者說,2010年她入職時,工号排在兩百多位,鼎盛時期兩萬多名員工跟随賈躍亭...
2026-03-02
升職加薪時什麼樣的員工最優先(容易被提拔與加薪的員工)
升職加薪時什麼樣的員工最優先(容易被提拔與加薪的員工)
  文|小蝸牛、企業首席人事官全文:620字、閱讀需2分鐘非頭條發布的本文均是抄襲  【工作題記】無論什麼時候,無論在什麼樣的公司,凡是涉及到員工升職和加薪的事情,往往都是比較糾結的事情,而且大部分情況都難以做到公平的結果。   但是企業又不能不提拔員工,從多年的從業經曆來看,最容易被公司升職和加薪的人,往往具有共同的特征,整體梳理成以下3點。      【...
2026-03-02
時尚又減齡的圍巾系法一定要學會(時髦有型的8種圍巾系法)
時尚又減齡的圍巾系法一定要學會(時髦有型的8種圍巾系法)
  尤其是像時尚博主Leonie Hanne這種粗棒針的針織款,堆砌會讓人顯得很臃腫,而随意的披挂反而成為搭配增添了亮點。      時尚博主Leonie Hanne   時尚博主Charlotte Bridgeman、和時尚博主Marina the Moss這種亮色的流蘇圍巾也很适合冬天,輕輕松松就能從滿大街的黑白灰中跳脫出來!      時尚博主Char...
2026-03-02
dnf瞎子下個版本加強嗎(17個職業平衡性調整)
dnf瞎子下個版本加強嗎(17個職業平衡性調整)
  今日,DNF進行了版本大更新,其中就是大家期待已久的職業平衡性調整,本次的版本更新一共涉及到17個職業,下面台長就來和大家一一盤點,看看對你的職業是否有影響:   1、阿修羅  鬼印珠、無雙波、邪光波動陣、暗天波動眼、雷神之息、天雷 · 波動劍 變更為獨立攻擊。(這樣的變化等于是由百分比職業變更為固傷職業了,有望告别下水道)   心眼 技能增加5%的命中...
2026-03-02
dnf60版本各個副本前置(DNF60版本異界回憶啟示錄)
dnf60版本各個副本前置(DNF60版本異界回憶啟示錄)
  說起DNF是大家已經在熟悉不過的網絡遊戲了,是由韓國NEOPLE開發的,于2008年在國服正式登陸,是一款免費角色扮演的2D闖關遊戲,距離至今已十餘年之久,而我們也與DNF風雨同舟十餘年,現在回憶起已是慢慢的回憶,甚至有時候還熱淚盈眶,懷念當時一起組隊刷圖升級,懷念當年一起組隊刷深淵,還念一起去PK場PK的成年往事;說道回憶各位小夥伴們還記的之前最開始的...
2026-03-02
Copyright 2023-2026 - www.tftnews.com All Rights Reserved