当前位置:网站首页>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, 有任何问题欢迎讨论交流
边栏推荐
- DS inner row heap sort
- DS sort -- quick sort
- A common Dao class and util
- Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection
- C language large and small end mode judgment function
- Error when using Fiddler hook: 502 Fiddler - connection failed
- Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
- Activity Registration: how to quickly start the open source tapdata live data platform on a zero basis?
- Huawei camera capability
- [tkinter beautification] window out of system style (common to three systems)
猜你喜欢

Outlook tutorial, how to create tasks and to DOS in outlook?

Various searches (⊙▽⊙) consolidate the chapter of promotion

【OpenCV 例程300篇】238. OpenCV 中的 Harris 角点检测

Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?

Error when using Fiddler hook: 502 Fiddler - connection failed

JSON file editor

The server switches between different CONDA environments and views various user processes

Kali concise language transformation method (illustration)

Summary of feature selection: filtered, wrapped, embedded

26. Code implementation of file using disk
随机推荐
Discussion on the basic use and address of pointer in array object
Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
Kali concise language transformation method (illustration)
Preparation of mobile end test cases
Fraud detection cases and Titanic rescued cases
Similarities and differences between nor flash and NAND flash
kali简洁转换语言方法(图解)
(零九)Flask有手就行——Cookie和Session
spark学习笔记(三)——sparkcore基础知识
佣金哪家券商最低,我要开户,手机上开户安不安全
新手第一次怎么买股票 哪家证券公司开户最好最安全
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
【Bug解决】Win10安装pycocotools报错
Comparison of traversal speed between map and list
REST风格
Jmmert aggregation test report
Outlook tutorial, how to create tasks and to DOS in outlook?
Date processing bean
spark:获取日志中每个时间段的访问量(入门级-简单实现)
[bug solution] error in installing pycocotools in win10