8-24 stone.c
1 #include <stdio.h> 2 unsigned int *heap; //保存各堆石子的数量 3 int num; 4 void Output() //显示各堆石子的状态 5 { 6 int i; 7 printf("各堆剩下的石子数量:\n"); 8 for (i = 1; i <= num; i++) 9 printf("第%2d堆剩下: %u \n", i, heap[i]); 10 } 11 unsigned int remain() //统计剩下的石子总数 12 { 13 unsigned int s; 14 int i; 15 s = 0; 16 for (i = 1; i <= num; i++) 17 s += heap[i]; 18 return s; 19 } 20 unsigned int xorall() //按位异或的结果 21 { 22 unsigned int s; 23 int i; 24 s = 0; 25 for (i = 1; i <= num; i++) 26 s ^= heap[i]; 27 return s; 28 } 29 int main() 30 { 31 unsigned int t; 32 int i, s, h; 33 printf("输入石子的堆数:"); 34 scanf("%d", &num); 35 if(num<2) 36 { 37 printf("至少应该有2堆石子!\n"); 38 exit(0); 39 } 40 if(!(heap=(unsigned int *)malloc(sizeof(unsigned int)*num))) 41 { 42 printf("内存分配失败!\n"); 43 exit(0); 44 } 45 for (i = 1; i <= num; i++) 46 { 47 printf("输入第%d堆石子的数量:", i); 48 scanf("%d", &heap[i]); 49 } 50 while (remain()) //剩余石子数大于0 51 { 52 if (xorall() == 0) //若异或的结果为0 53 { 54 for (i = 1; i <= num; i++) //从一堆石子中取一粒 55 { 56 if (heap[i] > 0) 57 { 58 printf("\n计算机从第%2d堆中拿1粒。\n", i); 59 heap[i]--; 60 break; 61 } 62 } 63 } 64 else 65 { 66 for (i = 1; i <= num; i++) 67 { 68 s = heap[i] - (xorall() ^ heap[i]); //计算要取的石子数量 69 if (s > 0) 70 { 71 printf("\n计算机从第%2d堆中拿%u粒。\n", i, s); 72 heap[i] ^= xorall(); 73 break; 74 } 75 } 76 } 77 if (remain() == 0) 78 { 79 printf("\n计算机胜!"); 80 break; 81 } 82 Output(); //显示剩余的石堆情况 83 while (1) 84 { 85 printf("\n输入你的选择(堆 数量):"); 86 scanf("%d %u", &h, &t); 87 if ((h >= 1) && (h <= num) && (heap[h] >= t)) 88 { 89 heap[h] -= t; 90 break; 91 } else 92 printf("\n输入数据出错,重新输入!\n"); 93 } 94 if (remain() == 0) 95 { 96 printf("\n恭喜你获胜!"); 97 break; 98 } 99 } 100 getch(); 101 return 0; 102 }
8-25 gameoflife.c
1 #include <stdio.h> 2 #define MAXROW 10 //最大行数 3 #define MAXCOL 10 //最大列数 4 #define DEAD 0 //死细胞 5 #define ALIVE 1 //活细胞 6 int cell[MAXROW][MAXCOL], tempcell[MAXROW][MAXCOL]; //当前生命细胞的状态,一个用于判断当前的细胞的下一个状态 7 void init(); //初始化细胞数组 8 int BorderSum(int, int); //统计当前细胞四周的细胞数 9 void Output(); //输出细胞状态 10 int main() 11 { 12 int row, col,sum1; 13 char again; 14 init(); //初始化,输入细胞的位置 15 Output(); //输出细胞初始状态 16 printf("按任意键进行转换。\n"); 17 getch(); 18 while (1) 19 { 20 for (row = 0; row < MAXROW; row++) 21 { 22 for (col = 0; col < MAXCOL; col++) 23 { 24 switch (BorderSum(row, col)) //(row,col)单元格四周的生细胞数量 25 { 26 case 0: 27 case 1: 28 case 4: 29 case 5: 30 case 6: 31 case 7: 32 case 8: 33 tempcell[row][col] = DEAD; //将原细胞改为死细胞 34 break; 35 case 2: 36 tempcell[row][col] = cell[row][col]; //保持细胞原样 37 break; 38 case 3: 39 tempcell[row][col] = ALIVE; //将细胞改为活细胞 40 break; 41 } 42 } 43 } 44 for (row = 0; row < MAXROW; row++)//复制临时数组到map数组 45 for (col = 0; col < MAXCOL; col++) 46 cell[row][col] = tempcell[row][col]; 47 sum1=AliveSum(); 48 if(sum1==0) //若活细胞数量为0 49 break;//跳出循环 50 Output(); //输出转换后的细胞状态 51 printf("共有%d个活细胞。\n",sum1); 52 printf("\n继续生成下一次细胞的状态(n退出)?"); 53 again = getch(); 54 if (again == ‘N‘ || again == ‘n‘) 55 break; 56 } 57 getch(); 58 return 0; 59 } 60 void init() //初始化 61 { 62 int row, col; 63 for (row = 0; row < MAXROW; row++) //先全部初始化为死状态 64 for (col = 0; col < MAXCOL; col++) 65 cell[row][col] = DEAD; 66 printf("生命游戏\n"); 67 printf("输入活细胞的坐标位置,输入(-1 -1)结束:\n"); 68 while (1) 69 { 70 scanf("%d %d", &row, &col); //输入行列坐标 71 if (0 <= row && row < MAXROW && 0 <= col && col < MAXCOL) 72 cell[row][col] = ALIVE; //保存生细胞 73 else if (row == -1 || col == -1) 74 break; 75 else 76 printf("输入坐标超过范围。\n"); 77 } 78 } 79 int BorderSum(int row, int col) //统计四周细胞数量 80 { 81 int count = 0, c, r; 82 for (r = row - 1; r <= row + 1; r++) 83 { 84 for (c = col - 1; c <= col + 1; c++) 85 { 86 if (r < 0 || r >= MAXROW || c < 0 || c >= MAXCOL) //超过数组界限 87 continue; //处理下一个单元格 88 if (cell[r][c] == ALIVE) //若为活细胞 89 count++; //增加活细胞的数量 90 } 91 } 92 if (cell[row][col] == ALIVE) //若当前单元格为活细胞 93 count--; //活细胞总数减1 94 return count; //返回四周活细胞总数 95 } 96 void Output() //输出细胞状态 97 { 98 int row, col; 99 printf("\n细胞状态\n"); 100 printf("┌"); //输出项部横线 101 for (col = 0; col < MAXCOL -1; col++) //输出一行 102 printf("─┬"); 103 printf("─┐\n"); 104 for (row = 0; row < MAXROW; row++) 105 { 106 printf("│"); //输出行号 107 for (col = 0; col < MAXCOL; col++) //输出棋盘各单元格中棋子的状态 108 { 109 if (cell[row][col] == ALIVE ) //活细胞 110 printf("●│"); 111 else //死细胞 112 printf("○│"); 113 } 114 printf("\n"); 115 116 if (row < MAXROW - 1) 117 { 118 printf("├"); //输出交叉线 119 for (col = 0; col < MAXCOL - 1; col++) //输出一行 120 printf("─┼"); 121 printf("─┤\n"); 122 } 123 } 124 printf("└"); 125 for (col = 0; col < MAXCOL - 1; col++) //最后一行的横线 126 printf("─┴"); 127 printf("─┘\n"); 128 } 129 int AliveSum() //统计生细胞的数量 130 { 131 int row,col,count=0; 132 for (row = 0; row < MAXROW; row++) 133 { 134 for (col = 0; col < MAXCOL; col++) 135 { 136 if(cell[row][col] == ALIVE) //若是活细胞 137 count++; //累加数量 138 } 139 } 140 return count; 141 }
时间: 2024-10-11 00:22:23