这个是问我们数学建模的,我想出来一个方案,就是遍历一个对象,但是对象名称我不可能一个一个打,不知道怎么遍历,而且遍历完,还要遍历每个对象的函数具体看图片,求各位高人指导下,有偿的。我发的工程还没做完,你们看看
如果自己有自己的方法和想法或者代码也欢迎交流
用了一个结构体 Person 来表示每个借款人的信息,用了一个循环来模拟一年内的借贷过程,用了一个函数 calcInterest 来计算每天的利息,并根据借款人的还款情况更新借贷记录
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
const int NUM_PERSONS = 10;
const int LOAN_AMOUNT = 300000;
const int DAILY_PRINCIPAL_REPAYMENT = 10000;
const int DAILY_INTEREST_RATE = 1000;
struct Person {
int id;
int loan;
int interest;
bool is_repaid;
};
void calcInterest(Person& person, int days) {
int unpaid = person.loan - person.interest;
int interest_today = unpaid * DAILY_INTEREST_RATE / 100000;
person.interest += interest_today;
if (person.is_repaid || person.interest >= person.loan) {
person.interest = person.loan;
person.is_repaid = true;
} else if (days % 30 == 0) {
int principal_today = min(DAILY_PRINCIPAL_REPAYMENT, unpaid);
person.loan -= principal_today;
}
}
int main() {
vector<Person> persons;
for (int i = 0; i < NUM_PERSONS; i++) {
persons.push_back({i + 1, LOAN_AMOUNT, 0, false});
}
int days_passed = 0;
int total_interest = 0;
while (days_passed < 365) {
for (auto& person : persons) {
calcInterest(person, days_passed);
total_interest += person.interest - (person.is_repaid ? 0 : person.loan);
}
days_passed++;
}
cout << "Total interest earned in one year: " << total_interest << endl;
return 0;
}