一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

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

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

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

10個經(jīng)典C語言面試基礎(chǔ)算法及代碼

黃工的嵌入式技術(shù)圈 ? 來源:黃工的嵌入式技術(shù)圈 ? 作者:黃工的嵌入式技術(shù) ? 2020-01-16 11:09 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

鏈接:http://www.codeceo.com/article/10-c-interview-algorithm.html

算法是一個程序和軟件的靈魂,作為一名優(yōu)秀的程序員,只有對一些基礎(chǔ)的算法有著全面的掌握,才會在設(shè)計程序和編寫代碼的過程中顯得得心應(yīng)手。

一、計算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 { display=t1+t2; t1=t2; t2=display; ++count; printf("%d+",display); } return 0;}

結(jié)果輸出:

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

二、回文檢查

源代碼:

/* 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.

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

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

源代碼:

/*Cprogramtocheckwhetheranumberisprimeornot.*/#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.

四、打印金字塔和三角形

使用 * 建立三角形

** ** * ** * * ** * * * * 源代碼: #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("* "); } 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;}

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

源代碼:

/*Sourcecodetocreateasimplecalculatorforaddition,subtraction,multiplicationanddivisionusingswitch...casestatementinCprogramming.*/# 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

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

源代碼:

#include int prime(int n);int main(){ int n, i, flag=0; 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;} /* Function to check prime number */int prime(int n){ 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

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

源代碼:

/* 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

八、實現(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;} /* Function to convert decimal to binary.*/int decimal_binary(int n){ int rem, i=1, binary=0; while (n!=0) { rem=n%2; n/=2; binary+=rem*i; i*=10; } return binary;} /* Function to convert binary to decimal.*/int binary_decimal(int n){ int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal;}

結(jié)果輸出:

九、使用多維數(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 for(j=0;j { printf("Enter element a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } /* Storing elements of second matrix entered by user. */ printf("Enter elements of 2nd matrix: "); for(i=0;i for(j=0;j { printf("Enter element a%d%d: ",i+1,j+1); scanf("%d",&b[i][j]); } /*Adding Two matrices */ for(i=0;i for(j=0;j sum[i][j]=a[i][j]+b[i][j]; /* Displaying the resultant sum matrix. */ printf(" Sum of two matrix is: "); for(i=0;i for(j=0;j { printf("%d ",sum[i][j]); if(j==c-1) printf(" "); } return 0;}

結(jié)果輸出:

十、矩陣轉(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 for(j=0; j { printf("Enter elements a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } /* Displaying the matrix a[][] */ printf(" Entered Matrix: "); for(i=0; i for(j=0; j { printf("%d ",a[i][j]); if(j==c-1) printf(" "); } /* Finding transpose of matrix a[][] and storing it in array trans[][]. */ for(i=0; i for(j=0; j { trans[j][i]=a[i][j]; } /* Displaying the transpose,i.e, Displaying array trans[][]. */ printf(" Transpose of Matrix: "); for(i=0; i for(j=0; j { printf("%d ",trans[i][j]); if(j==r-1) printf(" "); } return 0;}

結(jié)果輸出:

聲明:本文內(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

    文章

    4708

    瀏覽量

    95268
  • 基礎(chǔ)知識
    +關(guān)注

    關(guān)注

    0

    文章

    82

    瀏覽量

    27008
  • C語言編程
    +關(guān)注

    關(guān)注

    6

    文章

    90

    瀏覽量

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

    關(guān)注

    30

    文章

    4899

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

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

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

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

    必看!15C語言常見陷阱及避坑指南

    ? C語言雖強大,但隱藏的“坑”也不少!稍不留神就會導(dǎo)致程序崩潰、數(shù)據(jù)異常。本文整理15高頻陷阱,助你寫出更穩(wěn)健的代碼! ? 陷阱1:運算符優(yōu)先級混淆? 問題:運算符優(yōu)先級不同可能導(dǎo)
    的頭像 發(fā)表于 03-16 12:10 ?594次閱讀

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

    全套C語言培訓(xùn)資料,共427頁,13章節(jié):C語言概述、程序的靈魂—算法、數(shù)據(jù)類型 &
    發(fā)表于 03-12 14:50

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

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

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

    的是,在我所接觸的控制算法當(dāng)中,PID 控制算法又是最簡單,最能體現(xiàn)反饋思想的控制算法,可謂經(jīng)典中的經(jīng)典。
    發(fā)表于 02-26 15:24

    代碼加密、源代碼防泄漏c/c++與git服務(wù)器開發(fā)環(huán)境

    代碼加密對于很多研發(fā)性單位來說是至關(guān)重要的,當(dāng)然每家企業(yè)的業(yè)務(wù)需求不同所用的開發(fā)環(huán)境及開發(fā)語言也不盡相同,今天主要來講一下c++及git開發(fā)環(huán)境的源代碼防泄密保護方案。企業(yè)源
    的頭像 發(fā)表于 02-12 15:26 ?521次閱讀
    源<b class='flag-5'>代碼</b>加密、源<b class='flag-5'>代碼</b>防泄漏<b class='flag-5'>c</b>/<b class='flag-5'>c</b>++與git服務(wù)器開發(fā)環(huán)境

    分析C語言代碼結(jié)構(gòu)的設(shè)計問題

    來分析一C語言代碼結(jié)構(gòu)的設(shè)計問題。 這段代碼,使用了兩次malloc,分別給 p1 和 p2 申請了內(nèi)存。用完后,內(nèi)存釋放,防止內(nèi)存泄漏。
    的頭像 發(fā)表于 02-11 09:31 ?360次閱讀

    AKI跨語言調(diào)用庫神助攻C/C++代碼遷移至HarmonyOS NEXT

    產(chǎn)品創(chuàng)新與功能迭代,而非技術(shù)遷移的細節(jié)問題,大幅提升開發(fā)效率。 據(jù)悉,在涉及C/C++/ETS跨越語言調(diào)用的鴻蒙化應(yīng)用中,有超過80%的項目都在使用AKI,如某知名購物應(yīng)用,使用后減少了項目1
    發(fā)表于 01-02 17:08

    LLMWorld上線代碼翻譯新工具——問丫·碼語翻譯俠,快來體驗!

    應(yīng)用案例 aicode.llmworld.net 案例一 閱讀復(fù)雜算法,將計算機語言翻譯成自然語言和偽代碼,快速幫助用戶理解算法實現(xiàn)邏輯。
    的頭像 發(fā)表于 12-09 11:11 ?977次閱讀
    LLMWorld上線<b class='flag-5'>代碼</b>翻譯新工具——問丫·碼語翻譯俠,快來體驗!

    深入理解C語言:循環(huán)語句的應(yīng)用與優(yōu)化技巧

    在程序設(shè)計中,我們常常需要重復(fù)執(zhí)行某一段代碼。為了提高效率和簡化代碼,循環(huán)語句應(yīng)運而生。C語言作為一門經(jīng)典的編程
    的頭像 發(fā)表于 12-07 01:11 ?617次閱讀
    深入理解<b class='flag-5'>C</b><b class='flag-5'>語言</b>:循環(huán)語句的應(yīng)用與優(yōu)化技巧

    PLLATINUMSIM-SW是否有相關(guān)C語言代碼進行參考?

    PLLATINUMSIM-SW是否有相關(guān)C語言代碼進行參考,以達到對器件指標(biāo)的準(zhǔn)確評估直觀顯示。
    發(fā)表于 11-11 06:20

    程序員去面試只需一技能征服所有面試官!

    車輛工程專業(yè)的研究生去面試,面試官最后問他會不會嵌入式。雖然應(yīng)聘的崗位不是嵌入式工程師,但看來老板還是希望他能懂點這方面的知識。這個小插曲就說明了一重要的就業(yè)
    的頭像 發(fā)表于 11-05 19:35 ?543次閱讀
    程序員去<b class='flag-5'>面試</b>只需一<b class='flag-5'>個</b>技能征服所有<b class='flag-5'>面試</b>官!

    C語言與Java語言的對比

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

    TMS320LF240x DSP的C語言和匯編代碼快速入門

    電子發(fā)燒友網(wǎng)站提供《TMS320LF240x DSP的C語言和匯編代碼快速入門.pdf》資料免費下載
    發(fā)表于 10-18 10:14 ?1次下載
    TMS320LF240x DSP的<b class='flag-5'>C</b><b class='flag-5'>語言</b>和匯編<b class='flag-5'>代碼</b>快速入門

    hex文件如何查看原c語言代碼

    直接將 .hex 文件轉(zhuǎn)換回原始的 C 語言代碼是不可能的,因為 .hex 文件是二進制文件,它包含了單片機程序編譯后的機器碼,這些機器碼與原始的 C
    的頭像 發(fā)表于 09-02 10:37 ?4852次閱讀