A-牛牛爱字符串
牛牛爱字符串
https://ac.nowcoder.com/acm/contest/6885/A
题意
给定一个字符串,提取其中的数字并以空格分隔输出,字符串长度
思路
遍历一遍字符串,遇见连续数字存到一个vector<string>
里,最后处理前导零即可。
AC代码:
#include<cmath> #include<cstdio> #include<vector> #include<queue> #include<cstring> #include<iomanip> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; string f(string s) { int f = 0; string t = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == '0' && f == 0) continue; else { f = 1; t += s[i]; } } if(t.size()==0) t="0"; return t; } int main() { string str; vector<string> ans; while (getline(cin, str)) { ans.clear(); int cnt = 0; for (int i = 0; i < str.size(); i++) { if (str[i] <= '9' && str[i] >= '0') { string tmp = ""; while (str[i] <= '9' && str[i] >= '0') { tmp += str[i]; i++; } ans.push_back(tmp); } } for (int i = 0; i < ans.size(); i++) { cout << f(ans[i]); if (i < ans.size() - 1) cout << ' '; } cout << "\n"; } }