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网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points