我的解法
解题思路:此题应该分两步走,分别确定指数和转为有效数位标准格式,确定指数应该根据小数点的位置来确定。小数点前面有几个整数,指数就为几;小数点后面要看第一个整数前面有几个0,有几个0就为负几。确定有效数位,从第一个整数开始,数n个数,如果缺数,用0补齐。
需要注意的c++语法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| int f; if(e>0) f=p-e; else f=p-e+1; cout<<"f:"<<f<<endl;
res.append(1,'0'); res+='0';
stringstream ss; ss<<e; string st=ss.str(); res+=st;
|
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <cstdio> #include <algorithm> #include <string> #include <iostream> #include <cctype> #include <cstring> #include <sstream>
using namespace std;
int findP(string s){ int l=s.length(); int res=0; if(s.find('.')!=string::npos){ int p=s.find('.'); int i=0; bool beforeHas=false; bool afterHas=true; while(i<p){ if(s[i]!='0'){ beforeHas=true; break; } else i++; }
res=p-i; i=p+1; if(!beforeHas){ while(i<l){ if(s[i]=='0'){ res--; afterHas=false; } else{ afterHas=true; break; } i++; } } if(!afterHas){ res=0; } }else{ int j=0; while(j<l){ if(s[j]!='0') break; else j++; } res=l-j; } return res; }
string changeTo(string s,int n,int e){ string res="0."; int len=s.length(); if(s.find('.')!=string::npos){ int p=s.find('.'); int f; if(e>0) f=p-e; else f=p-e+1;
for(int i=0;i<n;){ if(f<len){ if(isdigit(s[f])){ res.append(1,s[f]); f++; i++; }else{ f++; } }else{ res.append(1,'0'); i++; } } }else{ int j=0,t=0; while(j<len&&t<n){ if(s[j]!='0'){ res.append(1,s[j]); j++; t++; break; } j++; } while(t<n){ if(j<len){ res.append(1,s[j]); j++; }else{ res.append(1,'0'); } t++; } } res+="*10^";
stringstream ss; ss<<e; string st=ss.str(); res+=st; return res; }
int main(){ int n; string s1,s2; cin>>n>>s1>>s2; int e1=findP(s1); int e2=findP(s2);
string ss1=changeTo(s1,n,e1); string ss2=changeTo(s2,n,e2);
if(ss1==ss2){ cout<<"YES "<<ss1; }else{ cout<<"NO "<<ss1<<" "<<ss2; } return 0; }
|
算法笔记解法
解题思路:巧妙利用string的erase方法,将前导零和小数点去掉。也是寻找指数和有效位,先根据小数点寻找指数,最后确定有效位数,在寻找指数的过程中将小数点和零从字符串中清除。
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
| #include <iostream> #include <string> using namespace std; int n;
string deal(string s,int& e){ int k=0; while(s.length()>0&&s[0]=='0') s.erase(s.begin()); if(s[0]=='.'){ s.erase(s.begin()); while(s.length()>0&&s[0]=='0'){ e--; s.erase(s.begin()); } }else{ while(k<s.length()&&s[k]!='.'){ k++; e++; } if(k<s.length()) s.erase(s.begin()+k); } if(s.length()==0) e=0; int num=0; k=0; string res=""; while(num<n){ if(k<s.length()) res+=s[k++]; else res+='0'; num++; } return res; }
int main(){ string s1,s2,s3,s4; int e1=0,e2=0; cin>>n>>s1>>s2; s3=deal(s1,e1); s4=deal(s2,e2); if(e1==e2&&s3==s4){ cout<<"YES 0."<<s3<<"*10^"<<e1; }else{ cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2; } return 0; }
|