chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

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

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

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

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

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

掃碼添加小助手

加入工程師交流群

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

1、計(jì)算Fibonacci數(shù)列

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

C語言實(shí)現(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);//微信搜索公眾號(hào)【C語言中文社區(qū)】關(guān)注回復(fù)C語言,免費(fèi)領(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、簡單的加減乘除計(jì)算器

源代碼:

/* 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、檢查一個(gè)數(shù)能不能表示成兩個(gè)質(zhì)數(shù)之和

源代碼:

#include int prime(int n);int main(){ int n, i, flag=0;//微信搜索公眾號(hào)【C語言中文社區(qū)】關(guān)注回復(fù)C語言,免費(fèi)領(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、實(shí)現(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ù)組實(shí)現(xiàn)兩個(gè)矩陣的相加

源代碼:

#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)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 算法
    +關(guān)注

    關(guān)注

    23

    文章

    4784

    瀏覽量

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

    關(guān)注

    183

    文章

    7644

    瀏覽量

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

    關(guān)注

    30

    文章

    4968

    瀏覽量

    73990

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

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

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

掃碼添加小助手

加入工程師交流群

    評論

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

    fft算法c語言的實(shí)現(xiàn)

    問題之前我煩惱了一會(huì)兒,但別忘了。 前者的展開是N/2,因?yàn)榇藭r(shí)n是奇偶校驗(yàn)分離的,是從根據(jù)的可約束性得出的,在此不能混淆。 沒有必要提到計(jì)算效率。 m級(jí)計(jì)算總共需要: FFT c語言實(shí)現(xiàn) FFT算法
    發(fā)表于 01-27 06:10

    講解C語言代碼的實(shí)現(xiàn)過程

    重點(diǎn)講解C語言代碼的實(shí)現(xiàn)過程,算法C語言實(shí)現(xiàn)過程具有一般性,通過PID算法
    發(fā)表于 01-21 07:58

    C語言的PID算法

    C語言PID算法#include \"stdio.h\" #include //定義PID結(jié)構(gòu)體 struct _pid{ float SetSpeed; float
    發(fā)表于 01-16 08:13

    C語言增量式PID的通用算法

    C語言增量式PID通用算法控制算法中PID使用的非常廣泛,但是在網(wǎng)上找代碼的時(shí)候發(fā)現(xiàn)好多代碼都不夠通用,需要自己改好多東西,而且當(dāng)一個(gè)項(xiàng)目需要使用多個(gè)PID控制器時(shí)也頗為麻煩,這里設(shè)計(jì)
    發(fā)表于 01-14 08:28

    【精選活動(dòng)】缺陷系統(tǒng)檢測不走坑!10年+資深LabVIEW視覺專家全套珍藏

    “告別檢測系統(tǒng)能力缺陷!10+年LabVIEW視覺資深專家手把手教你:5000+分鐘高清教程(含工具、算法原理、實(shí)戰(zhàn)操作、項(xiàng)目優(yōu)化全流程講解)”——從傳統(tǒng)視覺算法→深度學(xué)習(xí)建模→工業(yè)級(jí)部署"
    的頭像 發(fā)表于 12-30 08:06 ?292次閱讀
    【精選活動(dòng)】缺陷系統(tǒng)檢測不走坑!<b class='flag-5'>10</b>年+資深LabVIEW視覺專家全套<b class='flag-5'>珍藏</b>

    C語言C++之間的區(qū)別是什么

    (STL),包含多種容器(如vector、list、map等)、算法以及迭代器,極大地提高了開發(fā)效率和代碼復(fù)用性。 而C語言的標(biāo)準(zhǔn)庫相對較小,雖然也提供了基本的數(shù)據(jù)結(jié)構(gòu)(如數(shù)組、鏈表等)和算法
    發(fā)表于 12-11 06:23

    為什么單片機(jī)還在用C語言編程?

    的缺陷 高級(jí)語言存在的目的是可以實(shí)現(xiàn)更為優(yōu)化的算法,更多的是為了方便的執(zhí)行方案,但是,高級(jí)語言對程序存儲(chǔ)空間的占用要比匯編和C語言多很多。
    發(fā)表于 11-28 07:37

    C語言的常見算法

    # C語言常見算法 C語言中常用的算法可以分為以下幾大類: ## 1. 排序
    發(fā)表于 11-24 08:29

    C語言的常量介紹

    在程序執(zhí)行過程中,值不發(fā)生改變的量稱為常量。 mtianyan: C語言的常量可以分為直接常量和符號(hào)常量。 直接常量也稱為字面量,是可以直接拿來使用,無需說明的量,比如: 整型常量:13、0
    發(fā)表于 11-24 07:12

    C語言和單片機(jī)C語言有什么差異

    單片機(jī)c語言相對于普通C語言增加了一些基本的指令,還有變量的賦值是16進(jìn)制,當(dāng)然單片機(jī)c語言只牽
    發(fā)表于 11-14 07:55

    復(fù)雜的軟件算法硬件IP核的實(shí)現(xiàn)

    具體方法與步驟 通過 C 語言實(shí)現(xiàn)軟件算法,并驗(yàn)證了算法的有效性以后,就可以進(jìn)行算法的 HDL 轉(zhuǎn)化工作了。通過使用 Altium Des
    發(fā)表于 10-30 07:02

    單片機(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 ?2050次閱讀
    深入理解<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èi)下載
    發(fā)表于 04-09 16:10 ?0次下載

    全套C語言培訓(xùn)資料—PPT課件

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