牛牛是一家商场的经理,为了进一步实现自动化,牛牛希望你能为商场书写一个程序以实现下述功能:
1. 记录仓库中某商品名称、售出一份的收益以及库存数量。
2. 按照顾客下单的顺序自动处理订单,并计算该单是否盈利;若某一订单的需求量大于库存量,则终止处理订单,并给进货处提示警告。
牛牛也知道,程序开发并不是一蹴而就的,但是,他想先看到一个简易化的功能,即:通过文件输入商品情况以及拟定的订单顺序,输出处理完订单后的总盈利或者提示库存不足的警告信息。
第一行输入两个正整数,依次代表库存商品种数,以及订单数量。
第到
行,每行输入一个字符串以及两个正整数
,依次代表该商品名称,售出一份的收益,以及库存数量。数据保证,这
个商品名均不相同。
最后行,按照拟定的订单顺序,一行输入一份待处理的订单,包括一个字符串以及一个整数
,代表该订单需要的商品名称以及需求数量。
如果能够顺利处理所有订单,则一行输出一个整数代表总盈利;否则输出,其中
代表依次处理到第
份订单时,库存不足。
3 2 apple 1 10 pear 1 6 bike 100 1 apple 10 bike 1
110
根据订单顺序,依次售出十个和一辆
,总收益
.
3 2 apple 1 10 pear 1 6 bike 100 1 apple 10 bike 2
-2
第二份订单中,需要两辆,但是商店库存中只有一辆,库存不足。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] params = br.readLine().split(" ");
int n = Integer.parseInt(params[0]), m = Integer.parseInt(params[1]);
HashMap<String, Item> wareHouse = new HashMap<>();
for(int i = 0; i < n; i++){
params = br.readLine().split(" ");
wareHouse.put(params[0], new Item(Integer.parseInt(params[1]), Integer.parseInt(params[2])));
}
int totalProfit = 0;
for(int i = 0; i < m; i++){
params = br.readLine().split(" ");
String name = params[0];
int needs = Integer.parseInt(params[1]);
if(!wareHouse.containsKey(name) || needs > wareHouse.get(name).count){
System.out.println(-(i + 1));
totalProfit = 0;
break;
}else{
totalProfit += wareHouse.get(name).profit * needs;
wareHouse.get(name).count -= needs;
}
}
if(totalProfit > 0){
System.out.println(totalProfit);
}
}
}
class Item {
public int count; // 库存
public int profit; // 收益
public Item(int profit, int count) {
this.profit = profit;
this.count = count;
}
} #include<iostream>
#include<unordered_map>
#include<string>
#include<utility>
using namespace std;
unordered_map<string, pair<int,int>>map;
int main()
{
int kind,orders;
cin>>kind>>orders;
int i=0,sum=0;
while(i<kind)
{
string tmp;
int tmp1,tmp2;
cin>>tmp>>tmp1>>tmp2;
map[tmp]=make_pair(tmp1, tmp2);
i++;
}
i=0;
while(i<orders)
{
string tmp;
int a;
cin>>tmp>>a;
if((map[tmp].second-a)<0)
{
map[tmp].second-=a;
cout<<-(i+1);
return 0;
}
sum+=a*map[tmp].first;
i++;
}
cout<<sum;
return 0;
} #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
//#define mod 998244353
#define mod 1000000007
#define ll long long
using namespace std;
const int N=1e6+5;
const double eps=1e-8;
int n,m,k;
void solve(){
cin>>n>>m;
map<string,int>mp1,mp2;
for(int i=0;i<n;i++){
string s;int x1,x2;
cin>>s>>x1>>x2;
mp1[s]=x1;mp2[s]=x2;
}
int sum=0;
for(int i=1;i<=m;i++){
string s;int x;
cin>>s>>x;
if(mp2[s]>=x){
sum+=x*mp1[s];
mp2[s]-=x;
}
else{
cout<<'-'<<i<<'\n';
return ;
}
}
cout<<sum<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cout<<fixed<<setprecision(10);
//int t;
//cin>>t;
//while(t--){
solve();
//}
return 0;
}