这题目挺有意思的,就帮你写了
我感觉这个代码是没问题了,加入更多的规则测试一下
如果之后会出现一个物品有多个配方的话,那就再加一个配方选优函数,选优规则你自己决定
另外,顺便问个问题,在python中有没有办法用这种全局的operator ?
result = result + base(items[i].first) * items[i].second;
我现在在python中就是定义两个函数add和mul,然后
result = add(result, mul(base(j[0]), j[1]))
有没有办法写这样的代码?
result = result + base(j[0]) * j[1]
为了问这个问题,我特意写了个C++代码,^_^
sh-5.2$ ls
main main.cpp main.py str.txt
sh-5.2$ cat main.py
#!/usr/bin/env python
#coding=utf-8
data = (
('炼金引擎', ('木板', 4), ('石砖', 2), ('金子', 6)),
('木板', ('木头', 4)),
('石砖', ('石头', 3)),
('x', ('炼金引擎', 3), ('木板', 4), ('金子', 5)) # test
)
def find(list_, key):
for i in list_:
if i[0] != key: continue
return list_.index(i)
return -1
def add(a, b):
for i in b:
index = find(a, i[0])
if index == -1: a.append(i)
else: a[index][1] += i[1]
return a
def mul(a, b):
for i in a: i[1] *= b
return a
def base(name):
for i in data:
if i[0] != name: continue
result = []
for j in i[1:]: result = add(result, mul(base(j[0]), j[1]))
return result
return [[name, 1]]
print(base('炼金引擎'))
print(base('x'))
print(base('木头'))
print(base('电磁铁'))
sh-5.2$ ./main.py
[['木头', 16], ['石头', 6], ['金子', 6]]
[['木头', 64], ['石头', 18], ['金子', 23]]
[['木头', 1]]
[['电磁铁', 1]]
sh-5.2$ cat main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <utility>
using std::cout, std::endl, std::ostream;
using std::vector, std::string;
using std::pair;
using item_t = pair<string, size_t>;
using items_t = vector<item_t>;
using rules_t = vector<pair<string, items_t>>;
const rules_t rules = {
{"炼金引擎", {{"木板", 4}, {"石砖", 2}, {"金子", 6}}},
{"木板", {{"木头", 4}}},
{"石砖", {{"石头", 3}}},
{"x", {{"炼金引擎", 3}, {"木板", 4}, {"金子", 5}}} // test
};
size_t find(const items_t &items, const string &key) {
for(size_t i = 0; i < items.size(); ++i) {
if(items[i].first == key) return i;
}
return -1;
}
const items_t operator+(const items_t &a, const items_t &b) {
items_t items = a;
for(const auto &i: b) {
size_t index = find(items, i.first);
if(index == (size_t)-1) items.push_back(i);
else items[index].second += i.second;
}
return items;
}
const items_t operator*(const items_t &items, size_t size) {
items_t new_items = items;
for(auto &i: new_items) i.second *= size;
return new_items;
}
ostream &operator<<(ostream &os, const item_t &item) {
os << "[" << item.first << ", " << item.second << "]";
return os;
}
ostream &operator<<(ostream &os, const items_t &items) {
bool flag = false;
os << "[";
for(const auto &i: items) {
if(flag) os << ", ";
flag = true; os << i;
}
os << "]";
return os;
}
const items_t base(const string &name) {
for(size_t i = 0; i < rules.size(); ++i) {
if(rules[i].first != name) continue;
const items_t &items = rules[i].second;
items_t result;
for(size_t i = 0; i < items.size(); ++i) {
result = result + base(items[i].first) * items[i].second;
}
return result;
}
return {{name, 1}};
}
int main() {
cout << base("炼金引擎") << endl;
cout << base("x") << endl;
cout << base("木头") << endl;
cout << base("电磁铁") << endl;
return 0;
}
sh-5.2$ ./main
[[木头, 16], [石头, 6], [金子, 6]]
[[木头, 64], [石头, 18], [金子, 23]]
[[木头, 1]]
[[电磁铁, 1]]
sh-5.2$