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?

http://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

http://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

UIU: Competitive Programming: Palindrome


Palindrome Check


// @BEGIN_OF_SOURCE_CODE
 
#include <cstdio>
#include <cstring>
#import <algorithm>
 
using namespace std;
 
bool isPalindrome(char str [])
{
    int length = strlen (str);
 
    char copy [length];
 
    // making a copy of original string
    strcpy(copy, str);
 
    reverse(copy, copy + length);
 
    return strcmp(copy, str) == 0;
}
 
int main(int argc, const char * argv[])
{
    char input [100];
 
    scanf ("%s", input);
 
    if (isPalindrome(input)) {
        printf ("Palindrome\n");
 
    } else {
        printf ("Not Palindrome\n");
    }
 
    return 0;
}
 
// @END_OF_SOURCE_CODE

UIU: Learn C by Examples : Reverse


Reverse string naive approach


#include <cstdio>
#include <cstring>

using namespace std;

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    char reverseString [50];

    int reverseIndex = lengthMainString - 1;

    for ( int i = 0; i < lengthMainString; i++ ) {
        reverseString [reverseIndex] = mainString [i];
        reverseIndex--;
    }

    reverseString [lengthMainString] = '\0';

    printf ("Reverse String: %s\n", reverseString);

    return 0;
}


Main String: Hello
Reverse String: olleH

Reverse string (length / 2) approach


#include <cstdio>
#include <cstring>

using namespace std;

void swap (char *a, char *b)
{
    char temp = *b;
    *b = *a;
    *a = temp;
}

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    for ( int i = 0; i < lengthMainString / 2; i++ ) {
        swap(&mainString [i], &mainString [lengthMainString - 1 - i]);
    }

    printf ("Reverse String: %s\n", mainString);

    return 0;
}


Main String: Hello
Reverse String: olleH

Reverse string STL approach


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main(int argc, const char * argv[])
{
    char mainString [50] = "Hello";

    printf ("Main String: %s\n", mainString);

    int lengthMainString = strlen(mainString);

    reverse(mainString, mainString + lengthMainString);

    printf ("Reverse String: %s\n", mainString);

    return 0;
}



Main String: Hello
Reverse String: olleH

Reverse number sprintf approach


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>

using namespace std;

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

    char number [50];

    sprintf(number, "%d", n);

    reverse(number, number + strlen (number));

    printf ("Reverse Number: %s\n", number);

    // convert a string to integer
    int reverseNumber = atoi(number);

    printf ("Reverse Number: %d\n", reverseNumber);

    return 0;
}


Reverse Number: 54321
Reverse Number: 54321

Reverse number modulus approach


#include <cstdio>

using namespace std;

int reverseNumber(int n)
{
    int ret = 0;

    while ( n ) {
        ret = 10 * ret + n % 10;
        n /= 10;
    }

    return ret;
}

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

    printf ("Reverse Number: %d\n", reverseNumber(n));

    return 0;
}


Reverse Number: 54321