如何将钱分成不同的金额

时间:2018-04-07 16:55:24

标签: c++

我尝试这个程序好几个小时,但我不能。

问题是我从用户那里拿了一些钱。然后,我要问他有多少5个,有多少2个。我将这笔钱转换为5s,2s和1s。用户拥有1的无限来源。简而言之,如果用户输入27我就告诉他5 5s和1 2s。

现在我做了这种类型的程序,其中我转换了时间(从几秒到几年等等)

现在我在程序中这样做了: -

int money;
cout << "How much amount of money= ";
cin >> money;
cout << endl;

int ao5;
cout << "how many number of 5 coins available in your drawer= ";
cin >> ao5;
cout << endl;

int ao2;
cout << "how many number of 2 coins available in your drawer= ";
cin >> ao2;
cout << endl;
int fchange, tchange, ochange;

ochange = (money / 1) % ao5%ao2;
tchange = (money / ao2) % ao5;
fchange = money / ao5;





cout << " " << fchange;
cout << "  " << tchange;
cout << " " << ochange;

我尝试过其他方法。我尝试将钱除以5,然后用5s减去它,但这没有任何意义。

任何人都可以带我走正确的道路吗?

1 个答案:

答案 0 :(得分:1)

您可以使用此

void divid(int num){
if(num>=5){
        int remainderAfterdevidingTo5 = num%5;
        int numOf5s = num/5;
        std::cout << "  numOf5s  "<< numOf5s<< std::endl;
        divid(remainderAfterdevidingTo5);
    }else if(num>=2){
        int remainderAfterdevidingTo2 = num%2;
        int numOf2s = num/2;
        std::cout <<  "  numOf2s  "<< numOf2s<< std::endl;
        divid(remainderAfterdevidingTo2);
    }else if(num>=1){
        std::cout << "  numOf1s  " <<num<< std::endl;
    }
}