c++一般解法
在字符串中找出连续最长的数字串
http://www.nowcoder.com/questionTerminal/2c81f88ecd5a4cc395b5308a99afbbec
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> using namespace std; int main(){ string s; while(cin >> s){ string ans,temp; int length = 0, max_length = 0; for(int i = 0; i<s.size(); i++){ if(s[i] >= '0' && s[i] <= '9'){ length ++; temp += s[i]; } if(s[i] < '0' || s[i] > '9' || i == s.size()-1){//非数字或最后一个字符 if(length > max_length){ max_length = length; ans = temp;//起到清空+放入新字符串的作用 } else if(length == max_length){ ans += temp; } length = 0; temp = ""; } } cout << ans << "," << max_length << endl; } }