您的位置:首页 > 其它

ZOJ 3930 Dice Notation【模拟】【字符串】

2016-04-12 10:10 549 查看

题目链接

http://www.icpc.moe/onlinejudge/showProblem.do?problemId=5690

思路

题目有点长,其实前面都是废话,直接看样例都能看懂。

三件事

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.

题目本身很简单,但有个坑就是,d后面的数,或者直接数字,是有可能出现大数的,比赛时就栽这个坑里了导致没过。

AC代码

#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;
for (int i = 0; i < s.length(); ++i)
{
if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
{
printf(" %c ", s[i]);
}
else if (isdigit(s[i]))
{
int j;
string out_num;//大数输出用
for (j = i; j < s.length() && isdigit(s[j]); ++j)
{
num *= 10;
num += s[j] - '0';
out_num += s[j];
}
j--;
i = j;
if (i == s.length() - 1 || s[i + 1] != 'd')
{
printf("%s", out_num.c_str());
num = 0;
}
}
else if (s[i] == 'd')
{
int j;
string num2;
for (j = i + 1; j < s.length() && isdigit(s[j]); ++j)
{
num2 += s[j];
}
j--;
i = j;
if (num == 1 || num == 0)
{
printf("[d%s]", num2.c_str());
}
else
{
printf("(");
for (int j = 0; j < num; ++j)
{
if (j == 0)printf("[d%s]", num2.c_str());
else printf(" + [d%s]", num2.c_str());
}
printf(")");
}
num = 0;
}
else if (s[i] == '(' || s[i] == ')')
{
printf("%c", s[i]);
}
}
printf(" = [Result]\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm