你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C语言算法系列的第二篇

[复制链接]
gaosmile 发布时间:2020-10-22 22:48
算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手。本文是近百个C语言算法系列的第二篇,包括了经典的Fibonacci数列、简易计算器、回文检查、质数检查等算法。也许他们能在你的毕业设计或者面试中派上用场。

1、计算Fibonacci数列

Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21。

C语言实现的代码如下:

  1. /* Displaying Fibonacci sequence up to nth term where n is entered by user. */
  2. #include <stdio.h>
  3. int main()
  4. {
  5.   int count, n, t1=0, t2=1, display=0;
  6.   printf("Enter number of terms: ");
  7.   scanf("%d",&n);
  8.   printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
  9.   count=2;    /* count=2 because first two terms are already displayed. */
  10.   while (count<n)  
  11.   {
  12.       display=t1+t2;
  13.       t1=t2;
  14.       t2=display;
  15.       ++count;
  16.       printf("%d+",display);
  17.   }
  18.   return 0;
  19. }
复制代码

结果输出:

  1. Enter number of terms: 10
  2. Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+
复制代码

也可以使用下面的源代码:

  1. /* Displaying Fibonacci series up to certain number entered by user. */

  2. #include <stdio.h>
  3. int main()
  4. {
  5.   int t1=0, t2=1, display=0, num;
  6.   printf("Enter an integer: ");
  7.   scanf("%d",&num);
  8.   printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
  9.   display=t1+t2;
  10.   while(display<num)
  11.   {
  12.       printf("%d+",display);
  13.       t1=t2;
  14.       t2=display;
  15.       display=t1+t2;
  16.   }
  17.   return 0;
  18. }
复制代码

结果输出:

  1. Enter an integer: 200
  2. Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+55+89+144+
复制代码


2、回文检查

源代码:

  1. /* C program to check whether a number is palindrome or not */

  2. #include <stdio.h>
  3. int main()
  4. {
  5.   int n, reverse=0, rem,temp;
  6.   printf("Enter an integer: ");
  7.   scanf("%d", &n);
  8.   temp=n;
  9.   while(temp!=0)
  10.   {
  11.      rem=temp%10;
  12.      reverse=reverse*10+rem;
  13.      temp/=10;
  14.   }  
  15. /* Checking if number entered by user and it's reverse number is equal. */  
  16.   if(reverse==n)  
  17.       printf("%d is a palindrome.",n);
  18.   else
  19.       printf("%d is not a palindrome.",n);
  20.   return 0;
  21. }
复制代码

结果输出:

  1. Enter an integer: 12321
  2. 12321 is a palindrome.
复制代码


3、质数检查

注:1既不是质数也不是合数。

源代码:

  1. /* C program to check whether a number is prime or not. */

  2. #include <stdio.h>
  3. int main()
  4. {
  5.   int n, i, flag=0;
  6.   printf("Enter a positive integer: ");
  7.   scanf("%d",&n);
  8.   for(i=2;i<=n/2;++i)
  9.   {
  10.       if(n%i==0)
  11.       {
  12.           flag=1;
  13.           break;
  14.       }
  15.   }
  16.   if (flag==0)
  17.       printf("%d is a prime number.",n);
  18.   else
  19.       printf("%d is not a prime number.",n);
  20.   return 0;
  21. }
复制代码

结果输出:

  1. Enter a positive integer: 29
  2. 29 is a prime number.
复制代码

4、打印金字塔和三角形

使用 * 建立三角形

  1. *
  2. * *
  3. * * *
  4. * * * *
  5. * * * * *
复制代码

源代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i,j,rows;
  5.     printf("Enter the number of rows: ");
  6.     scanf("%d",&rows);//微信搜索公众号【C语言中文社区】关注回复C语言,免费领取200G学习资料
  7.     for(i=1;i<=rows;++i)
  8.     {
  9.         for(j=1;j<=i;++j)
  10.         {
  11.            printf("* ");
  12.         }
  13.         printf("\n");
  14.     }
  15.     return 0;
  16. }
复制代码

如下图所示使用数字打印半金字塔。

  1. 1
  2. 1 2
  3. 1 2 3
  4. 1 2 3 4
  5. 1 2 3 4 5
复制代码

源代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i,j,rows;
  5.     printf("Enter the number of rows: ");
  6.     scanf("%d",&rows);
  7.     for(i=1;i<=rows;++i)
  8.     {
  9.         for(j=1;j<=i;++j)
  10.         {
  11.            printf("%d ",j);
  12.         }
  13.         printf("\n");
  14.     }
  15.     return 0;
  16. }
复制代码

用 * 打印半金字塔

  1. * * * * *
  2. * * * *
  3. * * *
  4. * *
  5. *
复制代码

源代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i,j,rows;
  5.     printf("Enter the number of rows: ");
  6.     scanf("%d",&rows);
  7.     for(i=rows;i>=1;--i)
  8.     {
  9.         for(j=1;j<=i;++j)
  10.         {
  11.            printf("* ");
  12.         }
  13.     printf("\n");
  14.     }
  15.     return 0;
  16. }
复制代码

用 * 打印金字塔

  1.         *
  2.       * * *
  3.     * * * * *
  4.   * * * * * * *
  5. * * * * * * * * *
复制代码

源代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i,space,rows,k=0;
  5.     printf("Enter the number of rows: ");
  6.     scanf("%d",&rows);
  7.     for(i=1;i<=rows;++i)
  8.     {
  9.         for(space=1;space<=rows-i;++space)
  10.         {
  11.            printf("  ");
  12.         }
  13.         while(k!=2*i-1)
  14.         {
  15.            printf("* ");
  16.            ++k;
  17.         }
  18.         k=0;
  19.         printf("\n");
  20.     }
  21.     return 0;
  22. }
复制代码

用 * 打印倒金字塔

  1. * * * * * * * * *
  2.   * * * * * * *
  3.     * * * * *
  4.       * * *
  5.         *
复制代码

源代码:

  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int rows,i,j,space;
  5.     printf("Enter number of rows: ");
  6.     scanf("%d",&rows);
  7.     for(i=rows;i>=1;--i)
  8.     {
  9.         for(space=0;space<rows-i;++space)
  10.            printf("  ");
  11.         for(j=i;j<=2*i-1;++j)
  12.           printf("* ");
  13.         for(j=0;j<i-1;++j)
  14.             printf("* ");
  15.         printf("\n");
  16.     }
  17.     return 0;
  18. }
复制代码


5、简单的加减乘除计算器

源代码:

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

  2. # include <stdio.h>
  3. int main()
  4. {
  5.     char o;
  6.     float num1,num2;
  7.     printf("Enter operator either + or - or * or divide : ");
  8.     scanf("%c",&o);
  9.     printf("Enter two operands: ");
  10.     scanf("%f%f",&num1,&num2);
  11.     switch(o) {
  12.         case '+':
  13.             printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
  14.             break;
  15.         case '-':
  16.             printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
  17.             break;
  18.         case '*':
  19.             printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
  20.             break;
  21.         case '/':
  22.             printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
  23.             break;
  24.         default:
  25.             /* If operator is other than +, -, * or /, error message is shown */
  26.             printf("Error! operator is not correct");
  27.             break;
  28.     }
  29.     return 0;
  30. }
复制代码

结果输出:

  1. Enter operator either + or - or * or divide : -
  2. Enter two operands: 3.4
  3. 8.4
  4. 3.4 - 8.4 = -5.0
复制代码


6、检查一个数能不能表示成两个质数之和

源代码:

  1. #include <stdio.h>
  2. int prime(int n);
  3. int main()
  4. {
  5.     int n, i, flag=0;//微信搜索公众号【C语言中文社区】关注回复C语言,免费领取200G学习资料
  6.     printf("Enter a positive integer: ");
  7.     scanf("%d",&n);
  8.     for(i=2; i<=n/2; ++i)
  9.     {
  10.         if (prime(i)!=0)
  11.         {
  12.             if ( prime(n-i)!=0)
  13.             {
  14.                 printf("%d = %d + %d\n", n, i, n-i);
  15.                 flag=1;
  16.             }

  17.         }
  18.     }
  19.     if (flag==0)
  20.       printf("%d can't be expressed as sum of two prime numbers.",n);
  21.     return 0;
  22. }
  23. int prime(int n)      /* Function to check prime number */
  24. {
  25.     int i, flag=1;
  26.     for(i=2; i<=n/2; ++i)
  27.        if(n%i==0)
  28.           flag=0;
  29.     return flag;
  30. }
复制代码

结果输出:

  1. Enter a positive integer: 34
  2. 34 = 3 + 31
  3. 34 = 5 + 29
  4. 34 = 11 + 23
  5. 34 = 17 + 17
复制代码


7、用递归的方式颠倒字符串

源代码:

  1. /* Example to reverse a sentence entered by user without using strings. */

  2. #include <stdio.h>
  3. void Reverse();
  4. int main()
  5. {
  6.     printf("Enter a sentence: ");
  7.     Reverse();
  8.     return 0;
  9. }
  10. void Reverse()
  11. {
  12.     char c;
  13.     scanf("%c",&c);
  14.     if( c != '\n')
  15.     {
  16.         Reverse();
  17.         printf("%c",c);
  18.     }
  19. }
复制代码

结果输出:

  1. Enter a sentence: margorp emosewa
  2. awesome program
复制代码


8、实现二进制与十进制之间的相互转换

  1. /* C programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */

  2. #include <stdio.h>
  3. #include <math.h>
  4. int binary_decimal(int n);
  5. int decimal_binary(int n);
  6. int main()
  7. {
  8.    int n;
  9.    char c;
  10.    printf("Instructions:\n");
  11.    printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
  12.    printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
  13.    scanf("%c",&c);
  14.    if (c =='d' || c == 'D')
  15.    {
  16.        printf("Enter a binary number: ");
  17.        scanf("%d", &n);
  18.        printf("%d in binary = %d in decimal", n, binary_decimal(n));
  19.    }
  20.    if (c =='b' || c == 'B')
  21.    {
  22.        printf("Enter a decimal number: ");
  23.        scanf("%d", &n);
  24.        printf("%d in decimal = %d in binary", n, decimal_binary(n));
  25.    }
  26.    return 0;
  27. }

  28. int decimal_binary(int n)  /* Function to convert decimal to binary.*/
  29. {
  30.     int rem, i=1, binary=0;
  31.     while (n!=0)
  32.     {
  33.         rem=n%2;
  34.         n/=2;
  35.         binary+=rem*i;
  36.         i*=10;
  37.     }
  38.     return binary;
  39. }

  40. int binary_decimal(int n) /* Function to convert binary to decimal.*/
  41. {
  42.     int decimal=0, i=0, rem;
  43.     while (n!=0)
  44.     {
  45.         rem = n%10;
  46.         n/=10;
  47.         decimal += rem*pow(2,i);
  48.         ++i;
  49.     }
  50.     return decimal;
  51. }
复制代码

结果输出:

9、使用多维数组实现两个矩阵的相加

源代码:

  1. #include <stdio.h>
  2. int main(){
  3.     int r,c,a[100][100],b[100][100],sum[100][100],i,j;
  4.     printf("Enter number of rows (between 1 and 100): ");
  5.     scanf("%d",&r);
  6.     printf("Enter number of columns (between 1 and 100): ");
  7.     scanf("%d",&c);
  8.     printf("\nEnter elements of 1st matrix:\n");

  9. /* Storing elements of first matrix entered by user. */

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

  16. /* Storing elements of second matrix entered by user. */

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

  24. /*Adding Two matrices */

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

  28. /* Displaying the resultant sum matrix. */

  29.     printf("\nSum of two matrix is: \n\n");
  30.     for(i=0;i<r;++i)
  31.        for(j=0;j<c;++j)
  32.        {
  33.            printf("%d   ",sum[i][j]);
  34.            if(j==c-1)
  35.                printf("\n\n");
  36.        }

  37.     return 0;
  38. }
复制代码

结果输出:

10、矩阵转置

源代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int a[10][10], trans[10][10], r, c, i, j;
  5.     printf("Enter rows and column of matrix: ");
  6.     scanf("%d %d", &r, &c);

  7. /* Storing element of matrix entered by user in array a[][]. */
  8.     printf("\nEnter elements of matrix:\n");
  9.     for(i=0; i<r; ++i)
  10.     for(j=0; j<c; ++j)
  11.     {
  12.         printf("Enter elements a%d%d: ",i+1,j+1);
  13.         scanf("%d",&a[i][j]);
  14.     }
  15. /* Displaying the matrix a[][] */
  16.     printf("\nEntered Matrix: \n");
  17.     for(i=0; i<r; ++i)
  18.     for(j=0; j<c; ++j)
  19.     {
  20.         printf("%d  ",a[i][j]);
  21.         if(j==c-1)
  22.             printf("\n\n");
  23.     }

  24. /* Finding transpose of matrix a[][] and storing it in array trans[][]. */
  25.     for(i=0; i<r; ++i)
  26.     for(j=0; j<c; ++j)
  27.     {
  28.        trans[j][i]=a[i][j];
  29.     }

  30. /* Displaying the transpose,i.e, Displaying array trans[][]. */
  31.     printf("\nTranspose of Matrix:\n");
  32.     for(i=0; i<c; ++i)
  33.     for(j=0; j<r; ++j)
  34.     {
  35.         printf("%d  ",trans[i][j]);
  36.         if(j==r-1)
  37.             printf("\n\n");
  38.     }
  39.     return 0;
  40. }
复制代码

结果输出:
微信图片_20201022224749.jpg

收藏 评论0 发布时间:2020-10-22 22:48

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版