「洛谷」P1553 数字反转(升级版)

题目链接:https://www.luogu.com.cn/problem/P1553

分析:这道题目主要考察了字符串的基本操作,有一些情况需要特判

小数点:去前导零,后导零,当某一部分全为零时,保留零(特殊情况:0.0)

百分号:与整数同(特殊情况:0%)

分号:两次整数操作(特殊情况:0/1234)

AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 string s;
 4 int main(){
 5     cin>>s;
 6     for(int i = 0;i<s.size();i++){
 7         if(s[i]=='.'){
 8             int biao1 = i-1;
 9             while(s[biao1]=='0'){
10                 biao1-=1;
11             }
12             if(biao1==-1){
13                 cout<<'0';
14             }else{
15                 for(int j = biao1;j>=0;j--){
16                     cout<<s[j];
17                 }
18             }
19             
20             
21             int biao2 = i+1;
22             while(s[biao2]=='0'){
23                 biao2+=1;
24             }
25             if(biao2==s.size()){
26                 cout<<'.';
27                 cout<<'0';
28                 return 0;
29             }else{
30                 cout<<'.';
31             }
32             for(int j = s.size()-1;j>=biao2;j--){
33                 cout<<s[j];
34             }
35             return 0;
36         }
37         if(s[i]=='/'){
38             int biao3 = i-1;
39             while(s[biao3]=='0'){
40                 biao3-=1;
41             }
42             if(biao3==-1){
43                 cout<<'0';
44             }else{
45                 for(int j = biao3;j>=0;j--){
46                     cout<<s[j];
47                 }
48             }
49         
50             cout<<'/';
51             int biao4 = s.size()-1;
52             while(s[biao4]=='0'){
53                 biao4-=1;
54             }
55             for(int j = biao4;j>=i+1;j--){
56                 cout<<s[j];
57             }
58             return 0;
59         }
60         if(s[i]=='%'){
61             int biao5 = i-1;
62             while(s[biao5]=='0'){
63                 biao5-=1;
64             }
65             if(biao5==-1){
66                 cout<<'0';
67             }else{
68                 for(int j = biao5;j>=0;j--){
69                     cout<<s[j];
70                 } 
71             }
72             
73             cout<<'%';
74             return 0;
75         }
76     }
77     if(s.size()==1&&s=="0"){
78         cout<<0<<endl;
79         return 0;
80     }
81     int biao6 = s.size()-1;
82     while(s[biao6]=='0'){
83         biao6-=1;
84     }
85     
86     for(int i = biao6;i>=0;i--){
87         cout<<s[i];
88     }
89     return 0;
90 } 

猜你喜欢

转载自www.cnblogs.com/Xinaop/p/13171831.html
今日推荐