-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [1주차 해바라기반]: 조현창 - 1541 잃어버린 괄호 Silver2(#2) * [1주차 해바라기반]: 조현창 - 1541 잃어버린 괄호 Silver2 2회차(#2) * [1주차 해바라기반] : 조현창 - 1541 잃어버린 괄호 Silver2 (#2) - 3회차 stack쓰기
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <deque> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
string input; | ||
int res = 0, sign = 1; | ||
cin >> input; | ||
for (int i = 0; i < input.length(); i++) | ||
{ | ||
int tmp = 0; | ||
if (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
while (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
tmp *= 10; | ||
tmp += input[i] - '0'; | ||
i++; | ||
} | ||
} | ||
res += tmp * sign; | ||
if (i < input.length() && input[i] == '-') | ||
sign = -1; | ||
} | ||
cout << res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
#include <stack> | ||
#include<string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
stack<char> cstack; | ||
int res = 0, sign = 1, sub = 0; | ||
char input; | ||
while((input = cin.get()) != '\n'){ | ||
cstack.push(input); | ||
} | ||
while(cstack.size()){ | ||
char top = cstack.top(); | ||
if(cstack.top() == '-'){ | ||
sub += res; | ||
res = 0; | ||
cstack.pop(); | ||
} | ||
else if(cstack.top() >='0' && cstack.top() <= '9'){ | ||
string tmp = ""; | ||
while(cstack.size() && cstack.top() >='0' && cstack.top() <= '9'){ | ||
tmp = string(1,cstack.top()) + tmp; | ||
cstack.pop(); | ||
} | ||
res += stoi(tmp); | ||
} | ||
else cstack.pop(); | ||
} | ||
cout << res-sub; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <deque> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
string input; | ||
int a, res = 0, tmp = 0, minus = 0, sum = 0, sub = 0; | ||
vector<int> A; | ||
vector<char> op; | ||
|
||
cin >> input; | ||
for (int i = 0; i < input.length(); i++) | ||
{ | ||
if (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
while (input[i] >= '0' && input[i] <= '9') | ||
{ | ||
tmp *= 10; | ||
tmp += input[i] - '0'; | ||
i++; | ||
} | ||
} | ||
A.push_back(tmp); | ||
tmp = 0; | ||
if (i < input.length()) | ||
op.push_back(input[i]); | ||
} | ||
for (int i = 0; i < A.size(); i++) | ||
{ | ||
if (minus) | ||
{ | ||
sub += A[i]; | ||
} | ||
else | ||
{ | ||
res += A[i]; | ||
} | ||
if (i < op.size()) | ||
{ | ||
if (op[i] == '-') | ||
{ | ||
minus = 1; | ||
} | ||
} | ||
} | ||
cout << res - sub; | ||
} |