/*所需头文件*/ #include<stdio.h> #include<stdlib.h> #include<time.h> #include<Windows.h> void guitu(void); main() { srand(time(NULL)); /*时间触发函数*/ guitu(); /*调用guitu函数*/ return 0; } void guitu() /*定义龟兔函数*/ { int j, count = 0,cou = 0, number; /*定义变量*/ char printfun[71]; while (1) { for (j = 0; j <70;j ++) { printfun[j] = ' '; printfun[70] = '|'; } number = rand() % 10 + 1; /*产生随机数*/ printf("\n"); if (number >= 1 && number <= 5) { count += 3; /*乌龟右移3格*/ if (count > 70) { count = 0; /*如超过70格,从头开始计数*/ } printfun[count] = 'G'; } if (number >= 6 && number <= 7) { count -= 6; /*乌龟左移6格*/ if (count < 0) { count = 0; /*如退到0,则从0重新计数*/ } printfun[count] = 'G'; } if (number >= 8 && number <= 10) { count += 1; /*乌龟右移1格*/ if (count > 70) { count = 0; /*如超过70格,从头开始计数*/ } printfun[count] = 'G'; } if (number >= 1 && number <= 2) { cou += 0; /*兔子不动*/ if (cou > 70) { cou = 0; } printfun[cou] = 'T'; } if (number >= 3 && number <= 4) { cou += 9; /*兔子右移9格*/ if (cou > 70) { cou = 0; /*如超过70格,从头开始计数*/ } printfun[cou] = 'T'; } if (cou == 5) { cou -= 12; /*兔子左移12格*/ if (cou < 0) { cou = 0; /*如退到0,则从0重新计数*/ } printfun[cou] = 'T'; } if (number > 6 && number <= 8) { cou += 1; /*兔子右移一格*/ if (cou < 70) { cou = 0; /*如超过70格,从头开始计数*/ } printfun[cou] = 'T'; } if (number >= 9 && number <= 10) { cou -= -2; /*兔子左移两格*/ if (cou < 0) { cou = 0; /*如退到0,则从0重新计数*/ } printfun[cou] = 'T'; } if (count == cou) /*同时到达*/ { printfun[count] = 'P'; } printf("%s\n",printfun); printf("\n"); if (count == 70) { printf("gui win !!!\n");/*乌龟胜出*/ break; } if (cou == 70) { printf("tui win !!!\n");/*兔子胜出*/ break; printf("ID: 2013914201\n"); } Sleep(1); } printf("ID: 2013914201\n"); }
总结:打了又改,改了又打,来来回回N次,真的很累。不过也从中知道,C语言程序是必须要小心谨慎的,一步错,全盘皆输,嗯,为了不再翻来覆去,努力改掉粗心!!!