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

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
推荐阅读
青年對職業生涯的規劃
青年對職業生涯的規劃
5月28日下午,一場别開生面的學生職業生涯規劃大賽決賽在線上開展,兩千五百餘人在線觀看了選手們的風采展示!“我的職業目标是成為新疆兵團建設者,到祖國最需要的地方去,回到家鄉,建設新疆!”“我的職業理想是成為一名大健康産業的産品研發工程師,助...
2026-03-06
世界最窮總統排行
世界最窮總統排行
他上班時是個總統,下班後卻是個農民,他是世界上最奇葩的國家領導人,也是被媒體公認的世界上最貧窮的總統,他就是烏拉圭總統穆希卡。穆希卡出生于烏拉圭首都蒙得維的一個小鎮上,他曾是一名自行車運動員。後因反對軍人獨裁,開始參與到政治生活中來。200...
2026-03-06
nba現個人最高薪水排行
nba現個人最高薪水排行
北京時間9月17日,據NBA知名記者查拉尼亞透露,聯盟已經告知各球隊最新的工資帽和奢侈稅起征點。2020-21賽季,工資帽是1.16億美元,奢侈稅起征點是1.41億美元,2021-22賽季,工資帽是1.25億美元,奢侈稅起征點是1.51億美...
2026-03-06
高速傳媒聘任蔣曉宇為副總經理怎麼樣
高速傳媒聘任蔣曉宇為副總經理怎麼樣
高速傳媒聘任蔣曉宇為副總經理怎麼樣?挖貝網3月8日消息,高速傳媒(870821)發公告稱,根據《公司法》及《公司章程》的有關規定,公司于2022年3月7日召開了第二屆董事會第十三次會議,審議并通過了《關于聘任常務副總經理的議案》:聘任蔣曉宇...
2026-03-06
優秀的pmo和項目經理
優秀的pmo和項目經理
Q“對于項目經曆感受性問題”(例如最有成就感、最困難、印象最深刻……)攻略:“3個特征4個步驟”AB結構化的描述,把AB中涉及的内容穿插到聊天内容中,核心目的就是通過回答這個問題,推銷自己,證明自己可以為企業創造價值,為領導分憂,畢竟面試你...
2026-03-06
Copyright 2023-2026 - www.tftnews.com All Rights Reserved