当前位置:网站首页>刷题错题录1-隐式转换与精度丢失
刷题错题录1-隐式转换与精度丢失
2022-08-03 19:56:00 【timerring】
刷题错题录
1.球的体积
给定你一个球体的半径 RR,请你计算球体的体积。
计算球体的公式为
。
π取 3.14159。
输入格式
输入一个整数 R。
输出格式
输出格式为 VOLUME = X,其中 XX 为球体的体积,结果保留三位小数。
数据范围
1≤R≤2000
输入样例
3输出样例
VOLUME = 113.097代码
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double a,V;
cin>>a;
V=(4/3.0)*3.14159*a*a*a;
printf("VOLUME = %.3f",V);
return 0;
}错题总结
注意:有些语言中 (4/3) 无法得到 1.3333…,建议在公式中使用 (4/3.0)。
2.钞票
在这个问题中,你需要读取一个整数值并将其分解为多张钞票的和,每种面值的钞票可以使用多张,并要求所用的钞票数量尽可能少。
请你输出读取值和钞票清单。
钞票的可能面值有 100,50,20,10,5,2,1。
输入格式
输入一个整数 N。
输出格式
参照输出样例,输出读取数值以及每种面值的钞票的需求数量。
数据范围
0<N<1000000
输入样例:
576输出样例:
576
5 nota(s) de R$ 100,00
1 nota(s) de R$ 50,00
1 nota(s) de R$ 20,00
0 nota(s) de R$ 10,00
1 nota(s) de R$ 5,00
0 nota(s) de R$ 2,00
1 nota(s) de R$ 1,00代码
#include <iostream>
using namespace std;
int main()
{
int n, a[7] = {100, 50, 20, 10, 5, 2, 1};
cin >> n;
printf("%d\n", n);
for (int i = 0; i < 7; i ++ )
{
printf("%d nota(s) de R$ %d,00\n", n / a[i], a[i]);
n %= a[i];
}
return 0;
}总结
错题总结:之前的做法太过麻烦,可以考虑采用贪心的思想来完成。用余数作为下一次分解的基数。
3.钞票和硬币
读取一个带有两个小数位的浮点数,这代表货币价值。
在此之后,将该值分解为多种钞票与硬币的和,每种面值的钞票和硬币使用数量不限,要求使用的钞票和硬币的数量尽可能少。
钞票的面值是 100,50,20,10,5,2100,50,20,10,5,2。
硬币的面值是 1,0.50,0.25,0.10,0.051,0.50,0.25,0.10,0.05 和 0.010.01。
输入格式
输入一个浮点数 NN。
输出格式
参照输出样例,输出每种面值的钞票和硬币的需求数量。
数据范围
0≤N≤1000000.000≤N≤1000000.00
输入样例:
576.73输出样例:
NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
3 moeda(s) de R$ 0.01代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
double sum,b[6]={1.00,0.50,0.25,0.10,0.05,0.01};
int a[6]={100,50,20,10,5,2};
scanf("%lf",&sum);
cout<<"NOTAS:"<<endl;
for(int i=0;i<6;i++)
{
printf("%d nota(s) de R$ %d.00\n",(int)(sum/a[i]),a[i]);
sum-=((int)sum/a[i])*a[i];
}
cout<<"MOEDAS:"<<endl;
for(int i=0;i<6;i++)
{
printf("%d moeda(s) de R$ %.2f\n",(int)(sum/b[i]),b[i]);
printf("%.2f\n",sum);
sum-=((int)(sum/b[i]))*b[i];
}
return 0;
}错误
第一个测试样例顺利通过,但是第二个测试样例却报了一发WA。
具体结果如下:
输入
463.01输出
NOTAS:
4 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
0 nota(s) de R$ 20.00
1 nota(s) de R$ 10.00
0 nota(s) de R$ 5.00
1 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1.01
0 moeda(s) de R$ 0.50
0.01
0 moeda(s) de R$ 0.25
0.01
0 moeda(s) de R$ 0.10
0.01
0 moeda(s) de R$ 0.05
0.01
0 moeda(s) de R$ 0.01 //出现问题
0.01在最末尾的时候明明剩余0.01,0.01/0.01=1,但是却输出0。
总结
你这里的精度丢失如果从底层来考虑的话是因为浮点数的表示所导致的,其实已开始定义的浮点数就是一个近似值,0.01实际是0.010000000000012这样的数,但是如果用减法的话会导致最后我们看到的n显示是0.01但是他实际是一个近似0.01的一个数,但比0.01小,因为定义的比所显示的大所以加上0.00001就解决了这个问题,这样可以使原来不够除的数够除了
因此我们可以对上面小数的输出做以下修改:
printf("%d nota(s) de R$ %.2f\n",(int)(sum/b[i]+0.00001),b[i]);结果成功AC。
边栏推荐
- MySQL基础
- 安装anaconda并创建虚拟环境
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
- 涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流
- 利用 rpush 和 blpop 实现 Redis 消息队列
- 汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
- JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
- net-snmp私有mib动态加载到snmpd
- codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
- 网络协议-TCP、UDP区别及TCP三次握手、四次挥手
猜你喜欢

花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!

622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)

1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)

倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即

亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得

Network protocol-TCP, UDP difference and TCP three-way handshake, four wave

Redis 内存满了怎么办?这样置才正确!

头条服务端一面经典10道面试题解析

那些年我写过的语言

Node version switching tool NVM and npm source manager nrm
随机推荐
演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
glide set gif start stop
PHP according to the longitude and latitude calculated distance two points
epoll + 线程池 + 前后置服务器分离
MySQL master-slave, 6 minutes you master!
LeetCode 622. 设计循环队列
利用 rpush 和 blpop 实现 Redis 消息队列
JWT详解
边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型
Reveal how the five operational management level of hundreds of millions of easily flow system
ThreadLocal详解
【STM32】标准库-自定义BootLoader
高位套牢机构,用友网络的信任危机是如何产生的?
CS免杀姿势
百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东
高并发,你真的理解透彻了吗?
DeepMCP网络详解
List类的超详细解析!(超2w+字)
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
Unity gets the actual coordinates of the ui on the screen under the canvas