• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

3周前 (04-21) 7次浏览

# 一道很不错的细节模拟题：洛谷P5587 打字练习

## 我的AC代码

``````#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

using ui = unsigned int;

string process(string str) {
ui nlen = str.size();
string ret = "";
stack<char> stk;
for (ui i = 0; i < nlen; ) {
if ('<' != str[i]) {
stk.push(str[i]);
++i;
}
else {
if (!stk.empty()) {
while (i < nlen && '<' == str[i] && !stk.empty()) {
stk.pop();
++i;
}
}
else {
++i;
}
}
}
while (!stk.empty()) {
ret += stk.top();
stk.pop();
}
reverse(ret.begin(), ret.end());
return ret;
}

int main() {
vector<string> vec;
string curStr;
while (getline(cin, curStr) && "EOF" != curStr) {
curStr = process(curStr);
vec.push_back(curStr);
}
int j = 0, cnt = 0, times, ans;
while (getline(cin, curStr) && "EOF" != curStr) {
curStr = process(curStr);
string modelStr = vec[j];
ui nlen = (curStr.size() < modelStr.size()) ? curStr.size() : modelStr.size();
for (ui i = 0; i < nlen; ++i) {
if (modelStr[i] == curStr[i]) {
++cnt;
}
}
++j;
}
cin >> times;
ans = (int)(cnt * 60.0 / times + 0.5);
cout << ans << endl;
return 0;
}
``````

## 稍微注意一下“四舍五入”的问题

（1）四舍五入到整数：

``````ans = (int)(ans + 0.5)
``````

（2）四舍五入到1位小数：

``````ans = (int)(ans * 10 + 0.5) * 0.1
``````

（3）四舍五入到2位小数：

``````ans = (int)(ans * 100 + 0.5) * 0.01
``````

（3）四舍五入到N位小数：

a

n

s

=

(

i

n

t

)

(

x

1

0

n

+

0.5

)

1

0

n

ans = (int)(x*10^n+0.5)*10^{-n}

ans=(int)(x10n+0.5)10n