首页
/
每日頭條
/
職場
/
c語言算法如何提高
c語言算法如何提高
更新时间:2026-05-21 16:31:37

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
推荐阅读
爸爸給孩子起名有多可怕
爸爸給孩子起名有多可怕
“本文由孕育島原創,歡迎個人轉發和分享”給孩子起名字是全家的一件大事,有時也讓人絞盡腦汁。起得好孩子日後容易被人記住,起不好就會鬧出笑話。有的父母大大咧咧,就連給孩子起名這件事都特别随意。于是誕生了一大票奇葩的名字,其中有的“辣眼睛”,也有...
2026-05-21
金力泰老闆
金力泰老闆
近日,金力泰(300225)公司内部的矛盾走向台前。公司3名董事、監事、高管辭職,其中,監事會主席王薇在辭職時把矛頭對準公司,稱監事會正常履行職權受到幹擾。不過金力泰卻表示,王薇所述與事實不符。上述情況受到了深交所的關注,3月24日早間,深...
2026-05-21
如何讓員工過好自己一生
如何讓員工過好自己一生
困惑(5)願意成長,願意付出,才是好員工韓雪麗困惑---面試溫惠這幾天陸續在面試,幾天下來,她得出一個結論。以公司目前的狀态,招聘有經驗又高學曆的,還不能馬上給員工入保險,這不可能,也不現實。李墨軒選定了家具,很聰明地轉了轉二手家具市場,這...
2026-05-21
許君聰賈玲張小斐風格一樣
許君聰賈玲張小斐風格一樣
今天,和大家談談娛樂圈的藝人創業的故事。事實上,很多藝人選擇單獨創業,或者合夥創業。畢竟,做老闆比單純賺錢輕松一些。娛樂圈裡有很多低調的老闆。楊幂、李冰冰、唐嫣等知名藝人都有自己的經紀公司,而且還簽了很多藝人!楊幂、郭德綱等人都是比較有名的...
2026-05-21
?早些年,經常會有親戚朋友向我發來4S店的檢修單,讓我看看保養項目是否存在貓膩,我來為大家講解一下關于?跟着小編一起來看一看吧!早些年,經常會有親戚朋友向我發來4S店的檢修單,讓我看看保養項目是否存在貓膩。(在以往4S店開出的保養清單中,很...
2026-05-21
Copyright 2023-2026 - www.tftnews.com All Rights Reserved