6.1:栈和队列;6.2:链表,看过跳过,不做记录。
6.3:二叉树:
6.3.1:小球下落679 - Dropping Balls
具体解释不写了,反正书里有。
//左边 k*2,右边 k*2+1 #include <stdio.h> #include <string.h> int open[2<<21]; int main() { int n, h, id, ans; while(scanf("%d", &n) != EOF) { if(n == -1) break; for(int i = 0; i < n; i++) { scanf("%d%d", &h, &id); memset(open, 0, sizeof(open)); for(int k = 0; k < id; k++) { ans = 1; for(int j = 0; j < h - 1; j++) { open[ans] = (1 + open[ans]) % 2; ans = (open[ans] == 1)? ans*2 : ans*2+1; } } printf("%d ",ans); } } return 0; }
//左边 k*2,右边 k*2+1 //直接模拟最后一个,当id奇数:id->id/2+1;偶数id->id/2 #include <stdio.h> #include <string.h> int main() { int n, h, id, ans; while(scanf("%d", &n) != EOF) { if(n == -1) break; for(int i = 0; i < n; i++) { scanf("%d%d", &h, &id); ans = 1; for(int j = 0; j < h - 1; j++) { ans = (id%2) ? ans*2 : ans*2+1; id = (id%2) ? id/2+1 :id/2; } printf("%d ",ans); } } return 0; }