杭电网安复试编程Day24
1、十六进制转换题目描述输入一个十进制的数把它转成十六进制。方法一利用内置函数#includeiostream using namespace std; int n; int main() { cinn; cout hex n endl; return 0; }方法二手动转换#includeiostream using namespace std; int n; void turns(int x) { if (x 0) { cout 0; return; //提前返回 } char hexDigits[] 0123456789ABCDEF; char result[100]; // 足够存储十六进制结果 int index 0; while (x 0) { int remainder x % 16; result[index] hexDigits[remainder]; x / 16; } // 逆序输出 for (int i index - 1; i 0; i--) { cout result[i]; } } int main() { cin n; turns(n); return 0; }2、贪吃蛇Worm is an old computer game. There are many versions, but all involve maneuvering a worm around the screen, trying to avoid running the worm into itself or an obstacle.Well simulate a very simplified version here. The game will be played on a 50*50 board, numbered so that the square at the upper left is numbers (1, 1). The worm is initially a string of 20 connected squares. Connected squares are adjacent horizontally or vertically. The worm starts stretched out horizontally in positions (25, 11) through (25, 30), with the head of the worm at (25, 30). The worm can move either East (E), West (W), North (N) or South (S), but will never move back on itself. So, in the initial position, a W move is not possible. Thus the only two squares occupied by the worm that change in any move are its head and tail. Note that the head of the worm can move to the square just vacated by the worms tail.You will be given a series of moves and will simulate the moves until either the worm runs into itself, the worm runs off the board, or the worm successfully negotiates its list of moves. In the first two cases you should ignore the remaining moves in the list.INPUTThere will be multiple problems instances. The input for each problem instance will be on two lines. The first line is an integer n (100) indicating the number of moves to follow. (A value of n 0 indicates end of input.) The next line contains n characters (either E, W, N or S), with no spaces separating the letter, indicating the sequence of moves.OUTPUTGenerate one line of output for each problem instance. The output line should be one of the follow three:The worm ran into itself on move m.The worm ran off the board on move m.The worm successfully made all m moves.Where m is for you to determine and the first move is move 1.#include iostream #include string using namespace std; // 蛇身坐标结构体 struct SnakeSegment { int x, y; }; int main() { int n; // 移动步数 string moves; // 存储移动序列 while (cin n n ! 0) { cin moves; // 读取移动字符串无空格 // 初始化蛇身20节水平放置头在 (25,30)尾在 (25,11) SnakeSegment worm[20]; for (int i 0; i 20; i) { worm[i].x 25; worm[i].y 11 i; // worm[0]为尾worm[19]为头 } bool success true; // 标记是否成功完成所有移动 int moveIdx 0; // 当前移动序号从1开始 // 依次执行每个移动指令 for (moveIdx 0; moveIdx n; moveIdx) { char dir moves[moveIdx]; // 当前方向 // 计算新头的位置 int newHeadX worm[19].x; int newHeadY worm[19].y; switch (dir) { case N: newHeadX--; break; // 北 case S: newHeadX; break; // 南 case E: newHeadY; break; // 东 case W: newHeadY--; break; // 西 } // 1. 检查是否撞墙边界 1~50 if (newHeadX 1 || newHeadX 50 || newHeadY 1 || newHeadY 50) { cout The worm ran off the board on move moveIdx 1 .\n; success false; break; } // 2. 检查是否撞到自己允许撞到蛇尾因为蛇尾即将离开 bool selfCollision false; // 遍历除蛇尾worm[0]以外的所有身体 for (int j 1; j 20; j) { if (newHeadX worm[j].x newHeadY worm[j].y) { selfCollision true; break; } } if (selfCollision) { cout The worm ran into itself on move moveIdx 1 .\n; success false; break; } // 3. 合法移动更新蛇身 // 将所有身体向前移动一节蛇尾消失新头加入 for (int j 0; j 19; j) { worm[j].x worm[j 1].x; worm[j].y worm[j 1].y; } worm[19].x newHeadX; worm[19].y newHeadY; } // 如果循环正常结束且未触发任何错误则成功完成所有移动 if (success) { cout The worm successfully made all n moves.\n; } } return 0; }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2439692.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!