|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- // https://www.luogu.com.cn/problem/P1553
- #include <iostream>
- #include <string>
- using namespace std;
- string reverse(string s)
- {
- int i;
- string a = "";
- for (i = s.size() - 1; i >= 0; i--)
- a += s[i];
- return a;
- }
- int count_zero_front(string s)
- {
- int i = 0, j;
- for (j = 0; j < s.size(); j++)
- {
- if (s[j] == '0')
- i++;
- else
- break;
- }
- return i;
- }
- int count_zero_end(string s)
- {
- int i = 0, j;
- for (j = (s.size() - 1); j >= 0; j--)
- {
- if (s[j] == '0')
- i++;
- else
- break;
- }
- return i;
- }
- int main()
- {
- string a;
- getline(cin, a);
- if (a.find('.') != string::npos)
- {
- int pos = a.find('.');
- string t = reverse(a.substr(0, pos));
- string s = reverse(a.substr(pos + 1, a.size() - 1));
- t.erase(0, count_zero_front(t));
- s.erase((s.size() - count_zero_end(s)), count_zero_end(s));
- cout << ((t != "") ? t : "0") << "." << ((s != "") ? s : "0");
- }
- else if (a.find('/') != string::npos)
- {
- int pos = a.find('/');
- string t = reverse(a.substr(0, pos));
- string s = reverse(a.substr(pos + 1, a.size() - 1));
- t.erase(0, count_zero_front(t));
- s.erase(0, count_zero_front(s));
- cout << ((t != "") ? t : "0") << "/" << ((s != "") ? s : "0");
- }
- else if (a.find('%') != string::npos)
- {
- int pos = a.find('%');
- string t = reverse(a);
- t.erase(0, 1);
- t.erase(0, count_zero_front(t));
- cout << ((t != "") ? t : "0") << "%";
- }
- else
- {
- string t = reverse(a);
- t.erase(0, count_zero_front(t));
- cout << ((t != "") ? t : "0");
- }
- return 0;
- }
复制代码 |
|