John Patrick Amata


my blog for algorithms
Dark/Light Mode:  
Share: 

Day 2

today

solved 5 problems at codeforces

Problem 1

Link: https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/A

// https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/A

#include <iostream>
using namespace std;
int main()
{
    string name;
    cin >> name;
    cout << "Hello, " << name;
}

Notes: note the spacing

Problem 2

Link: https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/C

details (click me) Given two numbers X and Y. Print the summation and multiplication and subtraction of these 2 numbers.
// https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/C

#include <iostream>
using namespace std;

int main()
{
    long long int X, Y;
    cin >> X >> Y;
    cout << X << " + " << Y << " = " << X + Y << endl;
    cout << X << " * " << Y << " = " << X * Y << endl;
    cout << X << " - " << Y << " = " << X - Y << endl;
}

Notes:

Finally found a use case for long long int, this problem gave an input that int cant handle

Also dont forget to watch for spaces and using endl

Problem 3

Link: https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/J

details (click me) Given two numbers A and B. Print "Multiples" if A is multiple of B or vice versa. Otherwise print "No Multiples".
// https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/J
#include <iostream>
using namespace std;
int main()
{
    int A, B;
    cin >> A >> B;
    if (A % B == 0 || B % A == 0)
    {
        cout << "Multiples";
    }
    else
    {
        cout << "No Multiples";
    }
}

Notes:

Getting multiples of numbers: number1 % number2 == 0

Problem 4

Link: https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/F

details (click me) Given two numbers N and M. Print the summation of their last digits.
// https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/F
#include <iostream>
using namespace std;
int main()
{
    long long int n, m;
    cin >> n >> m;
    int sum = (n % 10) + (m % 10);
    cout << sum << endl;
}

Notes:

Learned a trick to get the last digit of a number, just number % 10.

Another use case for long long int, this problem gave an input that int cant handle

Problem 5

Link: https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/T

details (click me) Given three numbers A, B, C. Print these numbers in ascending order followed by a blank line and then the values in the sequence as they were read.
// https://codeforces.com/group/MWSDmqGsZm/contest/219158/problem/T
#include <iostream>
using namespace std;
int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    int x = a, y = b, z = c;

    if (a > b)
        swap(a, b);
    if (b > c)
    {
        swap(c, b);
        if (a > b)
            swap(a, b);
    }

    cout << a << endl
         << b << endl
         << c << endl;
    cout << endl;
    cout << x << endl
         << y << endl
         << z << endl;
}

Notes:

learned to use C++ swap() to sort numbers, I couldve done some permutations to solve this codeforces problem use some sort function in C++, but I wanted to learn to use something else that I could use for a lot of use cases and I found swap()