Cho số tiền phải trả pay_Rupees, hai mệnh giá Rupees_amount_1, Rupees_amount_2, và tổng số tờ bắt buộc distribution_total. Tìm số tờ mệnh giá 1 (trả về -1 nếu không thể).
Công thức
a1 = (pay_Rupees - Rupees_amount_2 * distribution_total) / (Rupees_amount_1 - Rupees_amount_2)
Nếu a1 nguyên thì hợp lệ, ngược lại trả -1.
Ví dụ hoàn chỉnh
#include <bits/stdc++.h>
using namespace std;
int notes_needed(int Rupees_amount_1, int Rupees_amount_2, int pay_Rupees, int distribution_total) {
int total = pay_Rupees - (Rupees_amount_2 * distribution_total);
int total_given = Rupees_amount_1 - Rupees_amount_2;
if (total % total_given == 0)
return total / total_given;
else
return -1;
}
int main() {
int Rupees_amount_1 = 1;
int Rupees_amount_2 = 5;
int pay_Rupees = 11;
int distribution_total = 7;
cout << "Số tờ mệnh giá 1: " << notes_needed(Rupees_amount_1, Rupees_amount_2, pay_Rupees, distribution_total);
return 0;
}
Kết quả
Số tờ mệnh giá 1: 6