【发布时间】:2020-05-10 02:53:59
【问题描述】:
描述:假设我们可以从自动售货机上以每块 1 美元的价格购买一块巧克力棒。每个巧克力里面 酒吧是优惠券。我们可以从机器上用六张优惠券兑换一根巧克力棒。这表示 一旦你开始从机器上购买巧克力棒,你总会有一些优惠券。
编写一个程序来知道如果我们从 N 美元开始并且总是可以吃多少巧克力棒 如果我们有足够的额外巧克力棒,请兑换优惠券。
我需要编写两个函数并使用for循环。
我不想输入两次美元值。
#include <iostream>
using namespace std;
int chocCalc(int n){
int choc = 0, coup = 0, moreChoc = 0, dollars;
cout << "Enter dollars: ";
cin >> dollars;
// Calculating for amount of chocolates including the ones reedemed by cupons.
int i=0;
for (i = 1; i <= dollars; i++ ){
choc = dollars;
coup = choc;
moreChoc = coup / 6;
choc += moreChoc;
return choc;
}
}
int cupon(int m){
int choc = 0, coup = 0, moreChoc = 0, dollars, coupLeft = 0;
cout << "Enter dollars again: ";
cin >> dollars;
int i=0;
for (i = 1; i <= dollars; i++ ){
choc = dollars;
coup = choc;
// calculating the amount of cupons left
moreChoc = coup / 6;
choc += moreChoc;
coupLeft = coup%6;
return coupLeft;
}
}
int main(){
cout << "You can eat: " << chocCalc(0) << " chocolates \n";
cout << "and " << cupon(0) << " cupons are left.";
return 0;
}
【问题讨论】:
-
有什么问题?
-
将美元设置为全局变量
标签: c++ function c++11 for-loop