AcWing 3504. 字符串转换整数
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e6 + 10;
int t, n, m, k, l, r, op, y;
ll x;
string str;
void solve() {
getline(cin, str);
str.push_back('x');
x = -1;
for (char&ch : str) {
if (isdigit(ch)) {
if(x==-1)x=0;
x = x * 10 + (ch - '0');
} else if (x!=-1 && isalpha(ch)) {
if (x > INT_MAX) {
cout << -1;
} else {
cout << x;
}
return ;
}
}
cout << -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}