当前位置:网站首页>四舍五入,向下取整,向上取整的使用
四舍五入,向下取整,向上取整的使用
2022-07-13 17:54:00 【含若飞】
C语言中的四舍五入以及向下取整和向上取整
这是一道常见的收话费问题
思路很简单,首先就是判断它打电话的时间是否大于3分钟,也就是先判断他的收费是否大于0.5,若是大于0.5,则对多出的部分除以0.2。
在这里主要是想说两个函数,floor和ceil,使用它们是需要引入头文件<math.h>
1.floor
floor函数是取小于或等于它的最大整数
使用方法 double floor(x);默认类型为double,有六位小数,但也可以转化为低精度的数值
如int i= floor(x);
但是由高精度转为低精度时会损失精度。
2.ceil
即它会返回一个大于或等于它的最小整数,
使用方法 double ceil(x);
也是double类型的,默认有6位小数,也可以对其进行强制转换
int i = ceil(x);
在我们解决上面这个例题时就可以使用,因为它所要求的通话时长都为整数。
3.round
返回类型跟上面两个函数一样,也为double类型的
使用方法 double round(x);
但在使用时我们也可以对其进行转换
int i = round(x);
但会损失精度。
这在小学我们就讲过什么时候使用进一(ceil),什么时候使用退一(floor),什么时候使用四舍五入(round),大家自行判断。
如有不正确的地方,欢迎随时指出来。
#include<stdio.h>
#include<math.h>
int main(void)
{
float i,a;
int b,c=3;
scanf("%f",&i);
if(i==0.5)
{
c=3;
}
else
{
a=3+(i-0.5)/0.2;
c=ceil(a);
}
printf("%d",c);
return 0;
}
例题的代码,供参考。
边栏推荐
- General commands of MATLAB
- 01-kNN
- Embedded software development stm32f407 buzzer standard library version
- Virtual memory location structure (reserved area, code area, stack area, heap area, literal constant area) and variable modifiers (const, auto, static, register, volatile, extern)
- 快速入门ElasticSercher
- Train yolov3 on colab (I)
- Go language websocket library gorilla websocket
- Spark入门
- 如何使用Keil5中的虛擬示波器進行軟件仿真
- stm32学习(入门)
猜你喜欢
![[Go语言入门] 05 Go语言分支语句](/img/cb/9830418c0e14b40fb15cfb68966426.png)
[Go语言入门] 05 Go语言分支语句

OpenGL 3D graphics development notes, terrain, lighting, shadows, etc

Typescript basic configuration tutorial (automatically compiled in vscode)

Excel-1

vscode插件安装介绍
![[Go语言入门] 13 Go语言接口(interface)详解](/img/38/7576257ecc706251e23b8a5ec2d98e.png)
[Go语言入门] 13 Go语言接口(interface)详解

在Colab上训练yolov3(一)

Blue Bridge Cup embedded Hal library new project

01-kNN
![[Verilog] [vivado] counter example](/img/3b/1503717fe5a19b114ce687808cc21d.png)
[Verilog] [vivado] counter example
随机推荐
Customize and modify the width and height of the van button in the van weap component library
Notepad++ open bin file
001 null pointer and wild pointer
使用Idea IntelliJ查看字节码文件
Dynamic memory allocation principle of C language and the use of heap (malloc, calloc, realloc, free)
go语言websocket库Gorilla Websocket
pyopencv基础操作指南
快速入门ElasticSercher
[Go语言入门] 03 Go语言数据类型、变量、常量
ROS 通信机制
[matlab] matlab lesson 2 - preliminary drawing
What is the function of clearing the interrupt flag in the interrupt program?
STM32 uses a simple method to control many lights to achieve the effect of flowing water
Overview of arkui route jump
Get Erlang installation package for free!!
[Go语言入门] 05 Go语言分支语句
[go language introduction] 06 go language circular statement
Matlab warning: name does not exist or is not a directory
002 pointers and functions
VScode自动添加注释