因为当时做笔试的时候,是从后向前做的(第三题是大数相乘,思路比较清晰)
第二题卡了很久,所以这题的时间不是很充裕,就用了最直观的方法写的
当时没AC,通过率百分之80,现在想来应该是输出空的时候有问题吧,日后还会精进,这次就先拿来丢人现眼一下吧。
思路是:
/*得到字符串,定义一个bool,控制是否开始累加,如果前面的为字符,后面的为数字,则开始
如果前面为数字,后面为字符则结束,定义一个vector用来存开始和长度,定义一个N为vector的大小
每当结束时,N+1*/
void CalLong(string input) {
bool IsNum = false;
string value = input;
vector<pair<int, int>> StartAndCount;
int Count = 0;
int n = value.size()-1;
int NUM = 0;
if (value[0] - '0' <= 9 && value[0] - '0' >= 0) {
IsNum = true;
StartAndCount.push_back(make_pair(0,0));
}
for (int i = 1; i <= n; i++) {
if (value[i] - '0' <= 9 && value[i] - '0' >= 0 ) {
if (value[i - 1] - '0' > 9 || value[i - 1] - '0' < 0)
IsNum = true;
}
if (IsNum) {
Count++;
}
if (value[i] - '0' <= 9 && value[i] - '0' >= 0 ) {
if (value[i + 1] - '0' > 9 || value[i + 1] - '0' < 0) {
IsNum = false;
StartAndCount.push_back(make_pair(0, 0));
StartAndCount[NUM].first = i;
StartAndCount[NUM].second = Count;
Count = 0;
NUM++;
}
}
}
int num = 0;
for (int i = 0; i < StartAndCount.size(); i++) {
int max = StartAndCount[0].second;
if (max < StartAndCount[i].second) {
max = StartAndCount[i].second;
num = i;
}
}
if (Count > 0) {
cout << value.substr(StartAndCount[num].first + 1 - StartAndCount[num].second, StartAndCount[num].second) << "," << StartAndCount[num].second << endl;
}
else {
cout << "\"\""<<endl;
}
}
int main() {
string input;
cin >> input;
CalLong(input);
system("pause");
}