Dice Notation(模拟)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
... <Saika> I want to get some water from this strange lake. I have a bottle. <Keeper> OK. <Saika> Then I want to go forward to look into the parterre. <Keeper> More details? <Saika> Err..What will happen if I let some water drip on the flowers? <Keeper> ...Err...The flowers will all become super-huge monsters and it will be very dangerous. <Keeper> Ready to fight. <Saika> WHAT THE HELL??? ...
A tabletop role-playing game, or pen-and-paper role-playing game, or table-talk role-playing game is a form of role-playing game (RPG) in which the participants describe their characters' actions through speech. Participants determine the actions of their characters based on their characterization, and the actions will succeed or fail according to a formal system of rules and guidelines. Within the rules, players have the freedom to improvise. Their choices shape the direction and outcome of the game.
The outcomes of some actions are determined by the rules of the game. For example, while looking around the room, a character may or may not notice an important object or secret doorway, depending on the character's powers of perception. This usually involves rolling dice, and comparing the number rolled to their character's statistics to see whether the action was successful. Typically, the higher the character's score in a particular attribute, the higher their probability of success. Combat is resolved in a similar manner, depending on the character's combat skills and physical attributes.
From Wikipedia, by Sargoth. License: CC-by-sa 3.0.
Dice notation (also known as dice algebra, common dice notation, RPG dice notation, and several other titles) is a system to represent different combinations of dice in role-playing games using simple algebra-like notation such as "2d6 + 12".
In most role-playing games, dice rolls required by the system are given in the form of "NdX". N and X are variables, separated by the letter "d", which stands for dice. N is the number of dice to be rolled (usually omitted if 1), and X is the number of faces of each dice. For example, if a game would call for a roll of "d4" or "1d4", this would mean roll a 4-sided dice. While "3d6" would mean roll three six-sided dices. An X-sided dice can get an integer between 1 and X with equal probability.
To this basic notation, an additive modifier can be appended, yielding expressions of the form of "NdX + B". Here, B is a number to be added to the sum of the rolls. We can use a minus sign ("-") to indicate subtraction. So, "1d20 - 10" would indicate a roll of a single 20-sided dice with 10 being subtracted from the result. Further more, we can use multiplication ("*") or division ("/") to do some more compilcated calculations like "(2d6 + 5) * 10 / (12 - 3d6)".
To be specific, here is a standard BNF describes the dice notation:
<notation> ::= <term> "+" <notation>
| <term> "-" <notation>
| <term>
<term> ::= <factor> "*" <term>
| <factor> "/" <term>
| <factor>
<factor> ::= "(" <notation> ")"
| <integer>
| <dice>
<integer> ::= <digit> <integer>
| <digit>
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<dice> ::= <integer> "d" <integer>
| "d" <integer>
To have a clearer result of a dice notation in a game, our poor player, Saika, decides to write a program as a dice bot. To standardize the output information, the program needs to generate a format string from user's input string. It should:
- Expand dice notations. The <dice> field like "3d5" should be expanded to "([d5] + [d5] + [d5])". If only one dice is rolled in this field, simply replaced it with "[dX]".
- Trim whitespaces. There should be one and only one space character existed around operators ("+" / "-" / "*" / "/"). No extra whitespaces characters (including "Tab" and "Space") are allowed in the format string.
- End with specific content. Add " = [Result]" to the end of the format string.
However, Saika is fighting against some indescribable monsters now. She has no time to write this program by herself. Please help her to finish it.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
There is a line contains a valid dice notation. The length of the notation won't exceed 2000.
Output
For each test case, output the format string.
Sample Input
3
d6+1
((2d6) +5)*((12* 3d6))
2d10 * d100
Sample Output
[d6] + 1 = [Result]
((([d6] + [d6])) + 5) * ((12 * ([d6] + [d6] + [d6]))) = [Result]
([d10] + [d10]) * [d100] = [Result]
题解:挂了好多次。。。没想到数字可以是大数,题意很简单,就是给一个字符串,规范的表示出来,其中d的系数不是大数,但是纯数字可能是大数。。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
char s[MAXN];
char x[MAXN];
typedef long long LL;
bool is_digit(char c){
if(c >= '' && c <= '')return true;
else return false;
}
bool is_ys(char c){
if(c == '+' || c == '-' || c == '*' || c == '/' || c == '=')
return true;
else
return false;
}
LL chenge(string num){
LL temp = ;
for(int i = ; i < num.length(); i++){
temp = temp * + num[i] - '';
}
return temp;
}
void word(char *c, int &i){
LL temp;
int tp = ;
x[] = '[';
string num;
while(is_digit(c[i]) || c[i] == ' ' || c[i] == '\t'){
if(c[i] == ' ' || c[i] == '\t'){
i++;
continue;
}
num += c[i];
i++;
}
if(c[i] == 'd'){
temp = chenge(num);
x[tp++] = 'd';
i++;
while(is_digit(c[i]) || c[i] == ' ' || c[i] == '\t'){
if(c[i] == ' ' || c[i] == '\t'){
i++;
continue;
}
x[tp++] = c[i];
i++;
}
x[tp++] = ']';
x[tp] = '\0';
if(temp == ){
printf("%s", x);return;
}
printf("(");
while(temp--){
printf("%s", x);
if(temp > )printf(" + ");
}
printf(")");
}
else{
cout << num;
}
}
void work(){
int len = strlen(s);
for(int i = ; i < len;){
if(s[i] == ' ' || s[i] == '\t'){
i++;
continue;
}
if(is_digit(s[i])){
word(s, i);
}
else if(s[i] == 'd'){
printf("[d");
i++;
while(isdigit(s[i]) || s[i] == ' ' || s[i] == '\t'){
if(s[i] == ' ' || s[i] == '\t'){
i++;
continue;
}
printf("%c", s[i]);
i++;
}
printf("]");
}
else if(is_ys(s[i])){
printf(" %c ", s[i]);
i++;
}
else{
printf("%c", s[i]);
i++;
}
}
}
int main(){
int T;
scanf("%d", &T);
getchar();
while(T--){
gets(s);
work();
printf(" = [Result]\n");
}
return ;
}
Dice Notation(模拟)的更多相关文章
- ZOJ - 3930 Dice Notation 【模拟】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3930 题意 给出一串字符串 如果是 '+' '-' '*' '/ ...
- ZOJ 3826 Hierarchical Notation 模拟
模拟: 语法的分析 hash一切Key建设规划,对于记录在几个地点的每个节点原始的字符串开始输出. . .. 对每一个询问沿图走就能够了. .. . Hierarchical Notation Tim ...
- ZOJ 3930 Dice Notation
简单模拟题.一个int写成了char,搞了4个多小时.真垃圾.. #include<stdio.h> #include<string.h> +],s[+]; +]; +]; i ...
- Codeforces 691C. Exponential notation 模拟题
C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- ZOJ - 3829 Known Notation(模拟+贪心)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...
- NAIPC-2016
A. Fancy Antiques 爆搜+剪枝. #include <bits/stdc++.h> using namespace std ; typedef pair < int ...
- 简单介绍 Java 构造器
导读 构造器是编程的强大组件.使用它们来释放 Java 的全部潜力. 在开源.跨平台编程领域,Java 无疑(?)是无可争议的重量级语言.尽管有许多伟大的跨平台框架,但很少有像 Java 那样统一和直 ...
- zoj3826 Hierarchical Notation (字符串模拟)
Hierarchical Notation Time Limit: 2 Seconds Memory Limit: 131072 KB In Marjar University, stude ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
随机推荐
- UGUI 过渡动画插件,模仿NGUI的Tween (转载)
最近在相亲,后来好朋友跟我说他写了一个好插件,于是我就把女朋友甩了,看看他的插件,可以在UGUI制作简单过渡动画. 我看了下是模仿NGUI的Tween, 我在筱程的基础上稍微改到人性化, 简单支持的让 ...
- js判断是否是数字通用写法
function isNumber(value){ var isNumber = value.match(/^(-?\d+)(\.\d+)?$/g) !=null; if(value.substrin ...
- IoC容器Autofac正篇之简单实例
先上一段代码. namespace ConsoleApplication3 { class Program { static void Main(string[] args) { ContainerB ...
- 用phantomjs 进行网页整页截屏
写截取整个网页程序是一个做前台的哥们所托,要做一些漂亮的界面原形,参考一些不错的网站设计就帮他弄了个截屏的程序. phantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面 ...
- MongoDB学习笔记04
创建索引使用ensureIndex方法,对于同一个集合,同样的索引只需要创建一次,反复创建是徒劳的. 对某个键的索引会加速对该键的查询,然而,对于其它查询可能没有帮助,即便是查询中包含了被索引的键.实 ...
- 1215.1——动态分配内存的补充realloc
当再次在原来申请的内存基础上再加内存的时候用realloc,如果第一次分配的内存后面存储地方够用,则连着原来的申请,如果不够用,就重新找到一块够用的地方,然后把原来的复制过去 int main(int ...
- UIView的一些常用属性和方法
UIView的一些常用属性和方法 1. UIView的属性 UIView继承自UIResponder,拥有touches方法. - (instancetype)initWithFrame:(CGRec ...
- C#中的委托和游戏中的运用
C#中的委托与游戏中的运用 1.什么是委托 在C/C++中,有函数指针的概念,即指向函数的指针.当我们调用该指针时,就相当于调用了该指针所指向的函数,这就是函数指针的一个作用,而他另一个作用就是将自己 ...
- java集合使用——HashMap
在map中插入.删除和定位元素时,HashMap是最好的选择.如果要按照自然顺序或自定义顺序遍历(获取所有元素),那么treemap更好一些. 第一:构造和添加元素 HashMap map = new ...
- JQuery easyui (2)Droppable(放置)组件
所谓放置,就是将一个物体放入一个物体内,当然对于easyui来说触发各种效果是必不可少的,同时这个组件也不会依赖于其他组件. Droppable的加载方式 1,class 加载 一直不太喜欢cl ...