当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
AutoJs学习-AES加解密
Pycharm (1) the basic use of tutorial
ORBSLAM代码阅读
十、 网络管理
XML简介
function call to print lua internal structure
HCIA动态主机配置协议实验(dhcp)
【技术分享】OSPFv3基本原理
Jenkins--部署--3.1--代码提交自动触发jenkins--方式1
tf.where使用
Facebook自动化数据分析方案,广告投放省心省力
恋爱十不要
Spend 2 hours a day to make up for Tencent T8, play 688 pages of SSM framework and Redis, and successfully land on Meituan
1对1视频源码——快速实现短视频功能提升竞争力
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
cococreator dynamically set sprite
四字节的float比八字结的long范围大???
软件exe图标变记事本或浏览器、360压缩打不开的几种应急解决方法
tf中tensor的大小输出
初学者怎么快速学会SQL