C語言的10大基礎算法
算法是一個程序和軟件的靈魂,作為一名優秀的程序員,只有對一些基礎的算法有着全面的掌握,才會在設計程序和編寫代碼的過程中顯得得心應手。本文包括了經典的Fibonacci數列、簡易計算器、回文檢查、質數檢查等算法。也許它們能在你的畢業設計或者面試中派上用場。
1、計算Fibonacci數列
Fibonacci數列又稱斐波那契數列,又稱黃金分割數列,指的是這樣一個數列:1、1、2、3、5、8、13、21。
C語言實現的代碼如下:
/* 顯示用戶輸入n的fibonacci序列,直至n項. */
#include <stdio.h>
int main()
{
int count, n, t1=0, t2=1, display=0;
printf("Enter number of terms: ");
scanf_s("%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<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;
printf("%d+",display);
}
return 0;
}
結果輸出:

Enter number of terms: 10
Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+
也可以使用下面的源代碼:
/* 顯示用戶輸入的一定值的斐波那契級數. */
#include <stdio.h>
int main()
{
int t1=0, t2=1, display=0, num;
printf("Enter an integer: ");
scanf_s("%d",&num);
printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
display=t1+t2;
while(display<num)
{
printf("%d+",display);
t1=t2;
t2=display;
display=t1+t2;
}
return 0;
}
結果輸出:

Enter an integer: 200
Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+55+89+144+
/*輸入n,顯示第n個斐波那契數值,並輸出前n項之和.*/
源代碼:
#include<stdlib.h>
#include<string>
#include<math.h>
#include<iostream>
using namespace std;
int fib(int x);
int main()
{
int n, sum = 0;
int ret = 0;
printf("輸入第n項:");
scanf_s("%d", &n);
printf("Fibonacci數列的第%d項為:%d\n", n, fib(n));
for (int i = 1; i <= n; i++)
{
ret = fib(i);
sum += ret;
}
printf("前%d項之和= %d\n", n, sum);
return 0;
}
int fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
return (fib(n - 1) + fib(n - 2));
}
輸出結果:

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_s("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
/* 檢查數字是否為回文的C程序. */
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}
結果輸出:

Enter an integer: 12321
12321 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_s("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d 是質數.",n);
else
printf("%d 不是質數.",n);
return 0;
}
結果輸出:

Enter a positive integer: 29
29 是質數.
4、打印金字塔和三角形
使用 * 建立三角形
*
* *
* * *
* * * *
* * * * *
源代碼:
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf_s("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
如下圖所示使用數字打印半金字塔。
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
源代碼:
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf_s("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
用 * 打印半金字塔
* * * * *
* * * *
* * *
* *
*
源代碼:
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf_s("%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_s("%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_s("%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_s("%c",&o);
printf("Enter two operands: ");
scanf_s("%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.4
8.4
3.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_s("%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) /* 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: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
7、用遞歸的方式顛倒字符串
源代碼:
/* 不使用字符串反轉用戶輸入的句子的示例. */
#include <stdio.h>
void Reverse();
int main()
{
printf("Enter a sentence: ");
Reverse();
return 0;
}
void Reverse()
{
char c;
scanf_s("%c",&c);
if( c != '\n')
{
Reverse();
printf("%c",c);
}
}
結果輸出:
Enter a sentence: margorp emosewa
awesome program
8、實現二進制與十進制之間的相互轉換
/* c根據用戶輸入的數據將二進制數轉換成十進制數或十進制數轉換成二進制數的程序源代碼. */
#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. 輸入字母'd'將二進制轉換為十進制.\n");
printf("2. 輸入字母'b'將十進制轉換為二進制.\n");
scanf_s("%c",&c);
if (c =='d' || c == 'D')
{
printf("輸入二進制數: ");
scanf_s("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c == 'B')
{
printf("輸入一個十進制數字: ");
scanf_s("%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_s("%d",&r);
printf("Enter number of columns (between 1 and 100): ");
scanf_s("%d",&c);
printf("\n輸入第一個矩陣中的元素:\n");
/* Storing elements of first matrix entered by user. */
<font color=red size=4><marquee direction=right color=red>希望對大家有所幫助啊!</marquee></font>
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf_s("%d",&a[i][j]);
}
/* Storing elements of second matrix entered by user. */
printf("輸入第二個矩陣的元素:\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf_s("%d",&b[i][j]);
}
/*Adding Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
/* Displaying the resultant sum matrix. */
printf("\n兩個矩陣之和為: \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("輸入矩陣的行和列: ");
scanf_s("%d %d", &r, &c);
/* 將用戶輸入的矩陣元素存儲在數組A[]中。*/
printf("\n輸入矩陣元素:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf_s("%d",&a[i][j]);
}
/* 顯示矩陣 a[][] */
printf("\n輸入矩陣: \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");
}
/* 求矩陣a[][]的轉置並存儲在數組trans[][]中. */
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j];
}
/* 顯示轉置,即顯示數組trans[][]. */
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;
}
輸出結果:

————————————————
讓你的指尖跳躍出一行行代碼精靈,希望對你有所幫助。
本文博主:翻斗街扛把子胡圖圖
本文出處:https://www.cnblogs.com/dxg123/p/11458404.html
本文版權歸作者和博客園共有,歡迎轉載,轉載請附上原文出處鏈接及本聲明。
