四大文化赛道完整展开
06-deliverables/appendix-code.md
丝路商账核验:库存流水审计与异常定位 代码附录
站内文件视图直接读取仓库内容,Markdown 使用文档排版渲染,其余文本文件保持原始排版,方便校对训练证据链。
文件类型Markdown
10-cases/s3-jh-07-ledger-audit/06-deliverables/appendix-code.md
- 完整解题档案:complete-solution-dossier.md
Python 主实现
源文件:main.py
- 实现状态:当前已有可执行实现
import sys
def solve(data: str) -> str:
tokens = data.split()
if not tokens:
return ""
it = iter(tokens)
n = int(next(it))
stock = {}
turnover = 0
first_error = 0
for line_no in range(1, n + 1):
name = next(it)
op = next(it)
qty = int(next(it))
price = int(next(it))
current = stock.get(name, 0)
if op == "IN":
stock[name] = current + qty
continue
if qty > current:
if first_error == 0:
first_error = line_no
continue
stock[name] = current - qty
turnover += qty * price
remain = [f"{name}:{stock[name]}" for name in sorted(stock) if stock[name] > 0]
stock_text = ",".join(remain) if remain else "EMPTY"
return "\n".join(
[
f"turnover={turnover}",
f"first_error={first_error}",
f"stock={stock_text}",
]
)
if __name__ == "__main__":
sys.stdout.write(solve(sys.stdin.read()).strip())
sys.stdout.write("\n")
C++ 对照实现
源文件:main.cpp
- 实现状态:当前已有可执行实现
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) {
return 0;
}
map<string, long long> stock;
long long turnover = 0;
int first_error = 0;
for (int line_no = 1; line_no <= n; ++line_no) {
string name, op;
long long qty, price;
cin >> name >> op >> qty >> price;
long long current = stock[name];
if (op == "IN") {
stock[name] = current + qty;
continue;
}
if (qty > current) {
if (first_error == 0) {
first_error = line_no;
}
continue;
}
stock[name] = current - qty;
turnover += qty * price;
}
cout << "turnover=" << turnover << "\n";
cout << "first_error=" << first_error << "\n";
cout << "stock=";
bool first = true;
for (const auto& entry : stock) {
if (entry.second <= 0) {
continue;
}
if (!first) {
cout << ',';
}
first = false;
cout << entry.first << ':' << entry.second;
}
if (first) {
cout << "EMPTY";
}
cout << "\n";
return 0;
}