C语言的10大基础算法
来源:脚本之家
时间:2023-01-07 12:12:03 388浏览 收藏
对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《C语言的10大基础算法》,主要介绍了c语言算法,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!
算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手。本文是近百个C语言算法系列的第二篇,包括了经典的Fibonacci数列、简易计算器、回文检查、质数检查等算法。也许他们能在你的毕业设计或者面试中派上用场。
1、计算Fibonacci数列
Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21。
C语言实现的代码如下:
/* Displaying Fibonacci sequence up to nth term where n is entered by user. */ #includeint 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 结果输出:
Enter number of terms: 10 Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+也可以使用下面的源代码:
/* Displaying Fibonacci series up to certain number entered by user. */ #includeint main() { int t1=0, t2=1, display=0, num; printf("Enter an integer: "); scanf("%d",&num); printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */ display=t1+t2; while(display 结果输出:
Enter an integer: 200
Fibonacci 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 */ #includeint 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: 12321
12321 is a palindrome.3、质数检查
注:1既不是质数也不是合数。
源代码:
/* C program to check whether a number is prime or not. */ #includeint main() { int n, i, flag=0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=2;i 结果输出:
Enter a positive integer: 29
29 is a prime number.4、打印金字塔和三角形
使用 * 建立三角形
*
* *
* * *
* * * *
* * * * *源代码:
#includeint main() { int i,j,rows; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=1;i 如下图所示使用数字打印半金字塔。
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5源代码:
#includeint main() { int i,j,rows; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=1;i 用 * 打印半金字塔
* * * * *
* * * *
* * *
* *
*源代码:
#includeint main() { int i,j,rows; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=rows;i>=1;--i) { for(j=1;j 用 * 打印金字塔
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *源代码:
#includeint main() { int i,space,rows,k=0; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=1;i 用 * 打印倒金字塔
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*源代码:
#includeint main() { int rows,i,j,space; printf("Enter number of rows: "); scanf("%d",&rows); for(i=rows;i>=1;--i) { for(space=0;space 5、简单的加减乘除计算器
源代码:
/* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */ # includeint 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.4
8.4
3.4 - 8.4 = -5.06、检查一个数能不能表示成两个质数之和
源代码:
#includeint prime(int n); int main() { int n, i, flag=0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=2; i 结果输出:
Enter a positive integer: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 177、用递归的方式颠倒字符串
源代码:
/* Example to reverse a sentence entered by user without using strings. */ #includevoid 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 emosewa
awesome program8、实现二进制与十进制之间的相互转换
/* 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:\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) /* Function to convert decimal to binary.*/ { 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) /* Function to convert binary to decimal.*/ { int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal; } 结果输出:
9、使用多维数组实现两个矩阵的相加
源代码:
#includeint 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"); /* Storing elements of first matrix entered by user. */ for(i=0;i 10、矩阵转置
源代码:
#includeint 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("\nEnter elements of matrix:\n"); for(i=0; i 总结
以上所述是小编给大家介绍的C语言的10大基础算法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对golang学习网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!今天关于《C语言的10大基础算法》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
505 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
324 收藏
-
125 收藏
-
381 收藏
-
486 收藏
-
276 收藏
-
419 收藏
-
201 收藏
-
432 收藏
-
208 收藏
-
102 收藏
-
362 收藏
-
271 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习
-
- 糟糕的唇彩
- 这篇技术贴出现的刚刚好,好细啊,很有用,已收藏,关注博主了!希望博主能多写Golang相关的文章。
- 2023-04-09 20:14:44
-
- 称心的台灯
- 受益颇多,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢up主分享文章!
- 2023-03-05 01:59:36
-
- 碧蓝的百合
- 这篇技术贴太及时了,太细致了,很棒,已收藏,关注大佬了!希望大佬能多写Golang相关的文章。
- 2023-02-07 19:16:58
-
- 纯真的飞鸟
- 受益颇多,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢作者大大分享博文!
- 2023-02-07 11:16:51
-
- 花痴的花瓣
- 很有用,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢大佬分享文章!
- 2023-01-28 15:04:34
-
- 腼腆的月饼
- 太详细了,mark,感谢师傅的这篇博文,我会继续支持!
- 2023-01-26 19:25:06
-
- 阔达的路人
- 这篇技术贴真及时,太全面了,很有用,码起来,关注大佬了!希望大佬能多写Golang相关的文章。
- 2023-01-14 07:08:33