Skip to content

Commit

Permalink
[1주차] 해바라기반 - 조현창 (#3)
Browse files Browse the repository at this point in the history
* [1주차 해바라기반]: 조현창 - 1541 잃어버린 괄호 Silver2(#2)

* [1주차 해바라기반]: 조현창 - 1541 잃어버린 괄호 Silver2 2회차(#2)

* [1주차 해바라기반] : 조현창 - 1541 잃어버린 괄호 Silver2 (#2) - 3회차 stack쓰기
  • Loading branch information
vcho1958 authored Apr 16, 2022
1 parent 8335507 commit 6e746ed
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 해바라기반/조현창/1541잃어버린 괄호 try2.cpp
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;
}
35 changes: 35 additions & 0 deletions 해바라기반/조현창/1541잃어버린 괄호 try3-stack.cpp
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;
}
53 changes: 53 additions & 0 deletions 해바라기반/조현창/1541잃어버린 괄호.cpp
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;
}

0 comments on commit 6e746ed

Please sign in to comment.