我有一份家庭作业,我觉得我已经接近正确。作业如下:
银行互相贷款。在经济困难时期,如果银行破产,它可能无法偿还贷款。银行的总资产是其当前余额加上对其他银行的贷款。图8.1(附图)是一个图表,显示了五个银行。银行目前的余额分别为:25,125,175,75和1.81亿美元。从节点1到节点2的有向边指示银行1贷款40到银行2。
如果银行总资产低于一定限额,银行将被视为不安全。如果银行不安全,其借入的资金不能退还给贷方,贷方也不能将贷款计入其总资产中。因此,贷款人也可能不安全。
编写程序以查找所有不安全的银行。您的程序按如下方式读取输入。它首先读取两个整数n
和limit
,其中n
表示银行数量,limit
是保持银行安全的最低资产。然后,它会读取n
行,这些行描述id为0到n-1的n
个银行的信息。该行中的第一个数字是银行的余额。第二个数字表示从银行借来的钱数,其余的是两个数字对。每对描述一个借款人。第一个数字是银行ID,第二个数字是借入的数量。假设最大存储体数为100.例如,五个存储体的输入如下(限制为201)
5 201
25 2 1 100.5 4 320.5
125 2 2 40 3 85
175 2 0 125 3 75
75 1 0 125
181 1 2 125
银行3的总资产为75加125,不到201,因此银行不安全。在银行3不安全之后,银行1的总资产变为125 + 40,现在也不安全。输出应该是“不安全的银行是3 1”
这是我目前解决问题的方法。我无法弄清楚如何找到所有不安全的银行。只是第一个。我把它设置为静态输入进行测试。如果我能让它正常工作,我就可以使用工作用户输入代码了。
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 100;
double balance[SIZE];
double loan[SIZE][SIZE];
int nobanks;
int limit;
int i = 0;
int j = 0;
int k = 0;
int noborrowers;
double assets[SIZE];
bool isSafe[SIZE];
bool newunsafefound = true;
cout << "Enter number of banks and the limit:" << endl;
// Set all of the data
nobanks = 5;
limit = 201;
balance[0] = 25.0;
balance[1] = 125.0;
balance[2] = 175.0;
balance[3] = 75.0;
balance[4] = 181.0;
loan[0][1] = 100.5;
loan[0][4] = 320.5;
loan[1][2] = 40.0;
loan[1][3] = 85.0;
loan[2][0] = 125.0;
loan[2][3] = 75.0;
loan[3][0] = 125.0;
loan[4][2] = 125.0;
// Set array to all true values
for(i = 0; i < nobanks; i++)
{
isSafe[i] = true ;
}
cout << "Unsafe banks are: ";
i=0;
while(isSafe[i] == true)
{
newunsafefound=false;
i=0;
do
{
assets[i] = balance[i]; //Set assets to balance
for (j = 0; j < nobanks; j++) // Check if a bank has loans and add them to assets
{
if (loan[i][j] >= 0)
assets[i] += loan[i][j];
}
if (assets[i] < limit) // Check to see if current bank meets limit
{
isSafe[i] = false; // Set bank to not safe if limit not met
newunsafefound = true;
cout << i << " " ; //Display the bank that is unsafe and a space for the next bank
k=0;
for (k = 0; k < nobanks; k++)
{
loan[i][k] = 0; //Set banks loans to 0 if unsafe.
k++;
}
}
i++;
} while(i < nobanks);
}
return (0);
}
我做错了什么?
答案 0 :(得分:1)
for (k = 0; k < nobanks; k++)
{
loan[i][k] = 0; //Set banks loans to 0 if unsafe.
k++;
}
k
的额外增量看起来非常可疑;)
答案 1 :(得分:1)
您必须显式初始化loan
数组,因此您不使用的元素没有任意值:
double loan[SIZE][SIZE] = {{0}};
同样loan[i][k] = 0;
表示您将银行i
给予的贷款归零到银行k
,但您想要的是零钱银行i
借用到银行k
。
还有一个问题:
(nobank-1)
不安全时才会退出),newunsafefound
时没有做变量名称所暗示的内容。答案 2 :(得分:0)
// Astrid Giraldo
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int n; // number of banks
double limit; // Minimum total assets for keeping a bank safe.
System.out.println("Enter number of banks: ");
n = input.nextInt();
System.out.println("Enter minimum total assets to be a safe bank: ");
limit = input.nextDouble();
double[][] borrowers = new double[n][n];
double[] balance = new double[n];
int numBorrowers;
for (int i = 0; i < borrowers.length; i++) {
System.out.println("Enter the bank's balance");
balance[i] = input.nextDouble();
System.out.println("Enter number of borrowers from this bank");
numBorrowers = input.nextInt();
for (int j = 0; j < numBorrowers; j++) {
System.out.println("Enter borrower bank id and the amount borrowed: ");
borrowers[i][input.nextInt()] = input.nextDouble();
}
}
markUnsafeBanks(borrowers,balance, limit);
displayUnsafeBanks(borrowers, balance, limit);
}
public static double analizeUnsafeBanks(double[][] borrowers, double[] balance, int bankId) {
double sum = balance[bankId];
for (int i = 0; i < borrowers.length; i++) {
sum += borrowers[bankId][i];
}
return sum;
}
public static void setLoanToZero(double[][] borrowers, double[] balance, int unsafeBankId, double limit) {
for (int i = 0; i < borrowers.length; i++) {
if (borrowers[i][unsafeBankId] > 0) {
borrowers[i][unsafeBankId] = 0;
if ( unsafeBankId > i && analizeUnsafeBanks(borrowers, balance, i) < limit) {
setLoanToZero(borrowers, balance, i, limit);
}
}
}
}
public static void markUnsafeBanks(double[][] borrowers, double[] balance, double limit) {
for (int i = 0; i < borrowers.length; i++) {
if (analizeUnsafeBanks(borrowers, balance, i) < limit) {
setLoanToZero(borrowers, balance, i, limit);
}
}
}
public static void displayUnsafeBanks(double[][] borrowers, double[] balance, double limit){
for (int i = 0; i < borrowers.length; i++) {
double assets = analizeUnsafeBanks(borrowers,balance,i);
if ( assets < limit) {
System.out.println("Bank " + i + " is unsafe. It assets are " + assets);
}
}
}