View Code of Problem 3855

#include<bits/stdc++.h>
using namespace std;
int main() {
	int T,sum;
	string s; 
	cin>>T;
	while(T--) {
		cin>>s;
		sum=0;
		int flag=1;
		for(int i=0; i<s.length(); i++) {
			if(s[i]>='0'&&s[i]<='9') {//数字
				string b="";
				int k=0, a=0;
				b+=s[i];
				while(i+1<s.length()&&s[i+1]>='0'&&s[i+1]<='9') {
					i++;
					b+=s[i];
				}
				for(int j=b.length()-1; j>=0; j--) {
					a+=(b[j]-'0')*pow(10,k++);
				}
				sum+=a*flag;
			}else if(s[i]=='-') {
				flag=-1;
			}else {
				flag=1;
			}
		}
		cout<<sum<<endl;
	}
}

Double click to view unformatted code.


Back to problem 3855