当前位置: 首页 > news >正文

杭电网安复试编程Day24

1、十六进制转换

题目描述:输入一个十进制的数,把它转成十六进制。

方法一:利用内置函数

#include<iostream> using namespace std; int n; int main() { cin>>n; cout << hex << n << endl; return 0; }

方法二:手动转换

#include<iostream> 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.

We'll 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 worm's 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.

INPUT

There 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.

OUTPUT

Generate 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.jsqmd.com/news/521378/

相关文章:

  • Qt6 QML自定义控件:从零到插件化的实战开发手册
  • 3分钟掌握WE Learn智能助手:让你的网课学习效率提升300%
  • MCP3208 12位SPI ADC驱动开发与嵌入式精度采集实战
  • 【Unity进阶】AudioSource 实战技巧与性能优化指南
  • 5V光耦隔离继电器模块硬件设计与RT-Thread驱动实现
  • 极简七段数码管驱动库:裸机嵌入式GPIO直写方案
  • 一文读懂-yolo26如何预测识别图片|视频|摄像头|文件夹检测适用v8v11
  • 35岁以后,我们这些老程序员们能去哪儿?
  • Phi-3-vision-128k-instruct 创意应用:辅助 Visio 图表设计与文档撰写
  • 如何通过Win11Debloat实现Windows系统深度优化:从性能提升到隐私保护的全流程指南
  • 语音情感识别不再难:Emotion2Vec+ Large WebUI界面操作详解
  • 钻床主轴设计CAD图纸
  • Delphi 进阶实战:异常捕获+多线程,让软件更稳定、更高效!
  • 基于Gemma-3-270m的小说解析器开发教程
  • 性能调优指南:Z-Image-Turbo-rinaiqiao-huiyewunv 的 GPU 显存与推理速度优化
  • Delphi 成品发布:exe压缩、依赖处理、制作安装包,新手一步到位!
  • AnythingtoRealCharacters2511在虚拟偶像运营中的应用:2D形象→3D真人视频素材预处理
  • 仅剩47家芯片厂掌握的C语言存内逻辑映射技术,今天一次性讲透3类硬件指令扩展实现
  • 中小影楼降本增效:cv_unet_image-colorization替代传统人工上色服务案例
  • Wan2.2-T2V-A5B嵌入式展示系统:基于STM32F103C8T6的轻量级播放终端
  • 安装linux操作系统
  • 漫画脸描述生成快速上手:免配置Docker镜像开箱即用,5分钟生成NovelAI可用Tag
  • LTR559-ESP32光感与接近传感驱动实战指南
  • DA7280触觉驱动库深度解析:LRA/ERM振动控制实战
  • 深入理解 RAGFlow 混合检索:从 BM25 到 KNN 的底层实现与调优技巧
  • Python数学建模从入门到精通:5本实战书籍推荐(附避坑指南)
  • 【限时解禁】中国兵器工业集团内部《C语言安全编码红线手册》(2024修订版)核心章节流出:17条禁令+32个正向范式+4类典型误用反例
  • InternVL(1~3.5版本)多模型大模型训练中的数据集构造总结
  • PowerPaint-V1 Gradio部署指南:Docker独立运行,与.NET应用解耦的最佳实践
  • GeoScene Enterprise2.1在Windows环境下的高效安装与配置实战