Create N number of variable-length arrays and find a element from quaries
Solution
If you know vector in C++, it would be easy problem.
you initialize vector of vector a and insert variable sized vector into a.
# Variable Sized Arrays
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n = 0;
int q = 0;
cin >> n >> q;
vector<vector<int>> a;
for(int i = 0 ; i < n ; i++){
int k = 0;
cin >> k;
vector<int> v;
int value = 0;
int index = 1;
while(cin >> value){
v.push_back(value);
index++;
if (index > k){
break;
}
}
a.push_back(v);
}
for(int i = 0; i < q ; i++){
int index, j ;
cin >> index >> j;
cout << a[index][j] << endl;
}
return 0;
}
Read 3 numbers from stdin and print their sum to stdout
Solution
This challenge is to practice reading input from stdin(standard input stream) and printing out to stdout(standard output stream).
// Input and Output
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int a, b,c ;
int result = 0;
cin >> a >> b >> c;
result = a + b + c;
cout << result << endl;
return 0;
}
We also need to install a compiler. C++ is a compiled language so we must complie before it can be run in your machine. Unfortunately, VS code is an editor and does not include a C++ compiler or debugger. Therefore, you need to install a compiler additionally.
But, maybe you already have a compiler. Check if you have a compiler.
Please open Command Prompt in your machine and write this command.
This command is to check for the GCC compiler:
g++ --version
If you can see g++ version like below, you have already a compiler.
If you don't have any compiler, I recommend to you to install MinGW-x64 via MSYS2. The MSYS2 link is here(MSYS2)
If you install all toolchain, now you need to add the MinGW compiler to your environment PATH.
Please follow these steps.
1. open Windows settings.
2. search for Edit environment variables for your account.
3. Choose the Path variable in your User variables and then select Edit.
4. add your mingw-w64 installed path into Path. If you don't change any installation path, it should be C:\msys64\mingw64\bin.
5. Select OK.
Now, we can check your MinGW compiler.
Please open Command Prompt in your machine and write this command.
gcc --version
g++ --version
gdb --version
If you can see all version, you succeed to install MinGW.
But if you cannot see all, please check your PATHcorrect and C:\msys64\mingw64\bin folder has files. If you don't have files in C:\msys64\mingw64\bin folder, you need to install full Mingw-w64 toolchain again.
3. Run Hello World
So now, we need to check the compiler is working fine.
Let's open VS code and create a folder HelloWord and create helloworld.cpp file.
I just created one more parent folder "C++". But it doesn't matter!
Now, please paste this script into your source code(helloworld.cpp) and save source code: