首页 > 试题广场 >

发放贷款

[编程题]发放贷款
  • 热度指数:490 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
迫于房价的压力,很多人选择了向银行贷款。假设银行有n万元资金可供贷款,他会根据申请的顺序,依次初次符合要求的申请。
例如,银行有10万元,A、B、C三个人分别贷款5万、8万和3万。
1. 按照顺序,先批准A的申请;
2. 此时银行剩余5万,达不到B的申请要求,因此这一批不处理B的申请;
3. C的申请能被满足,因此批准C。
等A和C偿还了贷款后,银行会发放第二批的贷款,此时B的申请就能被批准。因此,整个处理过程是A、C、B。
现在给你所需的信息,请输出银行批准贷款的顺序。

输入描述:
输入包含多组数据,每组数据第一行包含两个正整数m(1≤m≤50)和n(1≤n≤100),代表有m个人申请贷款,以及银行的资金有n万元。

紧接着m行,每行包含一个字符串name(仅由字母组成,长度不超过16个字符)和整数金额amount(1≤amount≤n),代表申请人的姓名和申请贷款的金额。


输出描述:
对应每一组数据,按照申请被处理的顺序依次输出申请人的姓名。

每组数据之后输出一个空行作为分隔。
示例1

输入

3 10
A 5
B 8
C 3

输出

A
C
B
判断金钱 “<” 还是 “<=” 是解题的关键。前者运气不好会出现死循环,正解后者。找了老半天的错误,原来死在少了个等号上。尴尬~
发表于 2017-06-04 22:52:20 回复(0)
#include <cstdio>
#include <limits.h>
#include <vector>
#include <functional>
#include <string>
#include <iostream>
using namespace std;
int main()
{
 //freopen("in.txt", "r", stdin);
 int m, n;
 string name;
 int loans;
 while (scanf("%d%d", &m, &n) != EOF)
 {
  vector<pair<string, int>> people;
  people.reserve(m);
  for (int i = 0; i < m; ++i)
  {
   cin >> name >> loans;
   people.emplace_back(name, loans);
  }
  vector<bool> has_money(m, false);
  vector<string> order;
  order.reserve(m);
  int leftLoans;
  while (order.size() < m)
  {
   leftLoans = n;
   for (int i = 0; i < m; ++i)
   {
    if (has_money[i] || people[i].second > leftLoans) continue;
    order.emplace_back(people[i].first);
    leftLoans -= people[i].second;
    has_money[i] = true;
   }
  }
  for (auto& s : order) printf("%s\n", s.c_str());
  printf("\n");
 }
 return 0;
}

发表于 2017-07-27 12:08:10 回复(0)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int m = sc.nextInt();
			int n = sc.nextInt();
			People[] p = new People[m];
			for (int i = 0; i < m; i ++ ) {
				p[i] = new People(sc.next(), sc.nextInt(), false);
			}
			int count = 0;
			int money = n;
			while (count < m) {
				for (int i = 0; i < m; i ++ ) {
					if( ! p[i].flag && p[i].need <= money) {
						money -= p[i].need;
						p[i].flag = true;
						count ++ ;
						System.out.println(p[i].name);
					}
				}
				money = n;
			}
			System.out.println();
		}
	}
	static class People {
		String name;
		int need;
		boolean flag;
		public People(String name, int need, boolean flag) {
			this.name = name;
			this.need = need;
			this.flag = flag;
		}
	}
}

发表于 2016-10-13 18:18:11 回复(0)
import sys

m, n = 0, 0
start = 0  # 记录每组开始的索引
names = []
amounts = []
for i, line in enumerate(sys.stdin):
    a = line.split(" ")
    if i == start:
        m = int(a[0])
        n = int(a[1])
        start = i + m + 1
    else:
        names.append(a[0])
        amounts.append(int(a[1]))
        if i == start - 1:
            cur_money = n
            is_process = [False] * len(names) 
            processed_names = []
            count = 0 # 记录处理的人数
            while count < m:
                for i in range(len(names)):
                    if amounts[i] <= cur_money and not is_process[i]:
                        cur_money -= amounts[i]
                        processed_names.append(names[i])
                        is_process[i] = True
                        count += 1
                        if cur_money == 0:
                            break
                cur_money = n
            names = []
            amounts = []

            for name in processed_names:
                print(name)
            print()




发表于 2023-04-12 00:45:48 回复(0)
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

public class Main
{
    private static Scanner sc = new Scanner(System.in);

    static class User
    {
        String name;
        int money;

        public User(String name, int money)
        {
            this.name = name;
            this.money = money;
        }
    }

    public static void main(String[] args)
    {
        while (sc.hasNextLine())
        {
            String[] tmp = sc.nextLine().split(" ");
            int m = Integer.parseInt(tmp[0]);
            int n = Integer.parseInt(tmp[1]);
            LinkedList<User> users = new LinkedList<>();
            for (int i = 0; i < m; i++)
            {
                tmp = sc.nextLine().split(" ");
                users.add(new User(tmp[0], Integer.parseInt(tmp[1])));
            }
            deal(users, n);
            System.out.println();
        }
    }

    private static void deal(LinkedList<User> users, int n)
    {
        Iterator<User> iterator = users.iterator();
        int totalMoney = n;
        while (!users.isEmpty())
        {
            if (iterator.hasNext())
            {
                User user = iterator.next();
                if (user.money <= n)
                {
                    n -= user.money;
                    System.out.println(user.name);
                    iterator.remove();
                } else if (!iterator.hasNext())
                {
                    n = totalMoney;
                }
            } else
            {
                n = totalMoney;
                iterator = users.iterator();
            }
        }
    }
}

发表于 2018-07-20 15:28:35 回复(0)
#include <iostream>
#include<vector>
#include<string>
using namespace std;
typedef pair<string, int> PAIR;

int main()
{		
	int length, num;
	while (cin >> length >> num)
	{
		vector<PAIR> data;
		vector<string> res;
		PAIR p;
		for (int i = 0; i < length; i++)
		{
			cin >> p.first >> p.second;
			data.push_back(p);
		}
		int cnt = 0, sum;		
		while (cnt != length)
		{
			sum = num;
			for (vector<PAIR>::iterator it = data.begin(); it != data.end();it++)
			{
				if (it->second != 0 && it->second <= sum)
				{
					res.push_back(it->first);
					sum -= it->second;
					it->second = 0;
					cnt++;
				}
			}
		}
		for (vector<string>::iterator k = res.begin(); k != res.end(); k++)
			cout << *k << endl;
		cout << endl;
	}
	return 0;
}

发表于 2017-04-27 14:05:42 回复(0)
#include <iostream>
#include <string>
using namespace std;
struct man
{
    int money;
    string name;
};
int main()
{
    int n,m;//m个人贷款,银行共有n万元款
    while(cin >> m >> n)
    {
        struct man ask[50],su[50];//请求贷款者数组和成功贷款这数组
        int flag[50];
        for(int i = 0; i < m; i++)
        {
            cin >> ask[i].name >> ask[i].money;
            flag[i] = 0;//设置一个标志数组,用来标识银行是否已经给贷过款
        }
        int k = 0;
        for(int i = 0; i < m; i++)
        {
            int sum = 0;
            for(int j = 0 ; j < m; j++)
            {
                if(!flag[j])//如果没有贷过款才进行和其前面的人带的款算加法
                {
                    if(sum  + ask[j].money <= n)//如果几个人的贷款总金额小于最大限度
                    {
                        sum += ask[j].money;
                        su[k].money = ask[j].money;//给成功贷款的人赋值
                        su[k].name = ask[j].name;
                        flag[j] = 1;//已经贷过款
                        k++;
                    }
                }
            }
        }
        for(int i = 0; i < m; i++)
        {
            cout << su[i].name << endl;
        }
        cout << endl;
    }
    return 0;
}

发表于 2016-10-22 18:56:06 回复(0)
一开始用插入的方法老是超时。。。万般无奈之中想到了用两个队列来做。。。
#include <iostream>
#include <string>
#include <queue>
using namespace std;

void application(queue<pair<string, int> > que[2], int n)
{
	int swh = 0, total = n;  // swh是两个队列切换的标记。 while (!que[swh].empty() || !que[!swh].empty())
	{
		total = n;
		while (!que[swh].empty())
		{
			if (total >= que[swh].front().second)
			{
				total -= que[swh].front().second;
				cout << que[swh].front().first << endl;
			}
			else que[!swh].push(que[swh].front());
			que[swh].pop();
		}
		swh = !swh;
	}
}


int main()
{
	int m, n;
	while (cin >> m >> n && (m || n))
	{
		queue<pair<string, int> > que[2];
		string name;
		int apply;
		for (int i = 0; i < m; ++i)
		{
			cin >> name >> apply;
			que[0].push(pair<string, int>(name, apply));
		}
		
		application(que, n);
		
		cout << endl;
	}
	return 0;
}

发表于 2015-12-18 23:02:23 回复(0)