hyunjin

[SWEA 중위순회] 입력 버리기 , getline(cin,str) , getc(stdin) 본문

알고리즘 연습

[SWEA 중위순회] 입력 버리기 , getline(cin,str) , getc(stdin)

_h.j 2024. 7. 25. 10:49
728x90

문제

 

 

 

#include<iostream>
#include <string>
#include <cstring>

using namespace std;

int N;
char tree[102];

void inorder(int id) {
	if (!tree[id] || id > N) 
		return;
	inorder(id * 2);
	cout << tree[id];
	inorder(id * 2 + 1);
}

int main(int argc, char** argv)
{
	int T = 10;
	//cin >> T;

	for (int test_case = 1; test_case <= T; ++test_case)
	{
		memset(tree, 0, sizeof(tree));
		cin >> N;
		for (int i = 1; i <= N;i++) {
			int id; char c;
			cin >> id >> c;
			tree[id] = c;
			
			//방법1
			string str;
			getline(cin, str);

			// 방법2
			// int tmp;
			//while (getc(stdin) == ' ')
			//	cin >> tmp;
		}
		cout << "#" << test_case << " ";
		inorder(1);
		cout << "\n";
	}
	return 0;
}

 

728x90

'알고리즘 연습' 카테고리의 다른 글

최대공약수(GCD), 최소공배수(LCM)  (0) 2024.04.25