본문 바로가기

UVa. 112, PKU 1145. Tree Summing. AC get! main() { int st[10000], len, i, sum, lev, cp, f; char temp; while (scanf("%d", &len) != -1) { lev = 0; sum = 0; f = 0; while (1) { while ((temp = getchar()) != '(') { if (temp == ')') { f++; if (f == 4) { if (sum == len) { cp = 1; } } sum -= st[lev--]; if (!lev) { goto a; } } } lev++; f++; if (scanf("%d", &i)) { st[lev] = i; sum += i; f = 0; continue; } else { st[lev] = 0; while (getchar() != ').. 더보기
UVa. 112, PKU 1145. Tree Summing. 트리 합계?! 배경지식 LISP는 FORTRAN과 함께 최초의 고수준 프로그래밍 언어중 하나로, 현재까지 쓰이는 언어중 가장 오래된 것중 하나이다. 리스트나 트리 같은 LISP의 트리 구조는 현재까지도 쉽게 채용되어 잘 사용되고 있다. 이 문제는 LISP의 S-표현식의 방식에 근거해 이진 트리를 읽어들이는 문제와 관련이 있다. 문제 정수로 구성된 이진 트리를 입력받아서 루트-리프의 루틴중 원소들의 합이 가장 큰 루틴을 찾아내는 프로그램을 기술하라. 아래 예시에서는 각 루틴의 값이 27, 22, 26 18이다. 이진트리는 LISP의 S-표현식에서 다음과 같이 구성한다. 빈 트리 ::= () 트리 ::= 빈 트리 (정수 트리 트리) 위 예시의 트리 다이어그램을 표현식에 맞추어 바꾸면 이와 같다. (5 (4 .. 더보기
UVa. 112, PKU 1145. Tree Summing. Tree Summing Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, which are the fundamental data structures in LISP, can easily be adapted to represent other important data structures such as trees. This problem deals with determining whether binary trees represented as LISP S-expressions possess a.. 더보기