首页
/
每日頭條
/
職場
/
c語言算法如何提高
c語言算法如何提高
更新时间:2025-12-01 10:30:45

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
推荐阅读
對管理咨詢的個人思考
對管理咨詢的個人思考
近些年來,随着中國人力資源管理的發展,企業對于人力資源管理,尤其是人力資源管理中的績效管理模塊更加重視,各類型企業競相學習績效管理的知識和理論,并且也會将從各種渠道、各類“大師”傳授的績效管理“秘籍”應用到企業管理中去。從大部分人力資源從業...
2025-12-01
富士康脫崗事件處理
富士康脫崗事件處理
記者/李想俣實習記者/王一凡王琳茜呂惠編輯/楊寶璐段飛和很多工友在一起等待轉運11月6号,段飛發現自己“陽”了。這是自10月底富士康鄭州航空港廠區爆出員工徒步返鄉的視頻後,他留守在廠區的第9天。當天早上,他收到公司的短信,被告知前一天的檢測...
2025-12-01
uzi背後的故事
uzi背後的故事
食物系列-MLXG麻辣香鍋的大名如雷貫耳,要問香鍋名字的由來,那就太簡單了,愛吃而已。第一屆國服第一上單抗壓王當屬“一血哥”草莓,和香鍋一樣也純粹是因為愛吃。crisp因為愛吃薯片。随意系列——UZIUZI的ID有兩個說法,第一個說法是UZ...
2025-12-01
fate所對應的職業
fate所對應的職業
11區常常被人稱為全民中二病的國家,各種動漫梗常伴其身。最近,推主オタコスカメラマン在大學的職業規劃課上,就猛然發現老師引用了這樣一位熟悉的”偉人“的名言……PPT上可以看到如下字樣:”偉人曾經說過,‘想象的永遠都是最強的自己’。“配圖則是...
2025-12-01
優化工傷認定的建議
優化工傷認定的建議
随着寶山區複工複産工作的啟動推進,區人社局下屬社會保障服務中心采用“駐守單位集中辦公+居家辦公”雙管齊下的工作模式,加快恢複行政服務的步伐,開設工傷認定審批“綠色通道”和“熱線電話”,及時做好疫情期間醫護等抗疫一線工作人員工傷案件辦理工作,...
2025-12-01
Copyright 2023-2025 - www.tftnews.com All Rights Reserved