一共有几个普通文件

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

下面的 FS 描述了一棵文件树,出现在键里的是目录,其余是文件。运行这段程序:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

// 出现在键里的是目录,其余是文件
map<string, vector<string>> FS = {
    {"根目录", {"文档", "代码", "说明.md"}},
    {"文档", {"计划.md", "会议.md"}},
    {"代码", {"main.py", "工具"}},
    {"工具", {"helper.py"}},
};

bool is_dir(const string& name) {
    return FS.count(name) > 0;
}

int files(const string& name) {
    if (!is_dir(name)) return 1;
    int n = 0;
    for (const string& child : FS[name]) n += files(child);
    return n;
}

int main() {
    cout << files("根目录") << endl;
}

(本题用 g++ -std=c++17 -O0 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论