John Patrick Amata


my blog for algorithms
Dark/Light Mode:  
Share: 

Day 1

today

C++ basics: I/O, data types, operators, conditionals

tried a hello world problem at kattis.com, i think tomorrow i’ll do some more serious problems

// https://open.kattis.com/problems/hello
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!";
}

notes

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    cout << 123 << 123;             // 123123
    cout << "Hello world!" << endl; // Hello World!
    // the 2 above prints 1 line: 123123Hello world!
    cout << "<<" << endl;     //<<
    cout << 10 - 11 << endl;  //-1
    cout << 10 / 3 << endl;   // 3 -- because dividing 2 integers in C++ always returns an integer
    cout << 10.0 / 3 << endl; // 3.33333 -- because now 10 is a float (10.0)
    cout << sqrt(16) << '\n'; // 4 -- requires importing math.h

    // variable
    int x = 1; // int is the datatype, x is the name of the variablle
    cout << x << endl;

    /*
    primitive datatypes:
    int (unsigned int, long long int)
    char
    bool -- will output as 1 or 0
    float (double, long double)
    void
    */
    bool a = true;          // 1
    bool b = false;         // 0
    cout << a << b << endl; // prints: 10
    cout << false << endl;  // 0

    /*
    common derived datatypes:
    string
    vector
    map
    set
    priority_queue
    */

    /*
    assignment operators
    */
    int assignment = 5;
    assignment += 5;            // assignment = assignment  + 5
    cout << assignment << endl; // 10
    assignment *= 10;           // assignment = assignment  * 10
    cout << assignment << endl; // 100

    /*
    unary operators
    +
    -
    ++
    --
    */

    /*
    (a++) post-increment vs (++a) pre-increment
    pre-increment - increment first
    post-increment - incremenent later
    */
    int postIncrement = 5;
    int preIncrement = 5;
    cout << ++preIncrement << endl;  // 6: preIncrement is incremented first then it is used
    cout << postIncrement++ << endl; // 5: postIncrement is used first then it is increased, thus 5 is printed first then we add 6 later
    // let's print both again without using unary operators to post-increment/pre-increment them and notice postIncrement variable... now it's similar to preIncrement. Both are now 6
    cout << preIncrement << endl;  // 6
    cout << postIncrement << endl; // 6

    // input/output
    /*
    cin
    cout
    */
    int input;
    // cin >> input;
    // cout << input;
    // multiple values in 1 line, like here's inputting 3 variables in 1 line
    // cin >> a >> b >> c;
    // then output it in 1 line:
    // cout << a << b << c;

    // to take input of sentences/strings use getline():
    /*
    getline(cin, a);
    getline is not really used much in competitive programming as it is slow
     */

    // conditional statements
    /*
    if(condition) {

    } else if (another condition) {

    } else {

    }
    */
}