只是一道讨厌的模拟。建议难度:普及。
先在原始字符串上提取出每个子串。为了方便最后的处理,可以在原输入字符串最后加上一个分号。
每读到一个分号或逗号,当即进行判断。
写一个函数判断
如果是数字串,就 ans1+=t,否则就是字母串,ans2+=t。
为了方便输出,每 +=t 后都再加一个逗号。
最后了。如果没有数字串,就输出 -。不然,就把
如果字符串有前导零,只用看字符串的第一位是不是
但如果字符串就是 0,应当特判。
对字符串进行遍历。只要字符串出现非数字的字符,直接 return false。
否则 return true。
千万不要抄,小心棕名!
xxxxxxxxxx// 600A Extract Numbersusing namespace std; bool isNumber(string s){ if(s=="0") return true; if(s[0]=='0') return false; for(int i=0; i<s.size(); i++) if(!('0'<=s[i] && s[i]<='9')) return false; return true;} int main(){ string s; cin>>s; s+=';'; string t; string ans1; string ans2; for(int i=0; i<s.size(); i++) { if(s[i]!=',' && s[i]!=';') t+=s[i]; else { if(t=="") ans2+=','; else if(isNumber(t)) ans1+=t, ans1+=','; else ans2+=t, ans2+=','; t.clear(); } } if(ans1=="") cout<<"-"<<endl; else { ans1.erase(ans1.size()-1); cout<<"\""<<ans1<<"\""<<endl; } if(ans2=="") cout<<"-"; else { ans2.erase(ans2.size()-1); cout<<"\""<<ans2<<"\""; } return 0;}All Rights Reserved 2022 Wang Zhanrui