C++

Premise

To be more exact, we should include all the needed headers first.

For example:

1
2
3
4
#include <iostream> // for I/O
#include <iterator> // for istream_iterator
#include <vector> // for vector
#include <string> // for string

Actually, since we are usually hosted on machines with GCC, we can include only:

1
#include <bits/stdc++.h>

Which makes everything from the STL available. Remember this header is not standard.

Reading an array of N elements

The first line contains N, the number of elements.

The next line contains N space-separated integers.

1
2
3
4
int N;
std::cin >> N;
std::vector<int> v(N);
std::copy_n(std::istream_iterator<int>(std::cin), N, begin(v));

Using copy_n is the best choice because we can use it to read more things in sequence:

1
2
3
4
5
6
int N1, N2;
std::cin >> N1 >> N2;
std::vector<int> v1(N1);
std::vector<int> v2(N2);
std::copy_n(std::istream_iterator<int>(std::cin), N1, begin(v1));
std::copy_n(std::istream_iterator<int>(std::cin), N2, begin(v2));

Reading strings

We can simply use std::string:

1
2
std::string s;
std::cin >> s;

If the string length is introduced first, we can either read it:

1
2
3
4
int N;
std::cin >> N; // not used
std::string s;
std::cin >> s;

or ignore it (there are many ways, here is the simplest):

1
2
std::string s;
std::cin >> s >> s;

Output

We can easily print values and strings by sending them to cout:

1
std::cout << value << " " << stringValue << "\n";

Printing arrays

Vectors and arrays are easy to std::copy to standard output.

If v is a vector or array of ints, we can print its content separated by whitespaces this way:

1
std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));

If we must omit the last whitespace, we can do the following refinement:

1
2
std::copy(begin(v), std::prev(end(v)), std::ostream_iterator<int>(std::cout, " "));
std::cout << *std::prev(end(v));

Floating point with exact precision

Some challenges require printing floting points with a certain width.

In such cases, we use std::fixed in combination with std::setprecision(numberOfDigits) can be used.

For instance, suppose we want only two digits after comma:

1
cout << fixed << setprecision(2) << value << "\n";

Or suppose we want only one digit after comma:

1
cout << fixed << setprecision(1) << value << "\n";

Date to index

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
time_t to_time(const string& s)
{
    tm t{};
    sscanf(s.c_str(), "%d-%d-%d", &t.tm_year, &t.tm_mon, &t.tm_mday);
    t.tm_year -= 1900;
    t.tm_mon -= 1;
    return mktime(&t);
}

int dateToindex(int year, int month, int day, const string& date)
{
    tm origin{};
    origin.tm_year = year - 1900;
    origin.tm_mon = month;
    origin.tm_mday = day;
    time_t origin_time = mktime(&origin);

    return (to_time(date) - origin_time) / 86400;
}

Get next day

Get the next day of a date in string format YYYY-MM-DD:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
std::string getNextDay(const std::string& dateStr) 
{
    int y, m, d;
    sscanf(dateStr.c_str(), "%d-%d-%d", &y, &m, &d);
    
    // Days in each month
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    // Check for leap year
    const bool isLeap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
    if (isLeap) 
        daysInMonth[1] = 29;
    
    // Increment day
    d++;
    
    // Check if we exceeded the month
    if (d > daysInMonth[m - 1])
    {
        d = 1;
        m++;
        
        // Check if we exceeded the year
        if (m > 12)
        {
            m = 1;
            y++;
        }
    }
    
    // Format back to string
    std::ostringstream oss;
    oss << std::setfill('0') << std::setw(4) << y << "-"
        << std::setw(2) << m << "-"
        << std::setw(2) << d;
    
    return oss.str();
}
comments powered by Disqus