提问人:Henrique Bispo 提问时间:5/2/2023 最后编辑:Henrique Bispo 更新时间:5/5/2023 访问量:103
生成以美分为单位的结果时出错 (beecrowd - 1021)
Error when generating results in cents (beecrowd - 1021)
问:
该代码显然正在工作,但是在插入值时,代码无法以美分为单位返回值。
这是beecrowd练习1021。576.73
#include <stdio.h>
#include <math.h>
int calculo(float num);
int main() {
float numero;
scanf("%f",&numero);
calculo(numero);
return 0;
}
int calculo(float num) {
//TRANSFORMA PARA INTEIRO
int n;
//NOTAS
int n100, n50, n20, n10, n5, n2;
//MOEDAS
int m1, m05, m025, m010, m005, m001;
//NOTAS
n = floor(num);
n100 = n/100;
n50 = (n%100)/50;
n20 = ((n%100)%50)/20;
n10 = (((n%100)%50)%20)/10;
n5 = ((((n%100)%50)%20)%10)/5;
n2 = (((((n%100)%50)%20)%10)%5)/2;
//MOEDAS
m1 = (((((n%100)%50)%20)%10)%5)%2;
n = num*100;
n = (int) n*1;
n = n%100;
m05 = n/50;
n = n%50;
m025 = n/25;
n = n%25;
m010 = n/10;
n = n%10;
m005 = n/5;
m001 = n%5;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100,00\n",n100);
printf("%d nota(s) de R$ 50,00\n",n50);
printf("%d nota(s) de R$ 20,00\n",n20);
printf("%d nota(s) de R$ 10,00\n",n10);
printf("%d nota(s) de R$ 5,00\n",n5);
printf("%d nota(s) de R$ 2,00\n",n2);
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1,00\n",m1);
printf("%d moeda(s) de R$ 0,50\n",m05);
printf("%d moeda(s) de R$ 0,25\n",m025);
printf("%d moeda(s) de R$ 0,10\n",m010);
printf("%d moeda(s) de R$ 0,05\n",m005);
printf("%d moeda(s) de R$ 0,01\n",m001);
return 0;
}
答:
0赞
NoDakker
5/2/2023
#1
在审查您对货币和硬币的问题和要求时,由于输入的货币价值将采用小数点后两位数字的格式,因此使用整数值仍然是可行的,如上面的好评中所述。需要做的是查看输入的货币的最小货币价值,似乎是欧元。
考虑到这一点,以下是代码的重构版本,允许用户输入带有小数位的货币值,但在内部将输入的值转换为整数值,然后将该金额计算为等量的纸币和硬币。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int calculo(int num);
int main()
{
char inp[10];
int numero = 0, counter, multiplier = 1;
printf("Enter monetary amount: "); /* User friendly prompt */
if (scanf("%s",inp) != 1) /* Input a string */
{
printf("Input error\n");
return -1;
}
counter = strlen(inp);
while (1) /* Read through the string and convert to an integer value */
{
if (inp[counter] >= '0' && inp[counter] <= '9')
{
numero += (inp[counter] - '0') * multiplier;
multiplier *= 10;
}
counter --;
if (counter < 0)
{
break;
}
}
calculo(numero); /* Calculate the quantity of currency and coins */
return 0;
}
int calculo(int num)
{
//TRANSFORMA PARA INTEIRO
int n;
//NOTAS
int n100, n50, n20, n10, n5, n2;
//MOEDAS
int m1, m05, m025, m010, m005, m001;
//NOTAS
n = num / 100; /* In lieu of using the math "floor" function */
n100 = n/100;
n50 = (n%100)/50;
n20 = ((n%100)%50)/20;
n10 = (((n%100)%50)%20)/10;
n5 = ((((n%100)%50)%20)%10)/5;
n2 = (((((n%100)%50)%20)%10)%5)/2;
//MOEDAS
m1 = (((((n%100)%50)%20)%10)%5)%2;
n = num - n * 100; /* Acquire the value for calculating change */
//n = (int) n*1;
n = n%100;
m05 = n/50;
n = n%50;
m025 = n/25;
n = n%25;
m010 = n/10;
n = n%10;
m005 = n/5;
m001 = n%5;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100,00\n",n100);
printf("%d nota(s) de R$ 50,00\n",n50);
printf("%d nota(s) de R$ 20,00\n",n20);
printf("%d nota(s) de R$ 10,00\n",n10);
printf("%d nota(s) de R$ 5,00\n",n5);
printf("%d nota(s) de R$ 2,00\n",n2);
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1,00\n",m1);
printf("%d moeda(s) de R$ 0,50\n",m05);
printf("%d moeda(s) de R$ 0,25\n",m025);
printf("%d moeda(s) de R$ 0,10\n",m010);
printf("%d moeda(s) de R$ 0,05\n",m005);
printf("%d moeda(s) de R$ 0,01\n",m001);
return 0;
}
在美国,我们会指出我们正在使用“美分”而不是“美元”的等效值,因此根据情况,正在评估的值要么乘以“100”,要么除以“100”。以下是重构代码的一些关键点。
- 如前所述,输入的数据实际上是一个字符串,它允许用户输入带有分数的货币金额。
- 此字符串将转换为整数。
- 整数值用作输入参数来代替浮点值,以避免可能出现的浮点精度问题,如上面的好评论中所述。
- 根据需要,将派生的整数值除以“100”,而不是使用“floor”函数。
有了这些注意到的变化,以下是使用本问题中最初提到的值的测试输入和输出。
@Vera:~/C_Programs/Console/Money/bin/Release$ ./Money
Enter monetary amount: 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
快速检查纸币数量和硬币数量似乎是正确的。
同样,为了扩展上面的评论,通常使用整数变量来处理货币金额,以避免可能的浮点变量问题。
试试这个重构的代码,看看它是否符合你的项目精神。
0赞
Henrique Bispo
5/5/2023
#2
#include <stdio.h>
int calculo(double num);
int main() {
double numero;
scanf("%lf",&numero);
calculo(numero);
return 0;
}
int calculo(double num) {
//TRANSFORMA PARA INTEIRO
int n;
//NOTAS
int n100, n50, n20, n10, n5, n2;
//MOEDAS
int m1, m05, m025, m010, m005, m001;
//NOTAS
n = num;
n100 = n/100;
n50 = (n%100)/50;
n20 = ((n%100)%50)/20;
n10 = (((n%100)%50)%20)/10;
n5 = ((((n%100)%50)%20)%10)/5;
n2 = (((((n%100)%50)%20)%10)%5)/2;
//MOEDAS
m1 = (((((n%100)%50)%20)%10)%5)%2;
n = num*100;
n = n%100;
m05 = n/50;
n = n%50;
m025 = n/25;
n = n%25;
m010 = n/10;
n = n%10;
m005 = n/5;
m001 = n%5;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100,00\n",n100);
printf("%d nota(s) de R$ 50,00\n",n50);
printf("%d nota(s) de R$ 20,00\n",n20);
printf("%d nota(s) de R$ 10,00\n",n10);
printf("%d nota(s) de R$ 5,00\n",n5);
printf("%d nota(s) de R$ 2,00\n",n2);
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1,00\n",m1);
printf("%d moeda(s) de R$ 0,50\n",m05);
printf("%d moeda(s) de R$ 0,25\n",m025);
printf("%d moeda(s) de R$ 0,10\n",m010);
printf("%d moeda(s) de R$ 0,05\n",m005);
printf("%d moeda(s) de R$ 0,01\n",m001);
return 0;
}
评论
float
double
float
float
int amount = 100*numero +0.5;