c++ - 钞票程序

标签 c++

<分区>

我正在解决 following programming problem :

URI Online Judge | 1018 - Banknotes

In this problem you have to read an integer number and calculate the smallest possible number of notes in which the value may be decomposed. The notes are of 100, 50, 20, 10, 5, 2 e 1. Print the value read and the list of notes.

Input

The input file contain an integer number N (0 < N < 1000000).

Output

Print the minimum quantity of necessary banknotes, like as the given example.

我尝试了以下解决方案:

#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

bool NTrue(int n);
void HowMany(int x, int BankNote);

int main(int argc, char ** argv) { 
    int N;
    cin >> N;
    int BankNote = 100;
    if (NTrue(N)) { 
        while (BankNote != 0) { 
            if (BankNote == 25)
                BankNote = 20;  
            HowMany(N, BankNote); 
            N = N % BankNote;
            BankNote = BankNote / 2;
        }
    }
    return 0;
}

bool NTrue(int n) {
    if (0 < n && n <= pow(10, 6))
        return true;
    else
        return false;
}

void HowMany(int x, int BankNote) {
    int result = x / BankNote;
    float BN = BankNote;
    cout << result << " nota(s) de R$ " << fixed << setprecision(2) << BN << endl;
}

我没有得到想要的结果。我做错了什么?

最佳答案

我看到输出必须有一个特殊的格式,写 1,00 而不是 1。这里的逗号字符应该是十进制浮点分隔符。

我猜当在线法官运行你的程序时,它得到的是1.00。用于分数的字符取决于语言环境;在线法官可能使用不同的语言环境,因此它使用点字符 (.) 作为分隔符。

尝试显式打印 .00 部分:

void HowMany(int x, int BankNote) {
    int result = x / BankNote;
    cout << result << " nota(s) de R$ " << result << ",00" << endl;
}

当然,这只是一个猜测。

关于c++ - 钞票程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887360/

相关文章:

c++ - 在 C++ 中模拟静态构造函数?

c++ - STL - 字符串是 vector 吗?

c++ - Xcode 错误编译 C++ 预期成员名称或声明说明符后的 ';'

c++ - 使用 Poppler (C++) 从 PDF 中提取文本

c++ - 从 vector 中调用派生类函数 (c++)

c++ - 学习 LLVM 后端编程的代码示例

c++ - 程序不适用于 win,但适用于 mac。为什么?

c++ - For循环在C++中以字符串长度无限运行

c++ - 在 C++ 中删除一个三重指针

c++ - 一元 'operator' 没有在 C++ 中定义