UIU: Competitive Programming: File IO


1. Create a file named “in.txt” in same location of your source cpp file

2. Write two integer number in “in.txt” file

for example,
2 5


// @BEGIN_OF_SOURCE_CODE
 
#include <cstdio>
 
using namespace std;
 
int main(int argc, const char * argv[])
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
 
    int a, b;
 
    scanf ("%d %d", &a, &b);
 
    printf ("%d\n", a + b);
 
    return 0;
}
 
// @END_OF_SOURCE_CODE

UIU: Competitive Programming: GCD & LCM



// @BEGIN_OF_SOURCE_CODE
 
#include <cstdio>
 
using namespace std;
 
int gcd(int a, int b)
{
    if ( a == 0 ) return b;
 
    return gcd(b % a, a);
}
 
int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}
 
int main(int argc, const char * argv[])
{
    printf ("%d\n", gcd(42, 36));
 
    printf ("%d\n", gcd(36, 42));
 
    printf ("%d\n", lcm(36, 42));
 
    return 0;
}
 
// @END_OF_SOURCE_CODE


// TODO: using prime factor 

UIU: Competitive Programming (Session 02): Task 03 Solutions


Task 03 Problems

  1. http://codeforces.com/problemset/problem/1/A
  2. http://codeforces.com/problemset/problem/4/A
  3. http://codeforces.com/problemset/problem/158/A
  4. http://codeforces.com/problemset/problem/71/A
  5. http://codeforces.com/problemset/problem/118/A
  6. http://codeforces.com/problemset/problem/158/B
  7. STL Vector: http://wp.me/poiaX-XB
  8. C++ String: http://wp.me/poiaX-Uc

There might have been some mistakes and/or logical errors in the real time videos. Correct solutions can be found below of this post.

Discussion Video

YouTube Video (Part 1)

Original file: MediaFire (Part 1)

http://www.mediafire.com/download/7nwx5gzc72a4dpy/ (250 MB)

Converted file (Smaller size): MediaFire (Part 1)

http://www.mediafire.com/download/kpa10g1ezdi265g/ (85 MB)

YouTube Video (Part 2)

TBD

Original file: MediaFire (Part 2)

TBD

Converted file (Smaller size): MediaFire (Part 2)

TBD

Sourcecode

CodeForces 1A (Theatre Square)

http://wp.me/poiaX-15U

CodeForces 4A (Watermelon)

http://wp.me/poiaX-15Z

CodeForces 158A (Next Round)

http://wp.me/poiaX-164

CodeForces 71A (Way Too Long Words)

http://wp.me/poiaX-168

CodeForces 118A (String Task)

http://wp.me/poiaX-16c

CodeForces 158B (Taxi)

http://wp.me/poiaX-16f

UIU: Competitive Programming: Prime Factor


Prime factor


// @BEGIN_OF_SOURCE_CODE
 
#include <cstdio>
#import <cmath>
 
using namespace std;
 
bool isPrime(int n)
{
    if ( n < 2 ) return false;
 
    if ( n == 2 ) return true;
 
    if ( n % 2 == 0 ) return false;
 
    int squareRoot = (int) sqrt(n);
 
    for ( int i = 3; i <= squareRoot; i += 2 ) {
        if ( n % i == 0 ) return false;
    }
 
    return true;
}
 
int nextPrime(int n)
{
    while(!isPrime(++n));
 
    return n;
}
 
int main(int argc, const char * argv[])
{
    // 36 = 2 * 2 * 3 * 3;
    int input;
 
    scanf ("%d", &input);
 
    int primeNumber = nextPrime(0);
 
    while (input != 1) {
        while (input % primeNumber == 0) {
            printf ("%d\n", primeNumber);
            input /= primeNumber;
        }
 
        primeNumber = nextPrime(primeNumber);
    }
 
    return 0;
}
 
// @END_OF_SOURCE_CODE

UIU Competitive Programmers contest – 03 Solutions


A – Another A+B


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main(int argc, const char * argv[])
{
    int n; 
    
    while (scanf ("%d", &n) != EOF) {
        printf ("%d %d\n", 1, n - 1);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

B – Sum Problem


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define F(i, n) for (int i = 0; i < n; i++)

int main(int argc, const char * argv[])
{
    int n;

    while (scanf ("%d", &n) != EOF) {
        
        int sum = 0;
        
        F(i, n) {
            sum += (i + 1);
        }
        
        printf ("%d\n\n", sum);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

C – Is it a Right Triangle?

https://tausiq.wordpress.com/2013/05/21/uiu-beginners-practice-contest-01-solutions/#b

D – Eddy’s mistakes


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <string>
#include <algorithm>  // transform

using namespace std;

int main(int argc, const char * argv[])
{
    string str;

    // works like, gets in C programming
    while (getline(cin, str)) {

        // make the string lower case
        transform(str.begin(), str.end(), str.begin(), ::tolower);

        cout << str << endl;
    }

    return 0;
}

// @END_OF_SOURCE_CODE

E – f91

https://tausiq.wordpress.com/2013/05/24/uiu-beginners-practice-contest-03-solutions/#h

UIU Competitive Programmers contest – 02 Solution


A – QQ


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main ()
{
    for (int firstNumber = 1; firstNumber <= 9; firstNumber++) {
        for (int secondNumber = 1; secondNumber <= 9; secondNumber++) {
            printf ("%dx%d=%d\n", firstNumber, secondNumber, firstNumber * secondNumber);
        }
    }

    return 0;
}

// @END_OF_SOURCE_CODE

B – Sorting Five Numbers


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <algorithm>
#import <functional>

using namespace std;

int main ()
{
    int nums [5];

    for (int i = 0; i < 5; i++)
        scanf ("%d", &nums [i]);

    // descending order sort
    sort(nums, nums + 5, greater<int>());

    bool space = false;

    for (int i = 0; i < 5; i++) {
        if (space) printf (" ");
        space = true;
        printf ("%d", nums [i]);
    }

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

C – Digit Number


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

using namespace std;

int main ()
{
    int a, b;

    while (scanf ("%d %d", &a, &b) != EOF) {
        int sum = a + b;
        int ret = 0;

        while(sum) {
            sum /= 10;
            ret++;
        }

        printf("%d\n", ret);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

D – GCD and LCM


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

#define LL long long

using namespace std;

LL gcd(LL a, LL b)
{
    if (b == 0) return a;

    return gcd(b, a % b);
}

int main ()
{
    LL a, b;

    while(cin >> a >> b) {
        LL gcdVal = gcd(a, b);
        LL lcmVal = a * b / gcdVal;

        cout << gcdVal << " " << lcmVal << endl;
    }


    return 0;
}

// @END_OF_SOURCE_CODE

E – List of Top 3 Hills


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <algorithm>

using namespace std;

int main ()
{
    int heights [10 + 5];

    for (int i = 0; i < 10; i++) {
        scanf ("%d", &heights[i]);
    }

    sort(heights, heights + 10);

    printf ("%d\n%d\n%d\n", heights [9], heights [8], heights [7]);

    return 0;
}

// @END_OF_SOURCE_CODE

UIU Competitive Programmers Contest – 01 Solution


A – A+B for Input-Output Practice (I)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int a;
    int b;

    while (scanf ("%d %d", &a, &b) != EOF) {
        printf ("%d\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

B – A+B for Input-Output Practice (II)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    scanf ("%d", &n);

    int a;
    int b;

    while (n--) {
        scanf ("%d %d", &a, &b);
        printf ("%d\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

C – A+B for Input-Output Practice (III)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int a;
    int b;

    while (true) {
        scanf ("%d %d", &a, &b);
        if (a == 0 && b == 0) break;
        printf ("%d\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

D – A+B for Input-Output Practice (IV)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    while (true) {
        scanf ("%d", &n);

        if (n == 0) break;
        
        int num;
        int sum = 0;

        for ( int i = 0; i < n; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }
        
        printf ("%d\n", sum);
    }



    return 0;
}

// @END_OF_SOURCE_CODE

E – A+B for Input-Output Practice (V)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    scanf ("%d", &n);

    while (n--) {
        
        int m;
        scanf ("%d", &m);
        
        int num;
        int sum = 0;

        for ( int i = 0; i < m; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }

        printf ("%d\n", sum);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

F – A+B for Input-Output Practice (VI)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;

    while (scanf ("%d", &n) != EOF) {

        int num;
        int sum = 0;

        for ( int i = 0; i < n; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }

        printf ("%d\n", sum);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

G – A+B for Input-Output Practice (VII)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int a;
    int b;

    while (scanf ("%d %d", &a, &b) != EOF) {

        printf ("%d\n\n", a + b);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

H – A+B for Input-Output Practice (VIII)


// @BEGIN_OF_SOURCE_CODE

#include <iostream>

using namespace std;

int main ()
{
    int n;
    
    scanf ("%d", &n);
    
    bool blankLine = false;
    
    while (n--) {
        
        int m; 
        scanf ("%d", &m);
        
        int num;
        int sum = 0;
        
        for ( int i = 0; i < m; i++ ) {
            scanf ("%d", &num);
            sum += num;
        }
        
        if (blankLine) printf ("\n");
        
        blankLine = true;
        
        printf ("%d\n", sum);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

I – Reverse Sequence


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>

using namespace std;

int main ()
{
    char str [20 + 10];

    scanf ("%s", str);

    int length = strlen(str);

    for ( int i = length - 1; i >= 0; i-- )
        printf ("%c", str [i]);

    printf ("\n");

    return 0;
}

// @END_OF_SOURCE_CODE

J – Factorial


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main ()
{
    int n;

    scanf("%d", &n);

    long long fact = 1;

    for ( int i = 1; i <= n; i++ )
        fact *= i;

    cout << fact << endl;

    return 0;
}

// @END_OF_SOURCE_CODE

K – Prime Number


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

bool prime [999999 + 10];

bool isPrime(int n) {

    // all numbers less than 2 is not prime
    if (n < 2) return false;

    // 2 is only even prime
    if (n == 2) return true;

    // others even numbers are not prime
    if (n % 2 == 0) return false;

    int squareRoot = (int) sqrt(n);

    for ( int i = 3; i <= squareRoot; i += 2 ) {
        if (n % i == 0) return false;
    }

    return true;
}

void preCalculation()
{
    for ( int i = 0; i <= 999999; i++ ) {
        prime [i] = isPrime(i);
    }

}

int main ()
{
    preCalculation();

    int n;

    while (scanf ("%d", &n) != EOF) {

        int count = 0;

        for ( int i = 2; i <= n; i++ ) {
            if (prime [i]) count++;
        }

        printf ("%d\n", count);
    }

    return 0;
}

// @END_OF_SOURCE_CODE