当前位置:网站首页>Mistakes in Brushing the Questions 1-Implicit Conversion and Loss of Precision
Mistakes in Brushing the Questions 1-Implicit Conversion and Loss of Precision
2022-08-02 09:33:00 【timerring】
Brush the wrong title
1.球的体积
Given you the radius of a sphere RR,Please calculate the volume of the sphere.
The formula for calculating a sphere is V = ( 4 / 3 ) ∗ π ∗ R 3 V=(4/3)∗π∗R^3 V=(4/3)∗π∗R3.
π取 3.14159.
输入格式
输入一个整数 R.
输出格式
输出格式为 VOLUME = X
,其中 XX is the volume of the sphere,结果保留三位小数.
数据范围
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…,Recommended for use in formulas (4/3.0).
2.钞票
在这个问题中,You need to read an integer value and decompose it into the sum of multiple bills,Multiple banknotes of each denomination can be used,And require the use of as few banknotes as possible.
Please output the read value and the banknote list.
钞票的可能面值有 100,50,20,10,5,2,1.
输入格式
输入一个整数 N.
输出格式
参照输出样例,Outputs the read value and the required number of banknotes of each denomination.
数据范围
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;
}
总结
错题总结:The previous approach was too cumbersome,It can be done with greedy thinking.Use the remainder as the base for the next decomposition.
3.钞票和硬币
Reads a floating point number with two decimal places,This represents monetary value.
在此之后,Decompose this value into the sum of various banknotes and coins,An unlimited number of banknotes and coins of each denomination can be used,The number of banknotes and coins required to be used is as small as possible.
The face value of the banknote is 100,50,20,10,5,2100,50,20,10,5,2.
The face value of the coin is 1,0.50,0.25,0.10,0.051,0.50,0.25,0.10,0.05 和 0.010.01.
输入格式
输入一个浮点数 NN.
输出格式
参照输出样例,Output the number of bills and coins required for each denomination.
数据范围
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;
}
错误
The first test case passed successfully,But the second test case was reportedWA.
具体结果如下:
输入
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
At the very end, there is clearly leftovers0.01,0.01/0.01=1,but output0.
总结
The loss of precision you have here is due to the representation of floating point numbers if you think about it from the bottom up,In fact, the floating point number that has been defined is an approximation,0.01实际是0.010000000000012这样的数,But using subtraction will result in what we see at the endn显示是0.01But he is actually an approximation0.01的一个数,但比0.01小,Added because defined is larger than shown0.00001就解决了这个问题,This can make the number that was not enough to be divided enough to be divided
So we can make the following modifications to the output of the decimal above:
printf("%d nota(s) de R$ %.2f\n",(int)(sum/b[i]+0.00001),b[i]);
结果成功AC.
边栏推荐
- Jenkins--基础--6.1--Pipeline--介绍
- LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
- cococreator 动态设置精灵
- HikariCP数据库连接池,太快了!
- The packet capture tool Charles modifies the Response step
- 百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)
- 稳定币:对冲基金做空 Tether 的结局会是什么?
- tf中tensor的大小输出
- Use the scrapy to climb to save data to mysql to prevent repetition
- 大厂外包,值得拥有吗?
猜你喜欢
随机推荐
XML简介
被报表需求逼疯的银行数据人,是时候放弃用Excel做报表了
Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
SVN下载上传文件
nacos项目搭建
Docker内MySQL主从复制学习,以及遇到的一些问题
【微信小程序】本地服务页面案例实现
Jenkins--基础--6.3--Pipeline--语法--脚本式
mysql进阶(二十一)删除表数据与数据库四大特性
【技术分享】OSPFv3基本原理
EdrawMax Crack,多合一的图表应用程序
使用scrapy 把爬到的数据保存到mysql 防止重复
Jenkins--基础--6.2--Pipeline--语法--声明式
【Redis】通用命令
HikariCP数据库连接池,太快了!
sql concat(),如何才能拼接表的名字
spark:热门品类中每个品类活跃的SessionID统计TOP10(案例)
node封装一个图片拼接插件
Jetpack Compose 中的状态管理
大厂外包,值得拥有吗?