鱼C论坛

 找回密码
 立即注册
查看: 1196|回复: 0

[技术交流] 洛谷题目 P1553

[复制链接]
发表于 2020-1-19 22:38:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. // https://www.luogu.com.cn/problem/P1553

  2. #include <iostream>
  3. #include <string>

  4. using namespace std;

  5. string reverse(string s)
  6. {
  7.     int i;
  8.     string a = "";
  9.     for (i = s.size() - 1; i >= 0; i--)
  10.         a += s[i];
  11.     return a;
  12. }

  13. int count_zero_front(string s)
  14. {
  15.     int i = 0, j;
  16.     for (j = 0; j < s.size(); j++)
  17.     {
  18.         if (s[j] == '0')
  19.             i++;
  20.         else
  21.             break;
  22.     }
  23.     return i;
  24. }

  25. int count_zero_end(string s)
  26. {
  27.     int i = 0, j;
  28.     for (j = (s.size() - 1); j >= 0; j--)
  29.     {
  30.         if (s[j] == '0')
  31.             i++;
  32.         else
  33.             break;
  34.     }
  35.     return i;
  36. }

  37. int main()
  38. {
  39.     string a;
  40.     getline(cin, a);

  41.     if (a.find('.') != string::npos)
  42.     {
  43.         int pos = a.find('.');
  44.         string t = reverse(a.substr(0, pos));
  45.         string s = reverse(a.substr(pos + 1, a.size() - 1));
  46.         t.erase(0, count_zero_front(t));
  47.         s.erase((s.size() - count_zero_end(s)), count_zero_end(s));
  48.         cout << ((t != "") ? t : "0") << "." << ((s != "") ? s : "0");
  49.     }
  50.     else if (a.find('/') != string::npos)
  51.     {
  52.         int pos = a.find('/');
  53.         string t = reverse(a.substr(0, pos));
  54.         string s = reverse(a.substr(pos + 1, a.size() - 1));
  55.         t.erase(0, count_zero_front(t));
  56.         s.erase(0, count_zero_front(s));
  57.         cout << ((t != "") ? t : "0") << "/" << ((s != "") ? s : "0");
  58.     }
  59.     else if (a.find('%') != string::npos)
  60.     {
  61.         int pos = a.find('%');
  62.         string t = reverse(a);
  63.         t.erase(0, 1);
  64.         t.erase(0, count_zero_front(t));
  65.         cout << ((t != "") ? t : "0") << "%";
  66.     }
  67.     else
  68.     {
  69.         string t = reverse(a);
  70.         t.erase(0, count_zero_front(t));
  71.         cout << ((t != "") ? t : "0");
  72.     }

  73.     return 0;
  74. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-27 01:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表