diff --git "a/C\350\257\255\350\250\200\345\255\246\344\271\240\347\254\224\350\256\260.md" "b/C\350\257\255\350\250\200\345\255\246\344\271\240\347\254\224\350\256\260.md" index 5f40c203..5c0d4d79 100755 --- "a/C\350\257\255\350\250\200\345\255\246\344\271\240\347\254\224\350\256\260.md" +++ "b/C\350\257\255\350\250\200\345\255\246\344\271\240\347\254\224\350\256\260.md" @@ -1,5 +1,10 @@ # C语言学习笔记 - -# 记录你学习过程中的所见所思!酸甜苦辣! - -# 看什么看! 赶紧填坑啊! \ No newline at end of file +***************************************************************** +scanf("%[^\n]", str)正则用法: +1 [^\n]表示一读入换行字符就结束读入。这个是scanf的正则用法。 +我们都知道scanf不能接收空格符,一接受到空格就结束读入,所以不能像gets()等函数一样接受一行字符串,但是使用%[^\n]就可以一直读数,直到碰到’\n’才结束读入 +2 那么如果scanf("%*[\n]")表示该输入项读入后不赋予任何变量,即scanf("%*[^\n]")表示跳过一行字符串。 +https://blog.csdn.net/weixin_43469047/article/details/83753526 +***************************************************************** +C语言中strand() rand() time()函数的简单介绍及获取随机数的方法: +https://blog.csdn.net/weixin_36820871/article/details/69831374 diff --git a/Goband/Content.h b/Goband/Content.h new file mode 100644 index 00000000..0b5e48f9 --- /dev/null +++ b/Goband/Content.h @@ -0,0 +1,732 @@ +#include +#include +#include +#include + + +int Board[15][15] = {0};//棋盘 +int PersonalStone; //1 黑棋 2 白棋 +int CursorX, CursorY; //光标位置 +int Count;//步数 +void DrawBoard(); //画棋盘 +char* Get(int x,int y); //辅助画棋盘 +int Put(); //玩家下棋 +int RunGame(); //五子棋运行 +void Initialize(); //初始化 +void Back(); //悔棋,每次仅能悔棋一步 + + +int PosValue[15][15] = { + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, + {0,1,2,2,2,2,2,2,2,2,2,2,2,1,0}, + {0,1,2,3,3,3,3,3,3,3,3,3,2,1,0}, + {0,1,2,3,4,4,4,4,4,4,4,3,2,1,0}, + {0,1,2,3,4,5,5,5,5,5,4,3,2,1,0}, + {0,1,2,3,4,5,6,6,6,5,4,3,2,1,0}, + {0,1,2,3,4,5,6,7,6,5,4,3,2,1,0}, + {0,1,2,3,4,5,6,6,6,5,4,3,2,1,0}, + {0,1,2,3,4,5,5,5,5,5,4,3,2,1,0}, + {0,1,2,3,4,4,4,4,4,4,4,3,2,1,0}, + {0,1,2,3,3,3,3,3,3,3,3,3,2,1,0}, + {0,1,2,2,2,2,2,2,2,2,2,2,2,1,0}, + {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} +};//棋子位置自身价值 +int HistoryHeuristic[15][15];//记录棋子位置自身价值 +int TypeRecord[15][15][4]; //棋盘一点所能构成的棋型 +int LineRecord[20]; //记录10种棋型 +int TypeCount[3][10];//存储棋子10种五子棋棋型对应的数量 +void ResetHistoryTable();//重置棋子自身价值 +int Result(int i,int j,int Count);//判断结果 +int Eveluate(int CurrentStone);//评分 +int AnalysisHorizon(int i,int j);//分析水平棋型 +int AnalysisVertical(int i,int j);//分析竖直棋型 +int AnalysisLeft(int i,int j);//分析左斜棋型 +int AnalysisRight(int i,int j);//分析右斜棋型 +int AnalysisLine(int Line[],int Num,int Pos);//分析给定行上某点及其周边的棋型 + + +struct move{ + int r; //棋子的行 + int c; //棋子的列 + int v; //棋子的价值 +}MoveList[10][225]; +int CreatePossibleMove( int depth,int Count); // 配合 α-β剪枝 +int MoveCount;//搜索时步数记录 +void Sort(int depth, int movecount); //价值排序 + +void ComputerDownStone(); //电脑下棋 +int AlphaBeta(int depth, int CurrentStone, int alpha, int beta);//α-β剪枝 +int SearchDepth; //搜索深度 +int row; //电脑行 +int column; //电脑列 +int ComputerStone; //电脑棋子颜色 + +int RunGame() +{ + Initialize(); + int Input; + int First; + int result; + printf("输入数字选择电脑难度:2.初级 4.高级"); + scanf("%d",&SearchDepth); + printf("输入数字选择先手:1.玩家先手 2.电脑先手 "); + scanf("%d",&First); + if(First == 1){ + PersonalStone = 1; + ComputerStone = 2; + while(1){ + DrawBoard(); + Input = getch(); + switch(Input){ + case 27: + exit(0); + break; + case 224: + { + Input = getch(); + switch(Input) + { + case 72: CursorX--;break; + case 80: CursorX++;break; + case 75: CursorY--;break; + case 77: CursorY++;break; + } + if(CursorX<0){CursorX=14;} + if(CursorX>BoardSize){CursorX=0;} + if(CursorY>BoardSize){CursorY=0;} + if(CursorY<0){CursorY=14;} + break; + } + case 32: + { + if(Put()){ + DrawBoard(); + result = Result(CursorX,CursorY,Count); + if(result == 1){ + MessageBox(0,TEXT("黑棋获得胜利"),TEXT("结果"),NULL); + exit(0); + } + else if(result == 2){ + MessageBox(0,TEXT("比赛平局"),TEXT("结果"),NULL); + } + ComputerDownStone(); + result = Result(row,column,Count); + if(result == 1){ + MessageBox(0,TEXT("白棋获得胜利"),TEXT("结果"),NULL); + exit(0); + } + else if(result == 2){ + MessageBox(0,TEXT("比赛平局"),TEXT("结果"),NULL); + } + } + break; + } + case 98: + { + Back(); + break; + } + + } + + } + } + else{ + PersonalStone = 2; + ComputerStone = 1; + DrawBoard(); + ComputerDownStone(); + while(1){ + DrawBoard(); + Input = getch(); + switch(Input){ + case 27: + exit(0); + break; + case 224: + { + Input = getch(); + switch(Input) + { + case 72: CursorX--;break; + case 80: CursorX++;break; + case 75: CursorY--;break; + case 77: CursorY++;break; + } + if(CursorX<0){CursorX=14;} + if(CursorX>BoardSize){CursorX=0;} + if(CursorY>BoardSize){CursorY=0;} + if(CursorY<0){CursorY=14;} + break; + } + case 32: + { + if(Put());{ + DrawBoard(); + result = Result(CursorX,CursorY,Count); + if(result == 1){ + MessageBox(0,TEXT("白棋获得胜利"),TEXT("结果"),NULL); + exit(0); + } + else if(result == 2){ + MessageBox(0,TEXT("比赛平局"),TEXT("结果"),NULL); + } + ComputerDownStone(); + result = Result(row,column,Count); + if(result == 1){ + MessageBox(0,TEXT("黑棋获得胜利"),TEXT("结果"),NULL); + exit(0); + } + else if(result == 2){ + MessageBox(0,TEXT("比赛平局"),TEXT("结果"),NULL); + } + + } + break; + } + + case 98: + { + Back(); + break; + } + + } + + } + } +} + +void Initialize() +{ + CursorX = BoardSize / 2; + CursorY = BoardSize / 2; + Count = 0; +} + +void DrawBoard() +{ + system("cls"); + for(int i = 0;i < BoardSize;i++){ + for(int j = 0;j < BoardSize;j++){ + printf(Get(i,j)); + } + printf("\n"); + printf("\n"); + } + printf("上下左右移动光标,空格下棋,'b'悔棋"); +} + +char* Get(int x,int y) +{ + if(Board[x][y] == 2) + return " ● "; + else if(Board[x][y] == 1) + return " ○ "; + else if(x == CursorX && y == CursorY) + return " ╬ "; + else if(x == 0&&y == 0) + return " ┌ "; + else if(x == BoardSize-1&&y == 0) + return " └ "; + else if(x == BoardSize-1&&y == BoardSize-1) + return " ┘ "; + else if(x == 0&&y == BoardSize-1) + return " ┐ "; + else if(x == 0) + return " ┬ "; + else if(x == BoardSize-1) + return " ┴ "; + else if(y == 0) + return " ├ "; + else if(y == BoardSize-1) + return " ┤ "; + else + return " ┼ "; +} + +int Put() +{ + if(Board[CursorX][CursorY] == 0) + { + Board[CursorX][CursorY] = PersonalStone; + return 1; + } + else + return 0; +} + +void Back() +{ + Board[CursorX][CursorY] = 0; + Board[row][column] = 0; + Count = Count - 2; + DrawBoard; +} + + + + +void ResetHistoryTable() +{ + for (int i = 0; i < 15; i++){ + for (int j = 0; j < 15; j++) { + HistoryHeuristic[i][j]= PosValue[i][j]; + } + } +} + +int Result(int i,int j,int Count) +{ + if (AnalysisHorizon( i, j) == 7 || AnalysisVertical( i, j) == 7 || AnalysisLeft(i, j) == 7 || AnalysisRight(i, j) == 7) + return 1; + if (Count == 225) return 2; + + return 3; + +} + +int Eveluate(int CurrentStone) +{ + for (int i = 0; i < 15; i++){ + for (int j = 0; j < 15; j++){ + for (int k = 0; k < 4; k++){ + TypeRecord[i][j][k] = 0; + + } + } + } + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 10; j++){ + TypeCount[i][j] = 0; + } + } + for (int i = 0; i < 15; i++) + { + for (int j = 0; j < 15; j++) + { + if (Board[i][j] != 0) + { + if (TypeRecord[i][j][0] == 0) AnalysisHorizon(i, j); + if (TypeRecord[i][j][1] == 0) AnalysisVertical(i, j); + if (TypeRecord[i][j][2] == 0) AnalysisLeft(i, j); + if (TypeRecord[i][j][3] == 0) AnalysisRight(i, j); + } + } + } + for (int i = 0; i < 15; i++) + { + for (int j = 0; j < 15; j++) + { + for (int k = 0; k < 4; k++) + { + if (Board[i][j] != 0 && TypeRecord[i][j][k] > 0) TypeCount[Board[i][j]][TypeRecord[i][j][k]]++; + } + } + } + if (TypeCount[CurrentStone][7] > 0) + { + return 9999;//五连 + } + if (TypeCount[3 - CurrentStone][7] > 0) + { + return -9999;//五连 + } + for (int i = 1; i <= 2; i++) if (TypeCount[i][3] > 1) TypeCount[i][6]++; + + if (TypeCount[CurrentStone][6] > 0) + { + return 9990;//活四 + } + + if (TypeCount[CurrentStone][3] > 0) + { + return 9980;//冲四 + } + + if (TypeCount[3 - CurrentStone][6] > 0) + { + return -9970;//活四 + } + + + if (TypeCount[3 - CurrentStone][3] > 0 && TypeCount[3 - CurrentStone][5] > 0) + { + return -9960; //冲四活三 + } + + if (TypeCount[CurrentStone][5]>0 && TypeCount[3 - CurrentStone][3] == 0) + { + return 9950;//冲四活三 + } + + if (TypeCount[3 - CurrentStone][5] > 1 && + TypeCount[CurrentStone][3] == 0 && + TypeCount[CurrentStone][5] == 0 && + TypeCount[CurrentStone][2] == 0) + { + return -9940; + } + + int CValue = 0, PValue = 0; + if (TypeCount[CurrentStone][5] > 1) + CValue += 2000; + else + { + if (TypeCount[CurrentStone][5] > 0) + CValue += 200; + } + + if (TypeCount[3 - CurrentStone][5] > 1) + PValue += 500; + else + { + if (TypeCount[3 - CurrentStone][5] > 0) + PValue += 100; + } + + + if (TypeCount[CurrentStone][2] > 0) + CValue += TypeCount[CurrentStone][2] * 10; + + if (TypeCount[3 - CurrentStone][2] > 0) + PValue += TypeCount[3 - CurrentStone][2] * 10; + + if (TypeCount[CurrentStone][4] > 0) + CValue += TypeCount[CurrentStone][4] * 4; + + if (TypeCount[3 - CurrentStone][2] > 0) + PValue += TypeCount[3 - CurrentStone][4] * 4; + + if (TypeCount[CurrentStone][2] > 0) + CValue += TypeCount[CurrentStone][2]; + + if (TypeCount[3 - CurrentStone][2] > 0) + PValue += TypeCount[3 - CurrentStone][2]; + + for (int i = 0; i < 15; i++) + { + for (int j = 0; j < 15; j++) + { + if (Board[i][j] != 0) + { + if (Board[i][j] == CurrentStone) CValue += PosValue[i][j]; + else PValue += PosValue[i][j]; + } + } + } + return CValue-PValue; +} + +int AnalysisHorizon(int i,int j) +{ + int Line[15]; + for (int s = 0; s < 15; s++){ + Line[s] = Board[i][s]; + } + AnalysisLine(Line, 15, j); + for (int s = 0; s < 15; s++){ + if (LineRecord[s] != 0){ + TypeRecord[i][s][0] = LineRecord[s]; + } + } + return TypeRecord[i][j][0]; +} + +int AnalysisVertical(int i,int j) +{ + int Line[15]; + for (int s = 0; s < 15; s++){ + Line[s] = Board[s][j]; + } + AnalysisLine(Line, 15, i); + for (int s = 0; s < 15; s++){ + if (LineRecord[s] != 0){ + TypeRecord[s][j][1] = LineRecord[s]; + } + } + return TypeRecord[i][j][1]; +} + +int AnalysisLeft(int i,int j) +{ + int Line[15]; + int x, y; + if (i < j){ + y = 0; + x = j - i; + } + else{ + x = 0; + y = i - j; + } + int k; + for (k = 0; k < 15; k++){ + if (x + k > 14 || y + k > 14){ + break; + } + + Line[k] = Board[y + k][x + k]; + } + AnalysisLine(Line, k, j - x); + for (int s = 0; s < k; s++){ + if (LineRecord[s] != 0){ + TypeRecord[y + s][x + s][2] = LineRecord[s]; + } + + } + return TypeRecord[i][j][2]; +} + +int AnalysisRight(int i,int j) +{ + int Line[15]; + int x, y, realnum; + if (14 - i < j){ + y = 14; + x = j - 14 + i; + realnum = 14 - i; + } + else{ + x = 0; + y = i + j; + realnum = j; + } + int k; + for (k = 0; k < 15; k++){ + if (x + k > 14 || y - k < 0){ + break; + } + + Line[k] = Board[y - k][x + k]; + } + AnalysisLine(Line, k, j - x); + for (int s = 0; s < k; s++){ + if (LineRecord[s] != 0){ + TypeRecord[y - s][x + s][3] = LineRecord[s]; + } + + } + return TypeRecord[i][j][3]; +} + +int AnalysisLine(int Line[],int Num,int Pos) +{ + for (int i = 0; i < Num; i++){ + LineRecord[i] = 0; + } + if (Num < 5){ + for (int i = 0; i < Num; i++) LineRecord[i] = -1; + return -1; + } + int LeftEdge, RightEdge; + int LeftRange, RightRange; + LeftEdge = RightEdge = Pos; + while (LeftEdge >0){ + if (Line[LeftEdge - 1] != Line[Pos]){ + break; + } + LeftEdge--; + } + while (RightEdge < Num-1){ + if (Line[RightEdge + 1] != Line[Pos]){ + break; + } + RightEdge++; + } + LeftRange = LeftEdge; + RightRange = RightEdge; + while (LeftRange >0){ + if (Line[LeftRange - 1] == 3-Line[Pos]){ + break; + } + LeftRange--; + } + while (RightRange < Num - 1){ + if (Line[RightRange + 1] == 3 - Line[Pos]){ + break; + } + RightRange++; + } + if (RightRange - LeftRange < 4){ + for (int k = LeftRange; k <= RightRange; k++){ + LineRecord[k] = -1; + } + return -1; + } + for (int k = LeftEdge; k <= RightEdge; k++){ + LineRecord[k] = -1; + } + if (RightEdge - LeftEdge > 3){ + LineRecord[Pos] = 7; //五连 + return 7; + } + if (RightEdge - LeftEdge == 3){ + if (LeftEdge==LeftRange || RightEdge==RightRange){ + LineRecord[Pos] = 3;//四连 + } + else LineRecord[Pos] = 6; + return LineRecord[Pos]; + } + if (RightEdge - LeftEdge == 2){ //三连 + bool LeftThree = false; + if (LeftEdge != LeftRange){ + if (LeftEdge > 1 && Line[LeftEdge - 2] == Line[LeftEdge]){ + LineRecord[LeftEdge] = 3; + LineRecord[LeftEdge - 2] = -1; + } + else LeftThree = true; + } + if (RightEdge != RightRange){ + if (RightEdge < Num - 2 && Line[RightEdge + 2] == Line[RightEdge]){ + LineRecord[RightEdge] = 3; + LineRecord[RightEdge + 2] = -1; + } + else{ + if (LeftThree) LineRecord[Pos] = 5; + else LineRecord[Pos] = 2; + } + } + else{ + if (LineRecord[LeftEdge] == 3) return LineRecord[LeftEdge]; + if(LeftThree) LineRecord[Pos] = 2; + } + return LineRecord[Pos]; + } + if (RightEdge - LeftEdge == 1){//二连 + bool Lefttwo = false; + if (LeftEdge > 2){ + if (Line[LeftEdge - 1] == 0){ + if (LeftEdge - 1 > 1 && Line[LeftEdge - 2] == Line[LeftEdge]){ + if (Line[LeftEdge - 3] == Line[LeftEdge]){ + LineRecord[LeftEdge - 3] = -1; + LineRecord[LeftEdge - 2] = -1; + LineRecord[LeftEdge] = 3; + } + else{ + if (Line[LeftEdge - 3] == 0){ + LineRecord[LeftEdge - 2] = -1; + LineRecord[LeftEdge] = 2; + } + } + } + else Lefttwo = true; + } + } + if (RightEdge < Num-1){ + if (Line[RightEdge + 1] == 0){ + if (RightEdge + 1 < Num - 2 && Line[RightEdge + 2] == Line[RightEdge]){ + if (Line[RightEdge + 3] == Line[RightEdge]){ + LineRecord[RightEdge + 3] = -1; + LineRecord[RightEdge + 2] = -1; + LineRecord[RightEdge] = 3; + } + else{ + if (Line[RightEdge + 3] == 0){ + LineRecord[RightEdge + 2] = -1; + if (Lefttwo == true) LineRecord[RightEdge] = 5; + else LineRecord[RightEdge] = 2; + } + } + + } + else{ + if (LineRecord[LeftEdge] == 3) return LineRecord[LeftEdge]; + if (LineRecord[LeftEdge] == 2){ + LineRecord[LeftEdge] = 5; + return LineRecord[LeftEdge]; + } + + if (Lefttwo == true) LineRecord[Pos] = 4; + else LineRecord[Pos] = 1; + } + } + else{ + if (LineRecord[LeftEdge] == 3) return LineRecord[LeftEdge]; + if (Lefttwo == true) LineRecord[Pos] = 1; + } + + } + return LineRecord[Pos]; + } + return -1; +} + +int CreatePossibleMove( int depth,int Count) +{ + MoveCount = 0; + for (int i = 0; i <15; i++){// 对每一可能的走法 + for (int j = 0; j <15; j++){ + // 生成新节点 + if (Board[i][j] == 0){ + MoveList[depth][MoveCount].r= i; + MoveList[depth][MoveCount].c = j; + MoveList[depth][MoveCount].v = HistoryHeuristic[i][j]; + MoveCount++; + } + } + } + return MoveCount; +} + + + +void Sort(int depth, int movecount) +{ + struct move temp; + for (int i = 0; i <= movecount - 2; i++){ + for (int j = movecount - 2; j >= i; j--){ + if (MoveList[depth][j].v alpha){ + if (depth == 0){ + row = MoveList[depth][i].r; + column = MoveList[depth][i].c; + } + alpha = score; //取极大值 + if (alpha >= beta){ + //相应棋子位置加分 + HistoryHeuristic[MoveList[depth][i].r][MoveList[depth][i].c] += 2 << depth; + return beta;//剪枝 + } + } + } + return alpha;//返回最大值 +} diff --git a/Goband/Function.h b/Goband/Function.h new file mode 100644 index 00000000..aaa0c21e --- /dev/null +++ b/Goband/Function.h @@ -0,0 +1,20 @@ +#include "Content.h" + +void DrawBoard(); +char* Get(int x,int y); +int Put(); +int RunGame(); +void Initialize(); +void Back(); +void ResetHistoryTable(); +int Result(int i,int j,int Count); +int Eveluate(int CurrentStone); +int AnalysisHorizon(int i,int j); +int AnalysisVertical(int i,int j); +int AnalysisLeft(int i,int j); +int AnalysisRight(int i,int j); +int AnalysisLine(int Line[],int Num,int Pos); +int CreatePossibleMove( int depth,int Count); +void Sort(int depth, int movecount); +void ComputerDownStone(); +int AlphaBeta(int depth, int CurrentStone, int alpha, int beta); diff --git a/Goband/main.cpp b/Goband/main.cpp new file mode 100644 index 00000000..da9b8507 --- /dev/null +++ b/Goband/main.cpp @@ -0,0 +1,11 @@ +#include +#include "Function.h" + + +int main() +{ + while(1){ + RunGame(); + } + return 0; +} diff --git a/level1/p01_runningLetter/runningLetter.c b/level1/p01_runningLetter/runningLetter.c new file mode 100644 index 00000000..e344698f --- /dev/null +++ b/level1/p01_runningLetter/runningLetter.c @@ -0,0 +1,35 @@ +#include +#include +#include + +const int LENGTH = 100; + +int main() +{ + int n,j,w,k; + int i = 1; + char ch = 's'; + for(n=LENGTH;i<=n;i++) + { + printf("%c",ch); + system("cls"); + Sleep(100); + + for(j=1;j<=i;j++) + { + printf(" "); + } + } + for(k=1;k<=LENGTH;k++) + { + for(w=LENGTH;w>=k;w--) + { + printf(" "); + } + + printf("%c",ch); + system("cls"); + Sleep(100); + } + return 0; +} diff --git a/level1/p02_isPrime/isPrime.c b/level1/p02_isPrime/isPrime.c new file mode 100644 index 00000000..9d8e7af0 --- /dev/null +++ b/level1/p02_isPrime/isPrime.c @@ -0,0 +1,29 @@ +#include +#include + +int main() +{ + int n,k,i; + printf("Please input a number:"); + scanf("%d",&n); + k = sqrt(n); + + if(n==1) + printf("%d is not a prime number.",n); + else if(n==2) + printf("%d is a prime number.",n); + else + { + for(i=2;i<=k;i++) + { + if(n%i==0) + break; + } + if(i>k) + printf("%d is a prime number.",n); + else + printf("%d is not a prime number.",n); + } + + return 0; +} diff --git a/level1/p03_Diophantus/Diophantus.c b/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..fe712569 --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + float x; + for(x=9.0;x<=120.0;x++) + { + if(x/6.0+x/12.0+x/7.0+5.0+x/2.0+4.0 == x) + { + printf("儿子死时丢番图年龄为:%d",int(x-4)); + break; + } + } + return 0; +} diff --git a/level1/p04_ narcissus/narcissus.c b/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..4b0ea367 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + int i,j,k; + printf("所有3位水仙花数:"); + for(i=1;i<=9;i++) + { + for(j=0;j<=9;j++) + { + for(k=0;k<=9;k++) + if(i*i*i+j*j*j+k*k*k == i*100+j*10+k) + printf("%d ",i*100+j*10+k); + } + } + return 0 ; +} diff --git a/level1/p05_allPrimes/allPrimes b/level1/p05_allPrimes/allPrimes new file mode 100644 index 00000000..a526c393 --- /dev/null +++ b/level1/p05_allPrimes/allPrimes @@ -0,0 +1,34 @@ +#include +#include +#include + +#define N 1000 + +int Prime[N]; + +int main() +{ + int i, j; + clock_t t1,t2; + t1 = clock(); + for(i=3; i<=N; i++){ + if(i%2){Prime[i]=1;} + else {Prime[i]=0;} + } + for(i=3;i<=sqrt(N);i++){ + if(Prime[i]){ + for(j=i+i; j<=N; j+=i){ + Prime[j]=0; + } + } + } + t2 = clock(); + printf("2 "); + for(i=3;i<=N;i++){ + if(Prime[i]==1){ + printf("%d ",i); + } + } + printf("\n总计算时间:%d",t2-t1); + return 0; +} diff --git a/level1/p06_Goldbach/Goldbach.c b/level1/p06_Goldbach/Goldbach.c new file mode 100644 index 00000000..ceb5624e --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.c @@ -0,0 +1,33 @@ +#include +#include + +int isPrime(int n); +int main() +{ + int n,i,p; + while(printf("从3~100内输入一个偶数:"),scanf("%d",&n)) + { + for(i=2;i<=n/2;i++) + { + if(isPrime(i)&&isPrime(n-i)) + { + printf("%d, %d\n",i,n-i); + break; + } + + } + + } return 0; +} + +int isPrime(int n) +{ + int j; + + for(j=2;j<=sqrt(n);j++) + { + if(n%j==0) return 0; + return 1; + } + +} diff --git a/level1/p07_encrypt_decrypt/encode_decode.c b/level1/p07_encrypt_decrypt/encode_decode.c new file mode 100644 index 00000000..fcc44e9d --- /dev/null +++ b/level1/p07_encrypt_decrypt/encode_decode.c @@ -0,0 +1,59 @@ +#include +#include + +void encode(char str[],int n); +void decode(char str[],int n); + +int main() +{ + char str[2048]; + int n; + int choice; + printf("请输入一段字符:"); + scanf("%s",&str); + printf("加密字符请输入1\n"); + printf("解密字符请输入2\n"); + printf("请输入你的选择;"); + scanf("%d",&choice); + + if(choice==1) + { + printf("请输入一个数字选择加密方式:"); + scanf("%d",&n); + } + + switch(choice) + { + case 1: + encode(str,n); + break; + + case 2: + decode(str,n); + break; + } + return 0; +} + +void encode(char str[],int n) +{ + char ch; + int i; + for(i=0;i + +void hanoit (int n,char A,char B,char C); +void move (char FROM,char TO); + +int main () +{ + const int m = 64; + char A = 'A'; + char B = 'B'; + char C = 'C'; // A B C 代表从左到右的三根柱子 + + hanoit(m,A,B,C); + + return 0; +} + +void hanoit (int n,char A,char B,char C) +{ + if(n == 1) + move(A, C); + else + { + hanoit (n-1, A, C, B); + move (A, C); + hanoit (n-1,B, A, C); + } + +} + +void move (char FROM,char TO) +{ + printf("move %c to %c\n",FROM, TO); +} diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c new file mode 100644 index 00000000..4e7b6387 --- /dev/null +++ b/level1/p09_maze/maze.c @@ -0,0 +1,177 @@ +#include +#include +#include +#define LENGTH 25 +void print(); +void place(); +int move(); +int Up(); +int Down(); +int Right(); +int Left(); + +int x,y; +int map[LENGTH][LENGTH] ={ + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, + {3,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0}, + {0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0}, + {0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0}, + {0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0}, + {0,1,0,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,1,1,1,1,0}, + {0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0}, + {0,1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0}, + {0,1,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0}, + {0,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,0,1,0}, + {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0}, + {0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,0}, + {0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0}, + {0,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}, + {0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0}, + {0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0}, + {0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,0}, + {0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0}, + {0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,1,1,0}, + {0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0}, + {0,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0}, + {0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0}, + {0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,4}, + {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, +}; +int main() +{ + + int p = 0; + while(1) + { + p = move(); + if(p == 1) + return 0; + } + return 0; +} +void place() +{ + + int i= 0,j = 0; + for(i = 0;i < LENGTH;i++) + { + for(j = 0;j < LENGTH;j++) + { + if(map[i][j] == 3) + { + x = i; + y = j; + break; + } + } + } +} +int move() +{ + int p = 0; + while(1) + { + p = 0; + place(); + print(); + printf("操作提示:上 下 左 右 控制人物移动,按Esc退出游戏\n"); + switch(getch()) + { + case 72:p = Up();break; + case 75:p = Left();break; + case 80:p = Down();break; + case 77:p = Right();break; + case 27:return 1; + } + if(p == 1) + { + MessageBox(0,TEXT("恭喜过关"),TEXT("恭喜"),NULL); + return 1; + } + } +} +void print() +{ + int i= 0,j = 0; + system("cls"); + for(i = 0;i < LENGTH;i++) + { + for(j = 0;j < LENGTH;j++) + { + if(map[i][j] == 0) + printf("■"); + else if(map[i][j] == 1) + printf(" "); + else if(map[i][j] == 3) + printf("⊙"); + else if(map[i][j] == 4) + printf("☆"); + } + printf("\n"); + } +} +int Up() +{ + if(x != 0) + { + if(map[x-1][y] == 1) + { + map[x-1][y] = 3; + map[x][y] = 1; + } + else if(map[x-1][y] == 4) + { + return 1; + } + } + return 0; +} +int Down() +{ + if(x != LENGTH-1) + { + if(map[x+1][y] == 1) + { + map[x+1][y] = 3; + map[x][y] = 1; + } + else if(map[x+1][y] == 4) + { + return 1; + } + } + return 0; +} +int Right() +{ + if(y != LENGTH-1) + { + if(map[x][y+1] == 1) + { + map[x][y+1] = 3; + map[x][y] = 1; + } + else if(map[x][y+1] == 4) + { + return 1; + } + } + return 0; +} +int Left() +{ + if(y != 0) + { + if(map[x][y-1] == 1) + { + map[x][y-1] = 3; + map[x][y] = 1; + } + else if(map[x][y-1] == 4) + { + return 1; + } + } + return 0; +} diff --git a/level1/p10_pushBoxes/pushBoxes b/level1/p10_pushBoxes/pushBoxes new file mode 100644 index 00000000..07b15395 --- /dev/null +++ b/level1/p10_pushBoxes/pushBoxes @@ -0,0 +1,281 @@ +#include +#include +#include +#include +#define LENGTH 8 +#define LEVEL 3 + +void Map(); +void print(); +void menu(); +void place(); +int move(); +void Up(); +void Down(); +void Right(); +void Left(); +int judge(); +void record(); + +int level = 0; +int x,y,step[LEVEL]={0}; +int map[LEVEL][LENGTH][LENGTH]; + +int main() +{ + int flag = 0; + menu(); + while(1) + { + flag = move(); + if(flag == 1 || flag == -1) + break; + } + record(); + return 0; +} + +void Map() +{ + int i,j,k; + FILE *fp=fopen("D:\\Try.txt","r"); + for(k=0;k1) + { + printf("错误输入,请重新输入:"); + } + else + { + if('1' == select[0]) + { + level = 0; + break; + } + else if('2' == select[0]) + { + level = 1; + break; + } + else if('3' == select[0]) + { + level = 2; + break; + } + else + { + printf("错误输入,请重新输入:"); + } + } + } + printf("%d\n",level); +} + +int move() +{ + int flag = 0; + Map(); + while(1) + { + place(); + print(); + switch(getch()) + { + case 72:Up();break; + case 75:Left();break; + case 80:Down();break; + case 77:Right();break; + case 27:return -1; + } + flag=judge(); + if(flag == 1) + { + if(level == LEVEL-1) + { + MessageBox(0,TEXT("恭喜你完成了所有关卡,圣诞节快乐"),TEXT("恭喜"),NULL); + return 1; + } + else + { + MessageBox(0,TEXT("恭喜你通过了这一关"),TEXT("恭喜"),NULL); + level ++; + } + } + } +} + +void place() +{ + + int i= 0,j = 0; + for(i = 0;i < LENGTH;i++) + { + for(j = 0;j < LENGTH;j++) + { + if(map[level][i][j] == 5||map[level][i][j] == 8) + { + x = i; + y = j; + break; + } + } + } +} + +void print() +{ + int i= 0,j = 0; + system("cls"); + printf("Number of steps are:%d\n",step[level]); + for(i = 0;i < LENGTH;i++) + { + for(j = 0;j < LENGTH;j++) + { + if(map[level][i][j] == 1) + printf("■"); + else if(map[level][i][j] == 0) + printf(" "); + else if(map[level][i][j] == 3) + printf("☆"); + else if(map[level][i][j] == 4) + printf("□"); + else if(map[level][i][j] == 5) + printf("♀"); + else if(map[level][i][j] == 7) + printf("★"); + else if(map[level][i][j] == 8) + printf("♀"); + + } + printf("\n"); + } +} + +void Up() +{ + if(map[level][x-1][y]== 0||map[level][x-1][y]== 3) + { + map[level][x-1][y] += 5; + map[level][x][y] -= 5; + step[level]++; + } + else if(map[level][x-1][y]== 4||map[level][x-1][y]== 7) + { + if(map[level][x-2][y]== 0||map[level][x-2][y]== 3) + { + map[level][x-2][y] += 4; + map[level][x-1][y] += 1; + map[level][x][y] -= 5; + step[level]++; + } + } +} + +void Down() +{ + if(map[level][x+1][y]== 0||map[level][x+1][y]== 3) + { + map[level][x+1][y] += 5; + map[level][x][y] -= 5; + step[level]++; + } + else if(map[level][x+1][y]== 4||map[level][x+1][y]== 7) + { + if(map[level][x+2][y]== 0||map[level][x+2][y]== 3) + { + map[level][x+2][y] += 4; + map[level][x+1][y] += 1; + map[level][x][y] -= 5; + step[level]++; + } + } +} + +void Right() +{ + if(map[level][x][y+1]== 0||map[level][x][y+1]== 3) + { + map[level][x][y+1] += 5; + map[level][x][y] -= 5; + step[level]++; + } + else if(map[level][x][y+1]== 4||map[level][x][y+1]== 7) + { + if(map[level][x][y+2]== 0||map[level][x][y+2]== 3) + { + map[level][x][y+2] += 4; + map[level][x][y+1] += 1; + map[level][x][y] -= 5; + step[level]++; + } + } +} + +void Left() +{ + if(map[level][x][y-1]== 0||map[level][x][y-1]== 3) + { + map[level][x][y-1] += 5; + map[level][x][y] -= 5; + step[level]++; + } + else if(map[level][x][y-1]== 4||map[level][x][y-1]== 7) + { + if(map[level][x][y-2]== 0||map[level][x][y-2]== 3) + { + map[level][x][y-2] += 4; + map[level][x][y-1] += 1; + map[level][x][y] -= 5; + step[level]++; + } + } +} + +int judge() +{ + int k=0; + int i= 0,j = 0; + for(i = 0;i < LENGTH;i++) + { + for(j = 0;j < LENGTH;j++) + { + if(map[level][i][j] != 4) + { + k++; + } + } + } + if(k==LENGTH*LENGTH){return 1;} + else return 0; +} + +void record() +{ + FILE *fp = fopen("D:\\score.txt","w+"); + if (fp!=NULL) + { + for(int i =0;i +#include + +typedef struct Node{ + int value; + struct Node *next; +}Linklist; + +Linklist *s; +int k=0; + +Linklist *Creat(int n){ + Linklist *head, *node, *end; + head = (Linklist*)malloc(sizeof(Linklist)); + end = head; + for(int i=0;ivalue); + end->next = node; + end = node; + } + end->next = NULL; + return head; +} + +Linklist *Reverse(Linklist *list){ + if(list == NULL){ + return list; + } + Linklist *p1,*p2,*p3; + p1 = list; + p2 = list->next; + p3 = NULL; + list->next = NULL; + while (p2 != NULL){ + p3 = p2; + p2 = p2->next; + p3->next = p1; + p1 = p3; + } + list = p1; + return list; +} + +int LinklistSearch(Linklist *list,int x){ + if(list == NULL){ + return -1; + } + while(list != NULL){ + k++; + if(list->value == x){ + s = list->next; + return k; + } + list = list->next; + } + return -1; +} + +int main() +{ + Linklist *head; + int n,x,location1,location2; + printf("Please enter the number of nodes:"); + scanf("%d",&n); + head = Creat(n); + s = Reverse(head); + printf("Please enter the number that you want to find:"); + scanf("%d",&x); + location1 = LinklistSearch(s,x); + location2 = LinklistSearch(s,x); + printf("The first location is %d,the second is %d",location1,location2); + return 0; + } diff --git a/level1/p12_warehouse/warehouse b/level1/p12_warehouse/warehouse new file mode 100644 index 00000000..265505eb --- /dev/null +++ b/level1/p12_warehouse/warehouse @@ -0,0 +1,137 @@ +#include +#include +#include + +void menu(); +void open(); +void display(); +void in_warehouse(); +void out_warehouse(); +void close(); + +int m; + +struct warehouse{ + char id[10]; + int data[10]; + int num; +}; +struct warehouse goods[1000]; + + +int main() +{ + int i; + open(); + menu(); + printf("请输入你选择操作的序号:"); + while(scanf("%d",&i)){ + switch(i){ + case 1: + display(); + break; + case 2: + in_warehouse(); + void close(); + break; + case 3: + out_warehouse(); + void close(); + break; + case 4: + exit(0); + } + } + + return 0; +} + +void menu() +{ + printf("***************************************\n"); + printf("* 库存信息管理系统 *\n"); + printf("* 1.显示存货列表 *\n"); + printf("* 2.入库 *\n"); + printf("* 3.出库 *\n"); + printf("* 4.退出程序 *\n"); + printf("***************************************\n"); +} + +void open() +{ + int i=0; + FILE *fp=fopen("D:\\goods.txt","r"); + while(fscanf(fp,"%s%s%d",&goods[i].id,&goods[i].data,&goods[i].num)!=EOF){ + i++; + } + fclose(fp); + m=i; + +} + +void display() +{ + printf("商品号 生产日期 商品数量\n"); + for(int i=0;i=num){ + goods[i].num=goods[i].num-num; + printf("出库成功"); + return; + } + else{ + printf("商品数量不足"); + return; + } + } + } + } + printf("仓库中不存在该商品"); +} + +void close() +{ + FILE *fp=fopen("D:\\goods.txt","w"); + for(int i=0;i