diff options
author | Santo Cariotti <sancn@live.com> | 2017-04-26 16:37:39 +0200 |
---|---|---|
committer | Santo Cariotti <sancn@live.com> | 2017-04-26 16:37:39 +0200 |
commit | 483d63fa7249ad8d6020680c48c3cf6df35010b3 (patch) | |
tree | 2f8649e3ae6b42ace5011246285c9c450f004222 /cpp/parentesi.cpp | |
parent | 6c957dc4e01aee6ce9cae3c8342d04b0fd9ca9c4 (diff) |
Moved all C++ files into CPP folder
Diffstat (limited to 'cpp/parentesi.cpp')
-rw-r--r-- | cpp/parentesi.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/cpp/parentesi.cpp b/cpp/parentesi.cpp new file mode 100644 index 0000000..159b75f --- /dev/null +++ b/cpp/parentesi.cpp @@ -0,0 +1,58 @@ +/* INPUT: +[ ( ( [ { { [ ] ] ( ( ( } } ( ) ) ) ) ) ) ] ] ] + +output: +11 */ +#include <iostream> +#include <string.h> +#include <fstream> + +using namespace std; + +int main() +{ + ifstream in; + ofstream out; + in.open("input.txt"); + out.open("output.txt"); + + int i; + int t = 0, q = 0, g = 0; //tonde, quadre, graffe + char str[1000], c; + i = 0; + while(!in.eof()) + { + in.get(c); + if(c == ' ') continue; + else { + str[i] = c; + i++; + } + } + + int n = strlen(str), tot = 0; + + for(int i = 0; i < n; i++) + { + if(str[i] == '(') t++; + else if(str[i] == '[') q++; + else if(str[i] == '{') g++; + + if(str[i] == ')' && t > 0) { + t--; + tot++; + } else if(str[i] == ']' && q > 0) { + q--; + tot++; + } else if(str[i] == '}' && g > 0) { + g--; + tot++; + } + } + + out << tot << endl; + + in.close(); + out.close(); + return 0; +} |