๋ฌธ์
์ฝ๋
#include <iostream>
#include <string>
using namespace std;
string s, temp;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> s;
bool minus = false;
int ans = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '-' || s[i] == '+') { //์ฐ์ฐ์์ผ๋
if (minus) ans -= stoi(temp); //๋ง์ด๋์ค ์ฐ์ฐ์๊ฐ ๋์จ์ํ์ด๋ฉด ๊ฐ์ ๋นผ์ค๋ค.
else ans += stoi(temp);
temp.clear();
if (s[i] == '-') minus = true;
}
else temp += s[i]; //ํผ์ฐ์ฐ์์ผ๋
}
if (minus) ans -= stoi(temp); //๋ง์ง๋ง ์ฐ์ฐ์ ์ดํ์ ์๋ฅผ ๋ง๋ฌด๋ฆฌํด์ค๋ค.
else ans += stoi(temp);
cout << ans;
}
ํ์ด
๋บ์ ์ดํ๋ง๋ค ๊ดํธ๋ฅผ ์น๋ฉด ์ต์๊ฐ ๋ฉ๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 15552๋ฒ - ๋น ๋ฅธA+B (0) | 2021.11.17 |
---|---|
[๋ฐฑ์ค,c++] 15482๋ฒ - ํ๊ธ LCS (0) | 2021.11.17 |
[๋ฐฑ์ค,c++] 1547๋ฒ - ๊ณต (0) | 2021.11.17 |
[๋ฐฑ์ค,c++] 15240๋ฒ - Paint bucket (0) | 2021.11.17 |
[๋ฐฑ์ค,c++] 1504๋ฒ - ํน์ ํ ์ต๋จ ๊ฒฝ๋ก (0) | 2021.11.17 |
[๋ฐฑ์ค,c++] 14950๋ฒ - ์ ๋ณต์ (0) | 2021.11.14 |
๋๊ธ