当前位置:网站首页>2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第三题 跑团机器人 (已完结)
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第三题 跑团机器人 (已完结)
2022-07-24 15:13:00 【trudbot】
题目
RC-u3 跑团机器人
在桌面角色扮演游戏(TRPG,俗称“跑团”)中,玩家需要掷出若干个骰子,根据掷出的结果推进游戏进度。在线上同样可以跑团,方法是由玩家们向机器人发出指令,由机器人随机产生每个需要掷出的骰子的结果。
玩家向机器人发出的指令是一个仅涉及加法和减法的表达式,即对若干个数字进行一系列加法或减法计算。这些数字可以是直接给出的非负整数(数字不超过 1000),也可以是若干个骰子掷出的结果。
“掷骰子”这个动作对应的指令格式为 xdy,表示摇动 x 个 y 面的骰子(1≤x≤1000,2≤y≤1000)。当 x 为 1 时,1 可以省略。
例如指令 2d3+3-d4的意思是:先掷出 2 个 3 面骰子(你不必考虑现实中是否存在这样的骰子),不妨假设结果为 1 和 3,则 2d3的结果就是两个骰子的面值之和 4;然后计算 4 + 3,得到结果为 7;再掷出 1 个 4 面骰子,不妨假设结果为 2,则计算 7 - 2 得到最终结果 5。
本题就请你计算玩家输入的指令里,不同种类的骰子需要掷出几个,以及可能得到的结果在什么区间范围内。
输入格式:
输入在一行中给出一条符合题目描述的玩家输入机器人的指令。题目保证指令长度不超过 2∗1e4。
输出格式:
首先输出不同种类的骰子分别需要掷出几个。每种骰子的信息占一行,依次输出骰子的面数和投掷的数量,按面数从小到大输出。
输入指令保证至少有一个骰子需要掷出。
最后一行输出两个数,表示根据输入指令可以得到的最小结果和最大结果。
同一行数字间以 1 个空格分隔,行首尾不得有多余空格。
输入样例:
d6+3d5+2-2d3+2d5
输出样例:
3 2
5 5
6 1
2 31
题解
运算符只有+/-, 所以我们不用考虑运算符的优先级, 用循环来完成即可.
基本的思想是每次循环处理一个指令(xdy)以及得到下一个指令的符号(正负), 但要注意操作数除了指令还有常量
在代码注释里进行更详细的讲解
AC代码(详细注释)
//
// Created by trudbot on 2022/7/12.
//
#include <bits/stdc++.h>
using namespace std;
map<int, int> num;//存储每种骰子被掷的总数
int Max, Min;//点数的最大最小值
void Solution(string str)
{
//l指向当前要处理指令的第一个字符, sign为当前指令的正负号, 1为正, -1为负
int r = str.length()-1, l = 0, sign = 1;
while(l <= r)
{
int x = 0, y = 0;
//读取x
while(l<=r && str[l] != 'd' && str[l] != '+' && str[l] != '-')
x = x*10 + str[l++] - '0';
//读取完x后停止在d字符, 说明是一个指令
if(str[l] == 'd')
{
if(x == 0) x = 1;//d前面没有字符时, x默认为1
l++;
//读取y
while(l<=r && str[l] != '+' && str[l] != '-')
y = y*10 + str[l++] - '0';
num[y] += x;//y面骰子的掷数增加x
if(sign == 1)
{
//为正时, Max加上最大数, Min加上最小数
Max += y*x;//每次都掷y点
Min += x;//每次都掷1点
}
else//为负时, 同上
{
Max -= x;
Min -= y*x;
}
}
else//读取完x后停止在+/-或越界, 说明是一个常量, 乘上符号直接加到Max, Min里即可
{
Max += sign*x;
Min += sign*x;
}
if(l <= r)//此时l要么越界, 要么停在+/-
sign = str[l++] == '+' ? 1 : -1;//判断下一个操作对象的符号, l后移指向它的第一个字符
}
}
int main() {
string str;
cin >> str;
Solution(str);
for(auto i : num)
{
cout << i.first << " " << i.second << endl;
}
cout << Min << " " << Max << endl;
return 0;
}

如上代码可AC, 有任何问题欢迎讨论交流
边栏推荐
- File upload and download and conversion between excel and data sheet data
- Caffe framework and production data source for deep learning
- VSCode如何调试Nodejs
- PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
- "After 00" is coming! Digital data ushers in a new generation of "codeless" forces
- Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
- Self join usage of SQL
- Attributeerror: module 'distutils' has no attribute' version error resolution
- (零九)Flask有手就行——Cookie和Session
- Date processing bean
猜你喜欢

(09) flask is OK if it has hands - cookies and sessions

Activity Registration: how to quickly start the open source tapdata live data platform on a zero basis?

"After 00" is coming! Digital data ushers in a new generation of "codeless" forces

ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测

Spark: get the access volume of each time period in the log (entry level - simple implementation)

Attributeerror: module 'distutils' has no attribute' version error resolution

Detailed explanation of document operation

Decrypt "sea Lotus" organization (domain control detection and defense)

25.从生磁盘到文件
![[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes](/img/4d/b0ba599a732d1390c5eeb1aea6e83c.png)
[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes
随机推荐
野火stm32霸道,通过固件库实现流水灯
C# SQLite Database Locked exception
Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals
SQL的SELF JOIN用法
Detailed explanation of document operation
Leetcode-09 (next rank + happy number + full rank)
Can you buy 6% of financial products after opening a stock account?
[USENIX atc'22] an efficient distributed training framework whale that supports the super large-scale model of heterogeneous GPU clusters
Mysql库的操作
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
26.文件使用磁盘的代码实现
C language large and small end mode judgment function
图的存储和遍历
Cloud development standalone image Jiugongge traffic main source code
Production environment tidb cluster capacity reduction tikv operation steps
DS diagram - the shortest path of the diagram (excluding the code framework)
打假Yolov7的精度,不是所有的论文都是真实可信
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
DS sort -- quick sort