Financial Tsunami

COP 3502: PROGRAMMING ASSIGNMENT 4

DUE DATE: MARCH 16, 4:00 PM

 

Name your class as PA4 and turn in .java file through Sakai. (20 points)

Financial Tsunami

Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it may not be able to pay back the loan (You might remember 2008 banking crisis). 

A bank’s total assets are its current balance plus its loans to other banks. Figure 1 is a diagram that shows five banks. The banks’ current balances are 25, 125, 175, 75, and 181 million dollars, respectively. The directed edge from node 1 to node 2 indicates that bank 1 lends 40 million dollars to bank 2.

 

 

Figure 1 Banks lend money to each other.

 

If a bank’s total assets are under a certain limit, the bank is unsafe. The money it borrowed cannot be returned to the lender, and the lender cannot count the loan in its total assets. Consequently, the lender may also be unsafe, if its total assets are under the limit. 

Write a program to find all unsafe banks. Your program reads the input as follows. It first reads two integers nand limit, where n indicates the number of banks and limit the minimum total assets for keeping a bank safe. It then reads n lines that describe the information for n banks with id from 0 to n-1. The first number in the line is the bank’s balance, the second number indicates the number of banks that borrowed money from the bank, and the rest are pairs of two numbers. Each pair describes a borrower. The first number in the pair is the borrower’s id and the second is the amount borrowed. For example, the input for the five banks in Figure 1 is as follows (note that the limit is 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

 

The  total  assets  of  bank  3  are  (75 + 125 = balance + loan),  which  is  under 

201.  So bank 3 is unsafe. After bank 3 becomes unsafe, the total assets of bank 1 will fall below (125 + 40). So, bank 1 is also unsafe. The output of the program should be 

>Unsafe banks are 3 1

(Hint:   Use   a   two-dimensional   array   borrowers to   represent   loans. borrowers[i][j] indicates the loan that bank i loans to bank j. Once bank j becomes unsafe, borrowers[i][j] should be set to 0.) 

  

import java.util.Scanner;

public class J717
{
  public static void main (String[] args){
    Scanner scan = new Scanner (System.in);
    int banks = scan.nextInt();
    double[] b = new double[banks];
    double[][] borrowsers = new double[5][5];
    int limit = scan.nextInt();
    for (int i =0;i<b.length;i++){
      b[i] = scan.nextDouble();
      int howmany = scan.nextInt();
      for (int j =0;j<howmany;j++){
        borrowsers[i][scan.nextInt()] = scan.nextDouble();
      }
    }
  for (int j =0;j<banks;j++){
    for (int k =0; k<banks;k++){
      int total =0;
      //Calculating total number of loans given by k
      for (int l =0;l<banks;l++){
        total +=borrowsers[k][l];
      }
      if (total + b[k] < limit){
        for (int m =0;m<banks;m++){
          borrowsers[m][k] = 0;
        }
      }
    }
  }
  System.out.print("Unsafe banks are ");
  for (int k =0; k<banks;k++)
  {
    int total =0;
    //Calculating total number of loans made by k
    for (int l =0;l<banks;l++)
    {
      total +=borrowsers[k][l];
    }
    if (total + b[k] < limit)
    {
      System.out.print(k + " ");
    }
    scan.close();
  }
}
}

 

转载于:https://www.cnblogs.com/zhangyongjian/p/3632764.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值