计算是错误的C ++硬币找零算法 [英] Calculating it wrong in a C++ coin change algorithm

查看:113
本文介绍了计算是错误的C ++硬币找零算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想建立这个节目,在那里我想采取从用户这是一个总量,其量是支付量的输入。现在,余款应被分解为$ 10 $ 5,$ 1季度,一角硬币和尼克斯。但是,下面的程序/ code,告诉我宿舍,硬币,和尼克斯的余额总数,而不是其余。例如 。如果总金额为5.76和支付金额为$ 15将它应该显示,零$ 10 1 $ 5,4 $ 1,零$ .25,2 $ .10零$ .05和四$ .01

So, I am trying to build this program, where I am suppose to take an input from a user which is a total amount and an amount which is the paid amount. Now, the balance should be broken down into $10, $5, $1, quarters, dimes and nickles. But the below program/code, tells me the total number of quarters, dimes, and nickles in the balance and not the remaining. For example . If the total amount is 5.76 and paid amount is $15 then it should show, zero $10, one $5, 4 $1, zero $.25, 2 $.10 , zero $.05 and four $.01 .

请告诉我,什么是错的code在这里?

Please tell me whats wrong in the code here?

推荐答案

最简单的解决方法是让数为float然后乘以100,然后将其保存为INT。

The easiest fix is to get the number as a float then multiplying it by 100 then saving it as int.

#include <iostream>

using namespace std;

int main ( )
{
    float userNUmber;
    int change, quarters, dimes, nickels, pennies; // declare variables
    cout <<"Enter the amount of money: ";
    cin >> userNUmber; // input the amount of change
    change = userNUmber * 100;
    quarters = change / 25; // calculate the number of quarters
    change = change % 25; // calculate remaining change needed
    dimes = change / 10; // calculate the number of dimes
    change = change % 10; // calculate remaining change needed
    nickels = change / 5; // calculate the number of nickels
    pennies = change % 5; // calculate pennies

    cout << "\nQuarters: " << quarters << endl; // display # of quarters
    cout << " Dimes: " << dimes << endl; // display # of dimes
    cout << " Nickels: " << nickels << endl; // display # of nickels
    cout <<" Pennies: " << pennies << endl; // display # of pennies
    system("Pause");
    return (0);
}

也做在一个循环是这样的

Also to do it in a loop is something like this

#include <iostream>
#include <vector>
#include <string>
using namespace std;


int main ( )
{
    float userNUmber;
    int change, quarters, dimes, nickels, pennies; // declare variables

    cout <<"Enter the amount of money: ";
    cin >> userNUmber; // input the amount of change
    change = userNUmber * 100;

    vector<int> coins(4, 0);
    coins[0] = 25; coins[1] = 10; coins[2] = 5; coins[3] = 1;
    vector<string> coinsName(4, "");
    coinsName[0] = "Quaters"; coinsName[1] = "Dimes"; coinsName[2] = "Nickels"; coinsName[3] = "Pennies";

    for(size_t i = 0; i < coins.size(); ++i)
    {
        int numberOfCoins = change / coins[i]; // calculate the number of quarters
        change = change % coins[i]; // calculate remaining change needed
        cout << coinsName[i] << ": " << numberOfCoins << endl; // display # of quarters
    }
    system("Pause");
    return (0);
}

这篇关于计算是错误的C ++硬币找零算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆