国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

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

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

3天內不再提示

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

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

掃碼添加小助手

加入工程師交流群

算法是一個程序和軟件的靈魂,作為一名優秀的程序員,只有對一些基礎的算法有著全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。本文是近百個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 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、質數檢查

注:1既不是質數也不是合數。

源代碼:

/* 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語言中文社區】關注回復C語言,免費領取200G學習資料 for(i=1;i<=rows;++i) { for(j=1;j<=i;++j) { printf("* "); } printf(" "); } return 0;}

如下圖所示使用數字打印半金字塔。

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、檢查一個數能不能表示成兩個質數之和

源代碼:

#include int prime(int n);int main(){ int n, i, flag=0;//微信搜索公眾號【C語言中文社區】關注回復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、實現二進制與十進制之間的相互轉換

/* 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、使用多維數組實現兩個矩陣的相加

源代碼:

#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、矩陣轉置

源代碼:

#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

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

    關注

    23

    文章

    4784

    瀏覽量

    98044
  • C語言
    +關注

    關注

    183

    文章

    7644

    瀏覽量

    145583
  • 代碼
    +關注

    關注

    30

    文章

    4968

    瀏覽量

    73961

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

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

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

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    fft算法c語言的實現

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

    講解C語言代碼的實現過程

    重點講解C語言代碼的實現過程,算法C語言實現過程具有一般性,通過PID算法
    發表于 01-21 07:58

    C語言的PID算法

    C語言PID算法#include \"stdio.h\" #include //定義PID結構體 struct _pid{ float SetSpeed; float
    發表于 01-16 08:13

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

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

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

    “告別檢測系統能力缺陷!10+年LabVIEW視覺資深專家手把手教你:5000+分鐘高清教程(含工具、算法原理、實戰操作、項目優化全流程講解)”——從傳統視覺算法→深度學習建?!I級部署"
    的頭像 發表于 12-30 08:06 ?284次閱讀
    【精選活動】缺陷系統檢測不走坑!<b class='flag-5'>10</b>年+資深LabVIEW視覺專家全套<b class='flag-5'>珍藏</b>

    C語言C++之間的區別是什么

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

    為什么單片機還在用C語言編程?

    的缺陷 高級語言存在的目的是可以實現更為優化的算法,更多的是為了方便的執行方案,但是,高級語言對程序存儲空間的占用要比匯編和C語言多很多。
    發表于 11-28 07:37

    C語言的常見算法

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

    C語言的常量介紹

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

    C語言和單片機C語言有什么差異

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

    復雜的軟件算法硬件IP核的實現

    具體方法與步驟 通過 C 語言實現軟件算法,并驗證了算法的有效性以后,就可以進行算法的 HDL 轉化工作了。通過使用 Altium Des
    發表于 10-30 07:02

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

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

    深入理解C語言C語言循環控制

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

    C語言的歷史及程序介紹

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

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

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