10個經典的C語言面試基礎算法及代碼

2021-02-19 單片機與嵌入式

算法是一個程序和軟體的靈魂,作為一名優秀的程式設計師,只有對一些基礎的算法有著全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。本文包括了經典的Fibonacci數列、簡易計算器、回文檢查、質數檢查等算法。

1、計算Fibonacci數列

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

C語言實現的代碼如下:

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 <stdio.h>int main(){ int t1=0, t2=1, display=0, num; printf("Enter an integer: "); scanf("%d",&num); printf("Fibonacci Series: %d+%d+", t1, t2); display=t1+t2; while(display<num) { printf("%d+",display); t1=t2; t2=display; display=t1+t2; } return 0;}

結果輸出:

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 <stdio.h>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; } 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 <stdio.h>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 <stdio.h>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("\n");    }    return 0;}

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

11 21 2 31 2 3 41 2 3 4 5

原始碼:

#include <stdio.h>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("\n");    }    return 0;}

用 * 列印半金字塔

* * * * ** * * ** * * * **

原始碼:

#include <stdio.h>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("\n");    }    return 0;}

用 * 列印金字塔

   

        *      * * *    * * * * *  * * * * * * ** * * * * * * * *

原始碼:

#include <stdio.h>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("\n");    }    return 0;}

用 * 列印倒金字塔

* * * * * * * * *  * * * * * * *    * * * * *      * * *        *

原始碼:

#include<stdio.h>int main(){    int rows,i,j,space;    printf("Enter number of rows: ");    scanf("%d",&rows);    for(i=rows;i>=1;--i)    {        for(space=0;space<rows-i;++space)           printf("  ");        for(j=i;j<=2*i-1;++j)          printf("* ");        for(j=0;j<i-1;++j)            printf("* ");        printf("\n");    }    return 0;}

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

原始碼:

/* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */

# include <stdio.h>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: 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 <stdio.h>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", 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) { 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 <stdio.h>void Reverse();int main(){ printf("Enter a sentence: "); Reverse(); return 0;}void Reverse(){ char c; scanf("%c",&c); if( c != '\n') { 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 <stdio.h>#include <math.h>int binary_decimal(int n);int decimal_binary(int n);int main(){ int n; char c; printf("Instructions:\n"); printf("1. Enter alphabet 'd' to convert binary to decimal.\n"); printf("2. Enter alphabet 'b' to convert decimal to binary.\n"); 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) { 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) { int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal;}

結果輸出:


9、使用多維數組實現兩個矩陣的相

原始碼:

#include <stdio.h>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("\nEnter elements of 1st matrix:\n");



for(i=0;i<r;++i) for(j=0;j<c;++j) { printf("Enter element a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); }



printf("Enter elements of 2nd matrix:\n"); for(i=0;i<r;++i) for(j=0;j<c;++j) { printf("Enter element a%d%d: ",i+1,j+1); scanf("%d",&b[i][j]); }



for(i=0;i<r;++i) for(j=0;j<c;++j) sum[i][j]=a[i][j]+b[i][j];



printf("\nSum of two matrix is: \n\n"); for(i=0;i<r;++i) for(j=0;j<c;++j) { printf("%d ",sum[i][j]); if(j==c-1) printf("\n\n"); }

return 0;}

結果輸出:


10、矩陣轉置

原始碼:

#include <stdio.h>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);

printf("\nEnter elements of matrix:\n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("Enter elements a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } printf("\nEntered Matrix: \n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("%d ",a[i][j]); if(j==c-1) printf("\n\n"); }

for(i=0; i<r; ++i) for(j=0; j<c; ++j) { trans[j][i]=a[i][j]; }

printf("\nTranspose of Matrix:\n"); for(i=0; i<c; ++i) for(j=0; j<r; ++j) { printf("%d ",trans[i][j]); if(j==r-1) printf("\n\n"); } return 0;}

結果輸出:

相關焦點

  • 必備的10大C語言基礎算法(附完整代碼)
    算法是一個程序和軟體的靈魂,作為一名優秀的程式設計師,只有對一些基礎的算法有著全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。
  • 經常遇到的10大C語言基礎算法(珍藏版源碼)
    算法是一個程序和軟體的靈魂,作為一名優秀的程式設計師,只有對一些基礎的算法有著全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。本文是近百個C語言算法系列的第二篇,包括了經典的Fibonacci數列、簡易計算器、回文檢查、質數檢查等算法。也許他們能在你的畢業設計或者面試中派上用場。1、計算Fibonacci數列Fibonacci數列又稱斐波那契數列,又稱黃金分割數列,指的是這樣一個數列:1、1、2、3、5、8、13、21。
  • C語言中10個經典的算法,學會它,利用它
    C語言中有有許多經典的算法,這些算法都是許多人的智慧結晶,也是編程中常用的算法,這裡面包含了眾多算法思想,掌握這些算法,對於學習更高級的、更難的算法都會有很大的幫助
  • 在C語言中,核心是指針,靈魂是算法,本篇用源碼解析十大基礎算法原理!
    算法是一個程序和軟體的靈魂,作為一名優秀的程式設計師,只有對一些基礎的算法有著全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。本文是近百個C語言算法系列的第二篇,包括了經典的Fibonacci數列、簡易計算器、回文檢查、質數檢查等算法。也許他們能在你的畢業設計或者面試中派上用場。
  • 10個經典的C語言小程序
    今天給大家分享10個比較基礎的C語言的小程序,希望給C語言初學者帶來一定幫助。
  • C 語言經典算法
    C語言作為多數工科學校編程入門的教學工具語言,是很多朋友開啟編程世界大門的鑰匙。
  • 10個Objective-C基礎面試題,iOS面試必備
    如果你準備去面試一個iOS開發崗位,那麼本文也許可以幫助你提前準備一些iOS面試題,這些面試題都是 Objective-C基礎面試題,一起來看看。1、#import和#include的區別,@class代表什麼?
  • 10 個經典的 C 語言小程序
    今天給大家分享10個比較基礎的C語言的小程序,希望給C語言初學者帶來一定幫助。1、題目:有1、2、3、4個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?程序分析:可填在百位、十位、個位的數字都是1、2、3、4。組成所有的排列後再去掉不滿足條件的排列。
  • C語言面試54題
    C語言面試54題大家好,這期呢,我們談一下c語言的面試題。
  • 學習C語言必看的最經典書籍
    來源:互動出版網學習c語言必看的最經典書籍推薦一:《新概念51單片機C語言教程--入門
  • 算法工程師面試問題及資料超詳細合集(多家公司算法崗面經/代碼實戰/網課/競賽等)
    阿里巴巴計算機視覺算法實習生視頻面試 website面試經驗AI算法工程師(面試官角度) website從零基礎到BAT算法崗SP——秋招準備攻略 website螞蟻金服/曠視/虹軟/騰訊優圖暑期實習offer面經 website我在美團的這兩年(附校招筆試/面試/面經分享) website1000 面試題,BAT
  • 推薦4個基於 Java語言的開源 Leetcode 題解!算法面試不愁了!
    2、☞ 《Java面試手冊》.PDF    點擊查看一個很明顯的現象,現在大廠的應屆生面試,甚至是社招面試都開始越來越重視算法了。經常會有人問 Guide 如何準備算法面試,今天統一回答一下。1.CS-Notes[1]這個開源項目不是單一關注算法的倉庫,它是一個大的集合,包括了技術面試必備基礎知識、Leetcode、計算機作業系統、計算機網絡、系統設計等知識。我和這個開源項目的原作者有過交流,是一名很優秀的 coder。
  • 【我是程式設計師】C語言的學習基礎,100個經典的算法(一)
    程序分析:利用for循環控制100-999個數,每個數分解出個位,十位,百位。  i=n/100;    j=n/10%10;    k=n%10;    if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)     {     printf("%-5d",n);     }
  • C語言入門級教程:基礎數據類型與基本算法,學編程從此刻開始!
    今天帶大家了解一下學C語言必備的基本數據類型和基本算法,適合剛學C以及零基礎的小夥伴! 話不多說,我們一起來學習吧~ 數據類型 ● 基本類型 基本類型就是我們在使用C語言時最基礎的數據類型,包括整形(短整型,基本整型,長整型)、字符型、浮點型(單、雙精度)以及枚舉類型。
  • 機器學習算法基礎(使用Python代碼)
    我提供了對各種機器學習算法的高級理解以及運行它們的R&Python代碼。這些應該足以弄髒你的手。線性回歸主要有兩種類型:簡單線性回歸和多元線性回歸。簡單線性回歸的特徵在於一個自變量。而多元線性回歸(顧名思義)的特徵是多個(超過1個)的自變量。在找到最佳擬合線時,可以擬合多項式或曲線回歸。這些被稱為多項式或曲線回歸。
  • C語言八大排序算法,附動圖和詳細代碼解釋!
    重複2.3步驟,直至堆中只有1個元素為止下面是基於大頂堆的堆排序算法代碼:void print(int a[], int n){ for(int j= 0; j<n; j++){ cout<<a[j] <<" "; } cout<<endl;}/*
  • C語言學習推薦書籍
    題圖:來自網絡關於C關於C編程,我覺得有下面3個層次:基礎 - 基本語法進階 - 避免常見錯誤
  • c語言50本電子書
    495個C語言問題.pdfC Primer Plus 第6版 中文版 高清完整PDF版.pdf
  • 寫出高效優美的單片機C語言代碼
    程序能跑起來並不見得你的代碼就是很好的c代碼了,衡量代碼的好壞應該從以下幾個方面來看1,代碼穩定,沒有隱患。下面發一些我在網上看到的技巧和自己的一些經驗來和大家分享;1、如果可以的話少用庫函數,便於不同的mcu和編譯器間的移植2、選擇合適的算法和數據結構應該熟悉算法語言,知道各種算法的優缺點,具體資料請參見相應的參考資料,有很多計算機書籍上都有介紹。
  • 憑藉清華掃地僧的路線指引,從Java基礎到算法,吊打阿里面試官!
    字節跳動面試總共是3+1面試(技術3面+HR1面),三面技術具體問了什麼題目我是有點分不清了,不過我記得每個知識點大概問了那些問題,大致就是分為Java架構基礎+Redis+Linux/作業系統+HTTPS+MySQL資料庫+算法這六個部分吧,不過話說回來,這次之所以能僥倖過關,多虧了清華掃地僧大佬的學習路線,自己也下了大功夫,也算是功夫不負有心人吧~~~