【洛谷-P1553】解题报告(字符串)

原始题目

P1553 数字反转(升级版)

题目大意

小数,分数,百分数,整数按照对应的规则反转后输出。

解题思路

按要求模拟操作就行

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define per(i, a, n) for (int i = n - 1; i >= a; --i)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 1e5 + 5;
string s, s1, s2;
//写成函数形式
void checkprint(string sss)
{
reverse(all(sss));
int flag = 1;
rep(i, 0, sss.length())
{
if (sss[i] == '0' && flag)
continue;
cout << sss[i];
flag = 0;
}
if (flag)
cout << 0;
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> s) {
int temp1 = s.find('/');
int temp2 = s.find('.');
int temp3 = s.find('%');
if (temp1 != -1) {
s1 = s.substr(0, temp1);
s2 = s.substr(temp1 + 1, s.length() - s1.length() - 1);
checkprint(s1);
cout << "/";
checkprint(s2);
cout << endl;
} else if (temp2 != -1) {
s1 = s.substr(0, temp2);
checkprint(s1);
cout << ".";
s2 = s.substr(temp2 + 1, s.length() - s1.length() - 1);
int tempi = 0;
while (tempi < s2.length() && s2[tempi] == '0')
tempi++;
if (tempi == s2.length())
cout << 0 << endl;
else {
string s3 = s2.substr(tempi, s2.length() - tempi);
reverse(all(s3));
cout << s3 << endl;
}
} else if (temp3 != -1) {
s1 = s.substr(0, temp3);
checkprint(s1);
cout << "%" << endl;
} else {
checkprint(s);
cout << endl;
}
}
}

收获与反思

注意细节,印象中去0还有百分数开始写崩了。