99精品伊人亚洲|最近国产中文炮友|九草在线视频支援|AV网站大全最新|美女黄片免费观看|国产精品资源视频|精彩无码视频一区|91大神在线后入|伊人终合在线播放|久草综合久久中文

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示

10大C語言基礎算法珍藏版源碼

Q4MP_gh_c472c21 ? 來源:嵌入式ARM ? 作者:嵌入式ARM ? 2020-11-16 15:07 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

算法是一個程序和軟件的靈魂,作為一名優(yōu)秀的程序員,只有對一些基礎的算法有著全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。本文是近百個C語言算法系列的第二篇,包括了經(jīng)典的Fibonacci數(shù)列、簡易計算器、回文檢查、質(zhì)數(shù)檢查等算法。也許他們能在你的畢業(yè)設計或者面試中派上用場。

1、計算Fibonacci數(shù)列

Fibonacci數(shù)列又稱斐波那契數(shù)列,又稱黃金分割數(shù)列,指的是這樣一個數(shù)列:1、1、2、3、5、8、13、21。

C語言實現(xiàn)的代碼如下:

/* Displaying Fibonacci sequence up to nth term where n is entered by user. */#include 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

結果輸出:

Enter number of terms: 10Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+

也可以使用下面的源代碼:

/* Displaying Fibonacci series up to certain number entered by user. */ #include 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

結果輸出:

Enter an integer: 200Fibonacci 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 int main(){ int n, reverse=0, rem,temp; printf("Enter an integer: "); scanf("%d", &n); temp=n; while(temp!=0) { rem=temp%10; 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: 1232112321 is a palindrome.

3、質(zhì)數(shù)檢查

注:1既不是質(zhì)數(shù)也不是合數(shù)。

源代碼:

/* C program to check whether a number is prime or not. */ #include 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;}

結果輸出:

Enter a positive integer: 2929 is a prime number.

4、打印金字塔和三角形

使用 * 建立三角形

** ** * ** * * ** * * * *

源代碼:

#include int main(){ int i,j,rows; printf("Enter the number of rows: "); scanf("%d",&rows);//微信搜索公眾號【C語言中文社區(qū)】關注回復C語言,免費領取200G學習資料 for(i=1;i<=rows;++i) { for(j=1;j<=i;++j) { printf("* "); } printf(" "); } return 0;}

如下圖所示使用數(shù)字打印半金字塔。

11 21 2 31 2 3 41 2 3 4 5

源代碼:

#include int main(){ int i,j,rows; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=1;i<=rows;++i) { for(j=1;j<=i;++j) { printf("%d ",j); } printf(" "); } return 0;}

用 * 打印半金字塔

* * * * ** * * ** * * * **

源代碼:

#include int main(){ int i,j,rows; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=rows;i>=1;--i) { for(j=1;j<=i;++j) { printf("* "); } printf(" "); } return 0;}

用 * 打印金字塔

* * * * * * * * * * * * * * * ** * * * * * * * *

源代碼:

#include int main(){ int i,space,rows,k=0; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=1;i<=rows;++i) { for(space=1;space<=rows-i;++space) { printf(" "); } while(k!=2*i-1) { printf("* "); ++k; } k=0; printf(" "); } return 0;}

用 * 打印倒金字塔

* * * * * * * * * * * * * * * * * * * * * * * * *

源代碼:

#includeint main(){ int rows,i,j,space; printf("Enter number of rows: "); scanf("%d",&rows); for(i=rows;i>=1;--i) { for(space=0;space

5、簡單的加減乘除計算器

源代碼:

/* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */ # include int main(){ char o; float num1,num2; printf("Enter operator either + or - or * or divide : "); scanf("%c",&o); printf("Enter two operands: "); scanf("%f%f",&num1,&num2); switch(o) { case '+': printf("%.1f + %.1f = %.1f",num1, num2, num1+num2); break; case '-': printf("%.1f - %.1f = %.1f",num1, num2, num1-num2); break; case '*': printf("%.1f * %.1f = %.1f",num1, num2, num1*num2); break; case '/': printf("%.1f / %.1f = %.1f",num1, num2, num1/num2); break; default: /* If operator is other than +, -, * or /, error message is shown */ printf("Error! operator is not correct"); break; } return 0;}

結果輸出:

Enter operator either + or - or * or divide : -Enter two operands: 3.48.43.4 - 8.4 = -5.0

6、檢查一個數(shù)能不能表示成兩個質(zhì)數(shù)之和

源代碼:

#include int prime(int n);int main(){ int n, i, flag=0;//微信搜索公眾號【C語言中文社區(qū)】關注回復C語言,免費領取200G學習資料 printf("Enter a positive integer: "); scanf("%d",&n); for(i=2; i<=n/2; ++i) { if (prime(i)!=0) { if ( prime(n-i)!=0) { printf("%d = %d + %d ", n, i, n-i); flag=1; } } } if (flag==0) printf("%d can't be expressed as sum of two prime numbers.",n); return 0;}int prime(int n) /* Function to check prime number */{ int i, flag=1; for(i=2; i<=n/2; ++i) if(n%i==0) flag=0; return flag;}

結果輸出:

Enter a positive integer: 3434 = 3 + 3134 = 5 + 2934 = 11 + 2334 = 17 + 17

7、用遞歸的方式顛倒字符串

源代碼:

/* Example to reverse a sentence entered by user without using strings. */ #include void Reverse();int main(){ printf("Enter a sentence: "); Reverse(); return 0;}void Reverse(){ char c; scanf("%c",&c); if( c != ' ') { Reverse(); printf("%c",c); }}

結果輸出:

Enter a sentence: margorp emosewaawesome program

8、實現(xiàn)二進制與十進制之間的相互轉(zhuǎn)換

/* C programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */ #include #include int binary_decimal(int n);int decimal_binary(int n);int main(){ int n; char c; printf("Instructions: "); printf("1. Enter alphabet 'd' to convert binary to decimal. "); printf("2. Enter alphabet 'b' to convert decimal to binary. "); scanf("%c",&c); if (c =='d' || c == 'D') { printf("Enter a binary number: "); scanf("%d", &n); printf("%d in binary = %d in decimal", n, binary_decimal(n)); } if (c =='b' || c == 'B') { printf("Enter a decimal number: "); scanf("%d", &n); printf("%d in decimal = %d in binary", n, decimal_binary(n)); } return 0;} int decimal_binary(int n) /* Function to convert decimal to binary.*/{ int rem, i=1, binary=0; while (n!=0) { rem=n%2; n/=2; binary+=rem*i; i*=10; } return binary;} int binary_decimal(int n) /* Function to convert binary to decimal.*/{ int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal;}

結果輸出:

9、使用多維數(shù)組實現(xiàn)兩個矩陣的相加

源代碼:

#include int main(){ int r,c,a[100][100],b[100][100],sum[100][100],i,j; printf("Enter number of rows (between 1 and 100): "); scanf("%d",&r); printf("Enter number of columns (between 1 and 100): "); scanf("%d",&c); printf(" Enter elements of 1st matrix: "); /* Storing elements of first matrix entered by user. */ for(i=0;i

結果輸出:

10、矩陣轉(zhuǎn)置

源代碼:

#include int main(){ int a[10][10], trans[10][10], r, c, i, j; printf("Enter rows and column of matrix: "); scanf("%d %d", &r, &c); /* Storing element of matrix entered by user in array a[][]. */ printf(" Enter elements of matrix: "); for(i=0; i

責任編輯:lq

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 算法
    +關注

    關注

    23

    文章

    4709

    瀏覽量

    95367
  • C語言
    +關注

    關注

    180

    文章

    7632

    瀏覽量

    141696
  • 代碼
    +關注

    關注

    30

    文章

    4900

    瀏覽量

    70724

原文標題:經(jīng)常遇到的10大C語言基礎算法(珍藏版源碼)

文章出處:【微信號:gh_c472c2199c88,微信公眾號:嵌入式微處理器】歡迎添加關注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    單片機常用算法源碼下載!

    單片機常用算法源碼下載!
    發(fā)表于 06-10 20:44

    深入理解C語言C語言循環(huán)控制

    C語言編程中,循環(huán)結構是至關重要的,它可以讓程序重復執(zhí)行特定的代碼塊,從而提高編程效率。然而,為了避免程序進入無限循環(huán),C語言提供了多種循環(huán)控制語句,如break、continue和
    的頭像 發(fā)表于 04-29 18:49 ?1142次閱讀
    深入理解<b class='flag-5'>C</b><b class='flag-5'>語言</b>:<b class='flag-5'>C</b><b class='flag-5'>語言</b>循環(huán)控制

    C語言的歷史及程序介紹

    電子發(fā)燒友網(wǎng)站提供《C語言的歷史及程序介紹.pdf》資料免費下載
    發(fā)表于 04-09 16:10 ?0次下載

    全套C語言培訓資料—PPT課件

    全套C語言培訓資料,共427頁,13個章節(jié):C語言概述、程序的靈魂—算法、數(shù)據(jù)類型 & 運算符與表達式、順序程序設計、選擇結構程序設
    發(fā)表于 03-12 14:50

    限時免積分下載:增量式與位置式PID算法C語言實現(xiàn)分享

    前面咱們有分享對PID算法離散化和增量式PID算法原理進行來探索,之后又使用Matlab進行了仿真實驗,對PID三個參數(shù)又有了更深入的認識,接下來我們來使用C語言進行PID
    發(fā)表于 03-05 18:32

    PID控制算法C語言實現(xiàn):PID算法原理

    在工業(yè)應用中 PID 及其衍生算法是應用最廣泛的算法之一,是當之無愧的萬能算法,如果能夠熟練掌握 PID 算法的設計與實現(xiàn)過程,對于一般的研發(fā)人員來講,應該是足夠應對一般研發(fā)問題了,而
    發(fā)表于 02-26 15:24

    C語言指針學習筆記

    本文從底層內(nèi)存分析,徹底讓讀者明白C語言指針的本質(zhì)。
    的頭像 發(fā)表于 11-05 17:40 ?639次閱讀
    <b class='flag-5'>C</b><b class='flag-5'>語言</b>指針學習筆記

    C語言中的socket編程基礎

    Socket編程簡介 Socket是一種通信機制,允許程序之間進行通信。在C語言中,socket編程是網(wǎng)絡編程的基礎。通過使用socket,程序可以發(fā)送和接收數(shù)據(jù),實現(xiàn)不同計算機之間的通信
    的頭像 發(fā)表于 11-01 16:51 ?1227次閱讀

    C語言C++中結構體的區(qū)別

    同樣是結構體,看看在C語言C++中有什么區(qū)別?
    的頭像 發(fā)表于 10-30 15:11 ?767次閱讀

    C語言與Java語言的對比

    C語言和Java語言都是當前編程領域中的重要成員,它們各自具有獨特的優(yōu)勢和特點,適用于不同的應用場景。以下將從語法特性、內(nèi)存管理、跨平臺性、性能、應用領域等多個方面對C
    的頭像 發(fā)表于 10-29 17:31 ?1069次閱讀

    C語言與其他編程語言的比較

    C語言作為一種歷史悠久的編程語言,自其誕生以來,一直在軟件開發(fā)領域扮演著重要角色。它以其高效、靈活和可移植性強的特點,成為了系統(tǒng)級編程的首選語言之一。
    的頭像 發(fā)表于 10-29 17:30 ?1079次閱讀

    C語言生成可執(zhí)行二進制文件的具體過程

    C語言源碼到生成可執(zhí)行文件的過程通常包括預處理(Preprocessing)、編譯(Compilation)、匯編(Assembly)、鏈接(Linking)等多個步驟,每個步驟都有其特定的任務
    的頭像 發(fā)表于 10-21 14:30 ?1615次閱讀
    <b class='flag-5'>C</b><b class='flag-5'>語言</b>生成可執(zhí)行二進制文件的具體過程

    ADC3101里邊自帶的miniDSP能夠支持自定義的C語言開發(fā)嗎?

    想咨詢一下, ADC3101里邊自帶的miniDSP能夠支持自定義的C語言開發(fā)嗎?比如把自己算法灌進去,實現(xiàn)某些算法,比如能做FFT嗎。還是說這個DSP只能支持簡單的濾波
    發(fā)表于 10-14 07:03

    C加密算法的實現(xiàn)

    電子發(fā)燒友網(wǎng)站提供《C加密算法的實現(xiàn).pdf》資料免費下載
    發(fā)表于 09-20 11:10 ?1次下載
    <b class='flag-5'>C</b>加密<b class='flag-5'>算法</b>的實現(xiàn)

    C++語言基礎知識

    電子發(fā)燒友網(wǎng)站提供《C++語言基礎知識.pdf》資料免費下載
    發(fā)表于 07-19 10:58 ?10次下載