Position BinarySearch( List L, ElementType X ){ int left = 1, right = L->Last, mid; while(left <= right){ mid = (left + right) / 2; if (X < L->Data[mid]) right = mid - 1; //updata right elseif (X > L->Data[mid]) left = mid + 1; //updata left elsereturn mid; // if (X == Data[mid]) } return NotFound; //when left > right, it is malposed that meant not find 'X', so return NotFound }
Preorder: 5 2 1 0 4 8 6 7 10 9 6 is found 3 is not found 10 is found 10 is the largest key 0 is found 0 is the smallest key 5 is found Not Found Inorder: 1 2 4 6 8 9结尾无空行
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
voidPrintPoly(Polynomial P){ int flag = 0; if (!P){printf("0 0\n"); return;} while (P){ if (!flag) //调整输出格式,使第一项输出头部无空格 flag = 1; else printf(" ");//使第二项输出头部、第三项输出头部...有空格 printf("%d %d",P->coef, P->expon); P = P->link; //输出下一个结点,我找了它接近两个小时,写的时候漏了 } printf("\n"); }
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
intmain(){ int Data[MaxSize]; int Next[MaxSize]; int List[MaxSize]; //List[]用来存放当前结点的地址,其索引是序号0 ~ 100005 int FirstAdd, N, K, i ,j, t; scanf("%d %d %d", &FirstAdd, &N, &K);
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.
#define MaxTree 10 #define ElementType char #define Tree int #define Null -1
structTreeNode{ ElementType Element; Tree Left; Tree Right; } T1[MaxTree], T2[MaxTree];
Tree BuildTree( struct TreeNode T[]); intIsomorphic(Tree R1,Tree R2);
intmain(){ Tree R1, R2; R1 = BuildTree(T1); R2 = BuildTree(T2); if (Isomorphic(R1, R2)) printf("Yes\n"); else printf("No\n"); return0; }
Tree BuildTree( struct TreeNode T[] ){ int N, i; int check[MaxTree]; Tree Root = Null; //Root set Null, because line 34 and line 55, if N = 0, return Null ElementType cl, cr; scanf("%d", &N); if (N) { for (i=0; i<N; i++) check[i] = 0; for (i=0; i<N; i++) { scanf("\n%c %c %c", &T[i].Element, &cl, &cr); if (cl != '-') { // four ‘if’ are at the same level T[i].Left = cl - '0'; check[T[i].Left] = 1; } if (cl == '-') T[i].Left = Null; if (cr != '-') { T[i].Right = cr-'0'; check[T[i].Right] = 1; } if (cr == '-') T[i].Right = Null; } for (i=0; i<N; i++) if (!check[i]) break; Root = i; } return Root; }
intIsomorphic( Tree R1, Tree R2 ){ if ( (R1==Null )&& (R2==Null) ) return1;/* both empty */ if ( ((R1==Null)&&(R2!=Null)) || ((R1!=Null)&&(R2==Null)) ) return0; /* one of them is empty */ if ( T1[R1].Element != T2[R2].Element ) return0; /* roots Element are different */ if ( ( T1[R1].Left == Null )&&( T2[R2].Left == Null ) ) return Isomorphic( T1[R1].Right, T2[R2].Right );/* both have no left sontree */ if ( ((T1[R1].Left!=Null)&&(T2[R2].Left!=Null)) && ((T1[T1[R1].Left].Element)==(T2[T2[R2].Left].Element)) ) /* no need to swap the left and the right */ /*If the left son of both trees is not empty and the Element is the same, recurse on the left son*/ /* 如果两棵树左儿子都不为空并且元素还是一样的,对左儿子进行递归*/ return ( Isomorphic( T1[R1].Left, T2[R2].Left ) && Isomorphic( T1[R1].Right, T2[R2].Right ) ); else/* need to swap the left and the right */ /*If two trees have different left sons (one is empty and neither is empty or neither is empty), then determine whether the left (right) son of the first tree is isomorphic to the right (left) son of the second tree*/ /* 如果两棵树左儿子(一个空一个不空或者都不空)并且元素不一样, 那么判断第一棵树的左(右)儿子是否跟第二棵树的右(左)儿子同构 */ return ( Isomorphic( T1[R1].Left, T2[R2].Right) && Isomorphic( T1[R1].Right, T2[R2].Left ) ); }
typedefstructTreeNode *Tree; structTreeNode { int v; Tree Left, Right; int flag; };
Tree MakeTree( int N ); Tree Insert( Tree T, int V ); Tree NewNode( int V ); intcheck( Tree T, int V ); intJudge( Tree T, int N ); voidResetT( Tree T ); voidFreeTree( Tree T );
intmain(){ int N, L, i; Tree T; scanf("%d", &N); while (N) { //N = 0时结束循环 scanf("%d", &L); T = MakeTree(N); for (i = 0; i < L; i++) { if (Judge(T, N)) printf("Yes\n"); elseprintf("No\n"); ResetT(T); //清除T中的标记flag,用以判断下一课搜索二叉树 } FreeTree(T); scanf("%d", &N); } return0; }
//制造树,先建个结点,再对其插入即可 Tree MakeTree( int N ){ Tree T; int i, V; scanf("%d", &V); T = NewNode(V); for (i=1; i<N; i++) { scanf("%d", &V); T = Insert(T, V); } return T; }
//插入时利用二叉搜索树左小右大的特点 Tree Insert( Tree T, int V ){ if ( !T ) //如果要该树是空的,要创建值为 V新结点 T = NewNode(V); else { if ( V > T->v ) //如果该树的值小于插入值 V,递归到该树的右子树插入 T->Right = Insert( T->Right, V ); else//反之,递归到该树的左子树插入 T->Left = Insert( T->Left, V ); } return T; }
//创建一个值为 V新结点 Tree NewNode( int V ){ Tree T = (Tree)malloc(sizeof(struct TreeNode)); T->v = V; T->Left = T->Right = NULL; T->flag = 0; //标志flag初始化为0 return T; }
//利用二叉搜索树左小右大的特点检查结点值是否相等 //此时的 T是对照二叉树 intcheck( Tree T, int V ){
//标志flag存在时(即flag置1时),说明要检查的值已经满足检查,无需再检查,跳过它,去检查它的左或右子树 if ( T->flag ) { if ( V < T->v ) return check(T->Left, V); elseif ( V > T->v ) return check(T->Right, V); elsereturn0; }
voidInsert( int X ){ /* 将X插入H。这里省略检查堆是否已满的代码 */ //因为这道题用不到,用了会出错 int i; //注意size是全局变量 //找到要插入的位置,从索引最小(即[1])的位置开始找(++size改变带动i改变),向下过滤 //为什么要i /= 2 //插入值和父结点[i/2]比较,如小于父节点,要改变父节点,如果还小于父节点的父节点,要继续向上过滤 for (i = ++size; H[i/2] > X; i /= 2) //因为size初始化为0,所以i = ++size = 1 H[i] = H[i/2]; //改变父结点的值 H[i] = X; //插入 }
//这段代码是自我调试用的 // int i = ++size; // int a = size; // while (H[i/2] > X){ // H[i] = H[i/2]; // i /= 2; // } // H[i] = X;
intmain(){ int n, m, x, i, j; scanf("%d %d", &n, &m); Create(); /* 堆初始化 */ for (i = 0; i < n; i++) { /*以逐个插入方式建堆 */ scanf("%d", &x); Insert(x); } for (i = 0; i < m; i++) { scanf("%d", &j); printf("%d", H[j]); while (j > 1) { /*沿根方向输出各结点*///向上折半逐个输出父结点即可 j /= 2; printf(" %d", H[j]); } printf("\n"); } return0; }
We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?
Input Specification:
Each input file contains one test case. For each test case, the first line contains N (2≤N≤104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:
1
I c1 c2
where I stands for inputting a connection between c1 and c2; or
1
C c1 c2
where C stands for checking if it is possible to transfer files between c1 and c2; or
1
S
where S stands for stopping this case.
Output Specification:
For each C case, print in one line the word “yes” or “no” if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line “The network is connected.” if there is a path between any pair of computers; or “There are k components.” where k is the number of connected components in this network.
Sample Input 1:
1 2 3 4 5 6 7 8
5 C 3 2 I 3 2 C 1 5 I 4 5 I 2 4 C 3 5 S
Sample Output 1:
1 2 3 4
no no yes There are 2 components.
Sample Input 2:
1 2 3 4 5 6 7 8 9 10
5 C 3 2 I 3 2 C 1 5 I 4 5 I 2 4 C 3 5 I 1 3 C 1 5 S
voidInitialization( SetType S, int n ); voidInput_connection( SetType S ); voidCheck_connection( SetType S ); voidCheck_network( SetType S, int n ); voidUnion( SetType S, SetName Root1, SetName Root2 ); SetName Find( SetType S, ElementType X );
voidInitialization( SetType S, int n ){ int i; for(i = 0; i < n; i++) S[i] = -1; }
voidCheck_network( SetType S, int n ){ int i, counter = 0; for (i=0; i<n; i++) { if ( S[i] < 0 ) counter++; } if ( counter == 1 ) printf("The network is connected.\n"); else printf("There are %d components.\n", counter); }