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

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

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

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

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

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

掃碼添加小助手

加入工程師交流群

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

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

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

五、簡單的加減乘除計(jì)算器

源代碼:

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

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

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

    關(guān)注

    23

    文章

    4784

    瀏覽量

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

    關(guān)注

    0

    文章

    83

    瀏覽量

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

    關(guān)注

    6

    文章

    90

    瀏覽量

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

    關(guān)注

    30

    文章

    4968

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

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

    單片機(jī)常用的14個(gè)C語言算法分享

    算法的描述:是對要解決一個(gè)問題或要完成一項(xiàng)任務(wù)所采取的方法和步驟的描述,包括需要什么數(shù)據(jù)(輸入什么數(shù)據(jù)、輸出什么結(jié)果)、采用什么結(jié)構(gòu)、使用什么語句以及如何安排這些語句等。通常使用自然語言、結(jié)構(gòu)化
    發(fā)表于 01-29 06:59

    講解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語言插入排序算法代碼

    插入排序是排序算法的一種,它不改變原有的序列(數(shù)組),而是創(chuàng)建一個(gè)新的序列,在新序列上進(jìn)行操作。   這里以從小到大排序?yàn)槔M(jìn)行講解。   基本思想及舉例說明   插入排序的基本思想
    發(fā)表于 01-15 06:44

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

    C語言增量式PID通用算法控制算法中PID使用的非常廣泛,但是在網(wǎng)上找代碼的時(shí)候發(fā)現(xiàn)好多代碼都不
    發(fā)表于 01-14 08:28

    c語言中的代碼優(yōu)化

    能夠提升算法效率。由于乘除運(yùn)算指令周期一般比移位運(yùn)算大。C語言位運(yùn)算除了能夠提升運(yùn)算效率外,在嵌入式系統(tǒng)的編程中,它的另外一 個(gè)最典型的應(yīng)用,并且十分普遍地正在被使用著的是位間的與(
    發(fā)表于 01-12 09:45

    C語言經(jīng)典問題及解析

    (\"> 6\") : puts(\"<= 6\"); } 考察點(diǎn): 這 個(gè)問題測試你是否懂得C語言中的整數(shù)自動(dòng)轉(zhuǎn)換原則,我發(fā)現(xiàn)有
    發(fā)表于 12-23 07:52

    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語言編程?

    的存儲(chǔ)空間非常有限,我們使用者需要靠精打細(xì)算來設(shè)計(jì)程序,根本經(jīng)不起高級語言臃腫的代碼體積。高級語言也無法實(shí)現(xiàn)精確的時(shí)序控制。 三、C語言
    發(fā)表于 11-28 07:37

    C語言的常見算法

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

    C語言特性

    1、高效性:直接操作硬件 C 語言代碼的執(zhí)行效率極高,這是其最為顯著的優(yōu)勢之一。它能夠直接訪問硬件資源,與底層硬件進(jìn)行緊密交互,充分發(fā)揮硬件的性能潛力。在嵌入式開發(fā)中,硬件資源往往十分有限,對程序
    發(fā)表于 11-24 07:01

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

    代碼編譯為 HDL 的過程一共分為兩步: (1)C to HASM (2)HASM to HDL 第一步 C to HASM 是將 C 語言
    發(fā)表于 10-30 07:02

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

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

    必看!15個(gè)C語言常見陷阱及避坑指南

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

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

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