算法学习之C语言基础
2024-10-10 23:36:34
算法学习,先熟悉一下C语言哈!!!
#include <conio.h>
#include<stdio.h>
int main(){
printf("%d\n",+);
getch();
return ;
}
计算1+2的值
结果:3
进一步计算加减乘除
#include <conio.h>
#include<stdio.h>
int main(){
printf("%d\n",+);
printf("%d\n",-);
printf("%d\n",*);
printf("%d\n",/);
printf("%d\n",/);
getch();
return ;
}
结果:
3
-1
30
2
1
tips:计算发现8除以5得到的不是1.6而是1,为什么呢?
如果才能得到1.6呢?
#include <conio.h>
#include<stdio.h>
int main(){
printf("%.1lf\n",8.0/5.0);//第二个是字母l
getch();
return ;
}
结果:
1.6
进一步实验
#include <conio.h>
#include<stdio.h>
int main(){
printf("%.2lf\n",8.0/5.0);//2表示小数点后的尾数保留
printf("%.1lf\n",8.0/5.0);//1表示只保留一位
printf("%.lf\n",8.0/5.0);//没有则表示保留0位,四舍五入
printf("%.1lf\n",/);//不明
printf("%d\n",8.0/5.0);//不明
getch();
return ;
}
结果:
1.60
1.6
2
1.6
-1717986918
tips:整数用%d输出,实数用%lf输出,整数与整数运算结果也是整数对于整数运算。
8.0和5.0称为实数,更专业一点叫“浮点数”。浮点数之间的运算也是浮点数。
额,我表示还是有点晕乎乎的!!!
再来看一看更复杂一点的算术表达式。
#include <conio.h>
#include<stdio.h>
#include<math.h>
int main(){
printf("%.8lf\n",+*sqrt()/(-0.75));//sqrt必须引入math.h头文件
getch();
return ;
}
结果:17.00000000
tips:整数-浮点数=浮点数
确切的说是整数先变成浮点数,然后浮点数-浮点数=浮点数
变量及其输入
下面是可以处理输入两个数的代码
#include <conio.h>
#include<stdio.h>
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
getch();
return ;
}
tips:scanf中的占位符合变量的数据类型一一对应,且每个变量前需要&符号。
计算圆柱体的表面积
要求:输入底面半径r和高h,输出圆柱体的表面积,保留3位小数。
样例输入:3.5 9
样例输出:Area = 274.889
分析:圆柱体表面积由3部分组成:上底面积、下底面积和侧面积。
完成的公式可写成:表面积=底面积*2+侧面积
#include <conio.h>
#include<stdio.h>
#include<math.h>
int main(){
const double pi = 4.0*atan(1.0); //pi的巧妙获取
double r,h,s1,s2,s; //声明变量
scanf("%lf%lf",&r,&h); //获取输入值
s1 = pi*r*r; //计算底面积
s2 = *pi*r*h; //计算侧面积
s = s1*2.0 + s2; //得到总面积
printf("Area = %.3lf\n",s); //输出结果
getch();
return ;
}
最新文章
- 首师大附中科创教育平台 我的刷题记录 0304 50095106扔核弹(XDC,你懂的)
- PHPCMS v9 超级安全防范教程!
- [leetcode 35] Search Insert Position
- user database的initial size和dbcc shrinkfile
- JDBC Connection
- HDU-1053-Entropy(Huffman编码)
- 可用与禁用 E:enabled { sRules }
- Git merge local repository
- Android-Launcher开发之ShortCut(1)
- 免费V P N获取方式
- php中urldecode()和urlencode()起什么作用啊
- DirectX11--实现一个3D魔方(2)
- Spring Boot 监控利器 —— Actutor
- mysql实用函数
- VHDL 例程
- Java学习第二篇 — 时间类的使用
- 【Spring Boot&;&;Spring Cloud系列】Spring Boot中使用数据库之MySql
- vue-cli 项目构建性能分析工具
- php内存回收机制的学习
- 数据库sharding系列好文收藏