女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

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

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

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

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

黃工的嵌入式技術圈 ? 來源:黃工的嵌入式技術圈 ? 作者:黃工的嵌入式技術 ? 2020-01-16 11:09 ? 次閱讀

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

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

一、計算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;}

結果輸出:

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;} 結果輸出: Enter an integer: 1232112321 is a palindrome.

三、質數(shù)檢查

注:1既不是質數(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;}

結果輸出:

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

結果輸出:

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

六、檢查一個數(shù)能不能表示成兩個質數(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;}

結果輸出:

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

結果輸出:

Enter a sentence: margorp emosewaawesome program

八、實現(xià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;}

結果輸出:

九、使用多維數(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;}

結果輸出:

十、矩陣轉置

源代碼:

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

結果輸出:

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

    關注

    23

    文章

    4695

    瀏覽量

    94608
  • 基礎知識
    +關注

    關注

    0

    文章

    81

    瀏覽量

    26765
  • C語言編程
    +關注

    關注

    6

    文章

    90

    瀏覽量

    21455
  • 代碼
    +關注

    關注

    30

    文章

    4886

    瀏覽量

    70203
收藏 人收藏

    評論

    相關推薦
    熱點推薦

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

    C語言編程中,循環(huán)結構是至關重要的,它可以讓程序重復執(zhí)行特定的代碼塊,從而提高編程效率。然而,為了避免程序進入無限循環(huán),C語言提供了多種循
    的頭像 發(fā)表于 04-29 18:49 ?697次閱讀
    深入理解<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語言雖強大,但隱藏的“坑”也不少!稍不留神就會導致程序崩潰、數(shù)據(jù)異常。本文整理15高頻陷阱,助你寫出更穩(wěn)健的代碼! ? 陷阱1:運算符優(yōu)先級混淆? 問題:運算符優(yōu)先級不同可能導
    的頭像 發(fā)表于 03-16 12:10 ?395次閱讀

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

    全套C語言培訓資料,共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算法原理

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

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

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

    分析C語言代碼結構的設計問題

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

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

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

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

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

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

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

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

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

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

    機器學習的經(jīng)典算法與應用

    關于數(shù)據(jù)機器學習就是喂入算法和數(shù)據(jù),讓算法從數(shù)據(jù)中尋找一種相應的關系。Iris鳶尾花數(shù)據(jù)集是一經(jīng)典數(shù)據(jù)集,在統(tǒng)計學習和機器學習領域都經(jīng)常被用作示例。數(shù)據(jù)集內(nèi)包含3類共150條記錄,每
    的頭像 發(fā)表于 06-27 08:27 ?1923次閱讀
    機器學習的<b class='flag-5'>經(jīng)典</b><b class='flag-5'>算法</b>與應用

    如何用C語言實現(xiàn)高效查找(二分法)

    今天給分享一下使用C語言實現(xiàn)二分算法,主要包含以下幾部分內(nèi)容:二分查找算法介紹二分查找算法使用場景二分查找
    的頭像 發(fā)表于 06-04 08:04 ?1616次閱讀
    如何用<b class='flag-5'>C</b><b class='flag-5'>語言</b>實現(xiàn)高效查找(二分法)