首页 > 编程知识 正文

英语音标怎么读(c和c#和c++区别)

时间:2023-05-04 03:44:24 阅读:64873 作者:1820

1 .实验内容

在C#中,使用者分别输入猴子、香蕉、箱子所在的位置,制定了输出完整猴子摘香蕉的流程。

2 .大致逻辑

首先,用户输入猴子、箱子、香蕉所在的位置,首先判断猴子和箱子是否在同一个位置,如果不在同一个位置,就把猴子移动到箱子所在的位置,改变猴子在状态结构体中的位置值。

第一步实现时,判断猴子、箱子和香蕉是否在同一个位置,否则猴子爬上箱子拿香蕉,要猴子把箱子推到香蕉位置,这时改变猴子和箱子在状态结构中的位置值。

猴子、箱子和香蕉在同一个位置时,判断猴子是否在箱子上,在箱子上时直接拿香蕉,猴子不在箱子上时,先猴子爬上箱子再拿香蕉。 此时,改变猴子是否在箱子上的状态结构的值,最后将步骤输出到控制台。

3 .代码实现

用户系统; using system.collections.generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace monkey { class program { struct state } public int monkey; //猴子的位置public int Banana; //香蕉位置公共盒; //箱的位置公共bool monb; //猴子是否在箱子上,有True,无false }//新的结构数组和相应的步骤static state [ ] States=newstate [ 200 ]; Dictionaryint,stringstepsave=newdictionaryint,string (; publicstaticvoidmain (字符串[ ] args ) { Program pg=new Program; Console.Write ('输入猴子的位置(-1,0,1 ) : ); 设置范围(ref States [0].monkey,int.parse ) console.readline (); Console.Write ('输入香蕉位置(-1,0,1 ) : ); 设置范围(ref States [0].banana,int.parse ) console.readline (); Console.Write ('输入框的位置(-1,0,1 ) : ); 设置范围(ref States [0].box,int.parse ) console.readline (); if (States [0].box==States [0].monkey ) { states[0].MonB=true; if (States [0].monkey==States [0].banana ) ) { Console.Write )猴子躺在箱子里,伸手就能碰香蕉) ); 控制台. read key (; 环境. exit (0; } } else states[0].MonB=false; pg.nextstep(0; //summary//3限定三个点的范围///summary//param name=' result '/param//param name=' a '/paramstaticvoidsetrame } else result=a; //猴子移动voidmonkeygoto(inta,int i ) ) if

(a == -1) { stepSave[i] = "猴子到了A点"; states[i + 1] = states[i]; states[i + 1].Monkey = -1; } else if (a == 0) { stepSave[i] = "猴子到了B点"; states[i + 1] = states[i]; states[i + 1].Monkey = 0; } else if (a == 1) { stepSave[i] = "猴子到了C点"; states[i + 1] = states[i]; states[i + 1].Monkey = 1; } } //推箱子的移动 void MoveBox(int a, int i) { if (a == -1) { stepSave[i] = "猴子将箱子推到了A点"; states[i + 1] = states[i]; states[i + 1].Monkey = -1; states[i + 1].Box = -1; } else if (a == 0) { stepSave[i] = "猴子将箱子推到了B点"; states[i + 1] = states[i]; states[i + 1].Monkey = 0; states[i + 1].Box = 0; } else if (a == 1) { stepSave[i] = "猴子将箱子推到了C点"; states[i + 1] = states[i]; states[i + 1].Monkey = 1; states[i + 1].Box = 1; } } //爬箱子 void ClimbOn(int i) { stepSave[i] = "猴子爬上了箱子"; states[i + 1] = states[i]; states[i + 1].MonB = true; } //下箱子:用于初始猴子和箱子位置一样的时候 void ClimbDown(int i) { stepSave[i] = "猴子爬下了箱子"; states[i + 1] = states[i]; states[i + 1].MonB = false; } //拿到香蕉 void Reach(int i) { stepSave[i] = "猴子摘到了香蕉"; } //在控制台上打印步骤 void ShowSolution(int i) { int j; Console.Write("问题结果:n"); for (j = 0; j < i + 1; j++) { Console.Write($"Step{j + 1}: {stepSave[j]} n"); } Console.Write("n"); } void nextStep(int i) { int c; int k; //步数超过一定界限认为失败 if (i >= 200) { Console.Write("%s n", "猴子不会摘香蕉了!"); return; } //先判断是否跟之前的状态相同,若是则返回防止程序死循环 for (c = 0; c < i; c++) { if (states[c].Monkey == states[i].Monkey && states[c].Box == states[i].Box && states[c].Banana == states[i].Banana && states[c].MonB == states[i].MonB) return; } //拿到香蕉 if (states[i].MonB == true && states[i].Monkey == states[i].Banana && states[i].Monkey == states[i].Box) { ShowSolution(i); Console.ReadKey(); Environment.Exit(0); } k = i + 1;//进行数据更新,用来标记当前是第几个状态 if (states[i].Monkey == states[i].Box)//猴子和箱子在一起 { if (states[i].MonB)//猴子在箱子上 { ClimbDown(i); nextStep(k); } else//猴子没在箱子上 { if (states[i].Monkey != states[i].Banana)//三者不在同一位置 { MoveBox(states[i].Banana, i); nextStep(k); } else //三者在同一位置 { ClimbOn(i); Reach(i + 1); nextStep(k); } } } else //猴子和箱子不在一起 { MonkeyGoTo(states[i].Box, i); nextStep(k); } } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。