一共有几个普通文件
下面的 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 编译。)
全部评论