ZOJ-3930-Dice Notation

2016河南省第九届ACM程序设计竞赛[正式赛四]
C - C
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit

Status

Practice

ZOJ 3930
Description

I want to get some water from this strange lake. I have a bottle.
OK.
Then I want to go forward to look into the parterre.
More details?
Err..What will happen if I let some water drip on the flowers?
…Err…The flowers will all become super-huge monsters and it will be very dangerous.
Ready to fight.
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:

::= “+”
| “-”
|
::= “*”
| “/”
|
::= “(” “)”
|
|
::=
|
::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
::= “d”
| “d”
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 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前系数合并到一种情况了,然后就是循环输出d后的数,其他的什么符号都原样输出,注意空格要求,而且最后还要有换行

中间为了方便用到了STL里的string类
string类用法:http://blog.csdn.net/qq_32680617/article/details/51122395
代码

#include<iostream>
#include<cstdio>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<string>
#include<cctype>
using namespace std;
int main()
{
    int T;
    cin>>T;
    getchar();
    while(T--)
    {
        string s;
        getline(cin,s);
        int num=0;//d前的系数
        for(int i=0; i<s.length(); i++)
        {
            if(isdigit(s[i]))//处理d前的系数和纯数字的情况
            {
                int j;
                string s1;//纯数字可能是大数
                for(j=i; j<s.length()&&isdigit(s[j]); j++)
                {
                    num=num*10+s[j]-'0';//如果是系数,记录下来
                    s1+=s[j];//如果是纯数字,可能是大数
                }
                j--;//细节
                i=j;
                if(i==s.length()-1||s[i+1]!='d')//如果这串数字到了边界或者这串数字不是系数而是纯数字
                {
                    cout<<s1;
                    num=0;
                }
            }
            else if(s[i]=='d')//处理d及其后面的数
            {
                int j;
                string s2;
                for(j=i+1; j<s.length()&&isdigit(s[j]); j++)
                    s2+=s[j];//记录下d后面的内容
                j--;
                i=j;
                if(num==0||num==1)//如果系数为1或者不存在
                    cout<<"[d"<<s2<<']';
                else
                {
                    for(int k=0; k<num; k++)//循环输出系数次
                    {
                        if(k==0)
                            cout<<"([d"<<s2<<']';
                        else
                            cout<<" + [d"<<s2<<']';
                    }
                    cout<<')';
                }
                num=0;
            }
            else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
                cout<<' '<<s[i]<<' ';//这些都原样输出,注意控制空格
            else if(s[i]=='('||s[i]==')')
                cout<<s[i];
        }
        cout<<" = [Result]"<<endl;
    }
    return 0;
}

没有写题意,因为我的英语和我的代码能力一样渣

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值