#CS40902. 阅读程序9-树和图2

阅读程序9-树和图2

阅读程序

注意:切勿用电脑直接运行代码得出答案,请用大脑+笔+纸运行代码答题,否则是在浪费你的时间。

第9节:树和图

第2题【NOIP】2008

#include<iostream>
#include<cstring>
using namespace std;
#define MAX 100
void solve(char first[], int spos_f, int epos_f, char mid[], int spos_m, int epos_m) {
	int i, root_m;
07	if(spos_f > epos_f)
08		return;
	for(i = spos_m; i <= epos_m; i++)
		if(first[spos_f] == mid[i]) {
			root_m = i;
			break;
		}
	solve(first, spos_f + 1, spos_f + (root_m - spos_m), mid, spos_m, root_m - 1);
	solve(first, spos_f + (root_m - spos_m) + 1, epos_f, mid, root_m + 1, epos_m);
	cout << first[spos_f];
17 }
18 int main() {
19	char first[MAX], mid[MAX];
	int len;
	cin >> len;
	cin >> first >> mid;
	solve(first, 0, len - 1, mid , 0, len - 1);
	cout << endl;
	return 0;
}

●判断题

(1)将19行移到17行和18行之间,程序不会出错。

{{ select(2-1) }}

  • 正确
  • 错误

(2)将07行和08行去掉,程序可以得出相同的结果

{{ select(2-2) }}

  • 正确
  • 错误

(3)该程序的时间复杂度为O(n)

{{ select(2-3) }}

  • 正确
  • 错误

(4)将19行的char改为int类型,程序可以得到相同的结果

{{ select(2-4) }}

  • 正确
  • 错误

●选择题

(5)输人为

7
ABDCEGF
BDAGECF

时,输出为()。

{{ select(2-5) }}

  • DBGEFCA
  • DRGFECA
  • GBDEFCA
  • ABCDEFG

(6)该程序要解决的间题是()。

{{ select(2-6) }}

  • 给出先序遍历和后序通历求中序遍历
  • 给出先序遍历和中序通历求后序遍历
  • 给出中序通历和后序遍历求前序遍历
  • 给出前序遍历和中序遍历求层序遍历