ACM/ICPC 之 递归(POJ2663-完全覆盖+POJ1057(百练2775)-旧式文件结构图)
POJ2663-完全覆盖
题解见首注释
//简单递推-三个米诺牌(3*2)为一个单位打草稿得出规律 //题意-3*n块方格能被1*2的米诺牌以多少种情况完全覆盖 //Memory 132K Time: 0 Ms #include<iostream> #include<cstring> #include<cstdio> using namespace std; int ans; //开始平铺 int Tiling(int n) { int sum = 0; if (n == 0) return 1; else if (n >= 2) { sum += 3 * Tiling(n - 2); while (n -= 2, n - 2 >= 0) sum += 2*Tiling(n-2); } return sum; } int main() { int n; while (scanf("%d",&n),n != -1) { ans = Tiling(n); printf("%d\n", ans); } return 0; }
POJ1057(百练2775)-旧式文件结构图
非递归版-注意记录状态
//旧式shell文件结构图模拟-分析文件(可递归) //Memory: 148K Time: 0 Ms #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define MAX 62 char buff[MAX][MAX]; /* File name */ struct File{ char f[MAX]; }; /* Folder property */ struct Folder{ int catalog; int num; File f[MAX]; }dir[MAX]; /* sort_cmp */ bool cmp(File a,File b) { return strcmp(a.f, b.f) < 0; } void open_Folder(int cur,int num) //cur-当前文件目录,num-文件数 { int i, j; sort(dir[cur].f, dir[cur].f + num, cmp); for (i = 0; i < num; i++) { for (j = 0; j < dir[cur].catalog; j++) printf("| "); printf("%s\n", dir[cur].f[i].f); } } int main() { int T, n; int i, j, k; T = 0; while (scanf("%s", buff[0]), buff[0][0] != '#') { /*Reading all*/ n = 0; k = 0; while (scanf("%s", buff[++n]), buff[n][0] != '*'); printf("DATA SET %d:\n",++T); printf("ROOT\n"); dir[0].num = 0; //Init for (i = 0; i <= n; i++) { if (buff[i][0] == 'd') //Folder { k++; dir[k].catalog = dir[k-1].catalog+1; dir[k].num = 0; for (j = 0; j < dir[k].catalog; j++) printf("| "); printf("%s\n", buff[i]); } else if (buff[i][0] == 'f') //File strcpy(dir[k].f[dir[k].num++].f, buff[i]); else if (buff[i][0] == ']') //end_Folder { open_Folder(k, dir[k].num); k--; } else if (buff[i][0] == '*') //end_root open_Folder(k, dir[k].num); } printf("\n"); } return 0; }
递归版-用形参记录状态
-PS:这个版本是本文发表当天写的,所以和上面版本(前一年做过的)的习惯和极值有些差异。
//递归模拟 //Memory:184K Time:0Ms #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAX 31 int T; //当前为第T个样例 char cur[MAX]; //当前输入字符串 //FILE *out = fopen("out.txt", "w"); struct File { char name[MAX]; friend bool operator < (File a, File b) { if (strcmp(a.name, b.name) < 0) return true; else return false; } }; struct Catalog { char name[MAX]; int num; //该目录下的文件数 File f[MAX]; Catalog(char *name) { strcpy(this->name, name); num = 0; } }; void open(char *name,int level) { if (name[0] == 'R') //ROOT前输入 printf("DATA SET %d:\n", ++T); //fprintf(out, "\nDATA SET %d:\n", ++T); while (level--) printf("| "); //fprintf(out,"| "); printf("%s\n", name); //fprintf(out, "%s\n", name); } void root(Catalog dir,int level) { scanf("%s", cur); if (cur[0] == '#') return; //END open(dir.name, level); do{ if (cur[0] == 'd') { Catalog newDir(cur); //创建新文件夹,命名为cur root(newDir, level + 1); } else if(cur[0] == 'f') strcpy(dir.f[dir.num++].name, cur); else { //cur[0] == ']' or cur[0] == '*' sort(dir.f, dir.f + dir.num); for (int i = 0; i < dir.num; i++) open(dir.f[i].name, level); return; } }while (scanf("%s", cur)); } int main() { while (cur[0] != '#') { Catalog r("ROOT"); //创建根目录 root(r,0); printf("\n"); } return 0; }
他坐在湖边,望向天空,她坐在对岸,盯着湖面