エンコーディングセッション
3948 ワード
コンパイラ時のパラメータ入力の受信
int main(int argc, char **argv)
実行可能ファイルをa.outと呼び、a.out abc def 5を入力すると、argcは実行可能ファイルを含む3つのパラメータ、すなわちargc=4、argv=[a.out、abc、def、5を加算する実装ツリー&ファイルI/O
class Tree{
public:
int n; //노드 개수
vector <int> *c; // 이차원 가변 배열
void init(const char *_fileName){
File *input=fopen(_fileName,"r");
fscanf(input,"%d",&n);
c=new vector <int>[n];
for(int i=0;i<n;i++){
int tmp;
while(true){
fscanf(input,"%d",&tmp);
if(tmp==-1) break;
c[i].push_back(tmp);
}
}
fclose(input);
}
DFS、BFS実施
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
class Tree
{
public:
int n; //# of nodes
vector<int> *c;
void Init(const char *_fileName)
{
FILE *input = fopen(_fileName, "r");
fscanf(input, "%d", &n);
c = new vector<int>[n];
for (int i = 0; i < n; i++)
{
int tmp;
while (true)
{
fscanf(input, "%d", &tmp);
if (tmp == -1)
{
break;
}
c[i].push_back(tmp);
}
}
fclose(input);
}
};
/*
void DFS(Tree &_t) //reference in c++
void DFS(Tree *_t) //pointer in c/c++
{
_t.n; //reference
_t->n; //pointer
}
*/
void DFS(Tree &_t)
{
std::stack<int> s;
s.push(0);
while (!s.empty())
{
int x = s.top();
s.pop();
printf("%d ", x);
// for (int i = 0; i < _t.c[x].size(); i++)
for (int i = _t.c[x].size() - 1; i >= 0; i--)
{
s.push(_t.c[x][i]);
}
}
}
void BFS(Tree &_t)
{
std::queue<int> q;
q.push(0);
while (!q.empty())
{
int x = q.front();
q.pop();
printf("%d ", x);
for (int i = 0; i < _t.c[x].size(); i++)
{
q.push(_t.c[x][i]);
}
}
}
int main(int argc, char **argv)
{
Tree t;
t.Init(argv[1]);
printf("DFS Order:\n");
DFS(t);
printf("\nBFS Order:\n");
BFS(t);
return 1;
}
Reference
この問題について(エンコーディングセッション), 我々は、より多くの情報をここで見つけました https://velog.io/@djc06048/코딩세션1テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol