C语言的10大基础算法
对于一个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学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
使用Golang的Context管理上下文的方法
- 上一篇
- 使用Golang的Context管理上下文的方法
- 下一篇
- golang websocket 服务端的实现
-
- 糟糕的唇彩
- 这篇技术贴出现的刚刚好,好细啊,很有用,已收藏,关注博主了!希望博主能多写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
-
- Golang · Go教程 | 39分钟前 |
- Go html/template 与 text/template 怎么选择输出类型
- 112浏览 收藏
-
- Golang · Go教程 | 50分钟前 | go · 模板 · html/template · Go html/template HTML转义 模板安全
- Go 模板输出可信 HTML 时为什么不能直接拼字符串
- 388浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- Go html/template 怎么避免用户输入被当成 HTML 执行
- 314浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- Go 模板里访问嵌套字段失败时怎么定位字段路径
- 486浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- Go 模板执行后输出空字符串怎么区分零值和缺失字段
- 204浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- Go text/template 缺少 map 键时怎么让它直接报错
- 102浏览 收藏
-
- Golang · Go教程 | 1小时前 | 正则表达式 · Go教程 · 日志解析 · Go regexp 日志解析 SubexpIndex FindStringSubmatch
- Go 正则匹配日志字段时怎么把子匹配映射成结构体
- 436浏览 收藏
-
- Golang · Go教程 | 2小时前 | 错误排查 · 字符串匹配 · Go正则表达式 · Go regexp 命名捕获组 FindStringSubmatch
- Go 正则 FindStringSubmatch 返回空切片时怎么判断原因
- 207浏览 收藏
-
- Golang · Go教程 | 2小时前 | go · 正则表达式 · regexp · 字符串解析 · Go regexp 命名捕获组 SubexpIndex SubexpNames
- Go regexp 怎么用命名捕获组解析可选字段
- 252浏览 收藏
-
- Golang · Go教程 | 2小时前 |
- Go URL 拼接时怎么避免双斜杠和转义重复
- 149浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 9次使用
-
- SuperCLUE
- SuperCLUE是权威的中文大语言模型综合评测基准,涵盖语言理解、知识应用、AI Agent智能体及安全性等12项核心能力。通过多轮对话与客观测试,定期发布榜单与技术报告,为模型研发、优化及行业选型提供科学依据。
- 174次使用
-
- C-Eval
- 深入了解C-Eval中文评估套件,涵盖52个学科与4级难度。本文详解其功能特点、Zero-shot/Few-shot使用方法及代码示例,助您全面评测LLM中文理解与泛化能力。
- 106次使用
-
- AI Prompt Library
- 探索AI Prompt Library免费资源库,涵盖营销、写作及多场景AI提示词。兼容ChatGPT、Claude等工具,一键复制优化输出,提升工作效率。
- 34次使用
-
- Generrated
- Generrated汇集9300+张DALL·E生成图像及对应提示词,支持查看完整图集、对比DALL·E 2与3版本差异,是AI绘图新手学习Prompt设计与获取创作灵感的实用工具。
- 3次使用
-
- Java 性能优化上线清单:从定位、改造到灰度发布
- 2026-06-11 860浏览
-
- Spring Boot 压测验证:Gatling、JMeter 与性能回归门禁
- 2026-06-11 843浏览
-
- Java NMT 非堆内存排查:Direct Buffer、线程栈与 Metaspace 分析
- 2026-06-11 826浏览
-
- Spring Boot 容器内存优化:JVM 堆、非堆与 MaxRAMPercentage
- 2026-06-11 809浏览
-
- Tomcat 连接与线程参数调优:maxThreads、acceptCount 与 KeepAlive
- 2026-06-11 792浏览

