在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

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

10大C語言基礎(chǔ)算法珍藏版源碼

Q4MP_gh_c472c21 ? 來源:嵌入式ARM ? 作者:嵌入式ARM ? 2020-11-16 15:07 ? 次閱讀

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

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

結(jié)果輸出:

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

結(jié)果輸出:

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;}

結(jié)果輸出:

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;}

結(jié)果輸出:

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ū)】關(guān)注回復(fù)C語言,免費領(lǐng)取200G學(xué)習(xí)資料 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;}

結(jié)果輸出:

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ū)】關(guān)注回復(fù)C語言,免費領(lǐng)取200G學(xué)習(xí)資料 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;}

結(jié)果輸出:

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); }}

結(jié)果輸出:

Enter a sentence: margorp emosewaawesome program

8、實現(xiàn)二進(jìn)制與十進(jì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;}

結(jié)果輸出:

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

結(jié)果輸出:

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

責(zé)任編輯:lq

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

    關(guān)注

    23

    文章

    4702

    瀏覽量

    94976
  • C語言
    +關(guān)注

    關(guān)注

    180

    文章

    7630

    瀏覽量

    140763
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4891

    瀏覽量

    70369

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

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

收藏 人收藏

    評論

    相關(guān)推薦
    熱點推薦

    單片機(jī)常用算法源碼下載!

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

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

    C語言編程中,循環(huán)結(jié)構(gòu)是至關(guān)重要的,它可以讓程序重復(fù)執(zhí)行特定的代碼塊,從而提高編程效率。然而,為了避免程序進(jìn)入無限循環(huán),C語言提供了多種循環(huán)控制語句,如break、continue和
    的頭像 發(fā)表于 04-29 18:49 ?967次閱讀
    深入理解<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語言培訓(xùn)資料—PPT課件

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

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

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

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

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

    C語言指針學(xué)習(xí)筆記

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

    C語言C++中結(jié)構(gòu)體的區(qū)別

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

    C語言與Java語言的對比

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

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

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

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

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

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

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

    C++語言基礎(chǔ)知識

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

    如何用java語言開發(fā)一套數(shù)字化產(chǎn)科系統(tǒng)? 數(shù)字化產(chǎn)科管理平臺源碼

    如何用java語言開發(fā)一套數(shù)字化產(chǎn)科系統(tǒng) 數(shù)字化產(chǎn)科管理平臺源碼
    的頭像 發(fā)表于 07-06 09:38 ?1250次閱讀
    如何用java<b class='flag-5'>語言</b>開發(fā)一套數(shù)字化產(chǎn)科系統(tǒng)? 數(shù)字化產(chǎn)科管理平臺<b class='flag-5'>源碼</b>

    按照這樣學(xué)習(xí)C語言,成為卷王不是夢!

    在計算機(jī)編程領(lǐng)域,C語言被譽(yù)為一種強(qiáng)大而靈活的編程語言,掌握好C語言不僅可以讓你輕松駕馭各種編程任務(wù),還能夠為你的職業(yè)生涯打下堅實的基礎(chǔ)。但
    的頭像 發(fā)表于 07-06 08:04 ?589次閱讀
    按照這樣學(xué)習(xí)<b class='flag-5'>C</b><b class='flag-5'>語言</b>,成為卷王不是夢!
    主站蜘蛛池模板: 黑粗硬大欧美视频 | 人人玩人人添天天爽 | 免费播放一区二区三区 | 亚洲一区二区三区中文字幕5566 | 久久99热久久精品 | 欧美色图888 | 精品综合久久88色鬼首页 | 黄色工厂在线播放 | 丁香伊人五月综合激激激 | 天天毛片| yy肉戏多纯黄的小说 | 插插操操| 色爱区综合 | 米奇精品一区二区三区 | 久久久久国产一级毛片高清版 | xxxx 欧美| 美女黄页黄频 | 久久久久久久国产 | 曰本又色又爽又黄三级视频 | 成年香蕉大黄美女美女 | 亚洲成人免费观看 | 俄罗斯一级成人毛片 | 奇米影视婷婷 | 五月婷婷 六月丁香 | 四虎国产精品永久在线 | 偷偷要色偷偷 | 性免费网站 | 河南毛片| 丁香综合激情 | 亚洲精品视频在线看 | 嫩草影院久久国产精品 | 在线色综合 | 中文天堂最新版在线精品 | 日日做日日摸夜夜爽 | 日韩精品一区二区三区免费视频 | 曰本毛片 | 手机在线视频你懂的 | 日本黄色大片免费 | 教官的好爽好深h片段 | 青青伊人91久久福利精品 | 黄乱色伦短篇小说h |