Hanwool Codes RSS Tag Admin Write Guestbook
software Engineering/(C++)HackerRank (9)
2023-02-10 02:45:54

Variable Sized Arrays | HackerRank

 

Variable Sized Arrays | HackerRank

Find the element described in the query for integer sequences.

www.hackerrank.com

 

Goal

 

Create N number of variable-length arrays and find a element from quaries

 

Solution

 

If you know vector in C++, it would be easy problem.

you initialize vector of vector a and insert variable sized vector into a.

 

# Variable Sized Arrays

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n = 0;
    int q = 0;
    
    cin >> n >> q;
    
     vector<vector<int>> a;
    
    for(int i = 0 ; i < n ; i++){
        int k = 0;
        
        cin >> k;
        
        vector<int> v;
        
        int value = 0;
        int index = 1;
        while(cin >> value){
            
            v.push_back(value);
            index++;
            
            if (index > k){
                break;
            }
            
        }
        
        a.push_back(v);
        
    }
    
    for(int i = 0; i < q ; i++){
        int index, j ;
        cin >> index >> j;
        cout << a[index][j] << endl;
    }
    
    
    return 0;
}
2023-02-09 05:43:55

Arrays Introduction | HackerRank

 

Arrays Introduction | HackerRank

How to access and use arrays. Print the array in the reverse order.

www.hackerrank.com

 

Goal

 

Print the N integers of the array in the reverse order, space-separated on a single line.

 

Solution

 

1. Create array with N size.

2. Save N integers into array.

3. Print reverse array with space

 

// Arrays Introduction

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    
    cin >> n;
    
    int a[n];
    int tmp;
    int i = 0;
    
    while (cin >> tmp)
    {
        a[i] = tmp;
        i++;
    }
    
    for(int index = n - 1; index >=0 ; index--){
        if(index !=0){
            cout << a[index] << " ";
        }
        else{
            cout << a[index] << endl;
        }
    }
    
    
    return 0;
}

 

 

2023-02-08 05:29:54

Pointer | HackerRank

 

Pointer | HackerRank

Learn how to declare pointers and use them.

www.hackerrank.com

 

Goal

 

By using pointer in C++, save a + b value into a and | a - b | into b.

 

 

Solution

 

This problem makes us to pratice pointer. 

You can save sum and abs_min of a and b with * sign in a and b, respectively. 

 

// Pointer

#include <stdio.h>
#include <cstdlib>


void update(int *a,int *b) {
    // Complete this function

    int sum = *a + * b;
    
    int abs_min = (*a) - (*b);
    *a = sum;
    *b = abs(abs_min);
        
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    
    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}
2023-02-04 04:29:14

Functions | HackerRank

 

Functions | HackerRank

Learn how to write functions in C++. Create a function to find the maximum of the four numbers.

www.hackerrank.com

 

 

Goal

 

Get four arguments and return the greatest of the four integers

 

 

Solution

 

Because you just need to return the greatest integer.

You can compare a and b first, then c and d.

If you find already greateest integer, you can simply return the value.

 

// Functions

#include <iostream>
#include <cstdio>
using namespace std;

/*
Add `int max_of_four(int a, int b, int c, int d)` here.
*/

int max_of_four(int a, int b, int c, int d){
    
    
    if(a >= b){
        if (a >= c){
            if (a >= d){
                return a;
            }
            else{
                return d;
            }
        }
            
        else{
            
            if( c>=d){
                return c;
            }
            
            else{
                return d;
            }
        }
    }
    
    else{
        
        if (b >= c){
            if (b >= d){
                return b;
            }
            else {
                return d;
            }
        }
        
        else{
            
            if (c >= d){
                return c;
            }
            
            else {
                return d;
            }
            
        }
        
    }
    
    return 0;
}



int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int ans = max_of_four(a, b, c, d);
    printf("%d", ans);
    
    return 0;
}
2023-01-24 06:01:01

For Loop | HackerRank

 

For Loop | HackerRank

Learn how to use for loop and print the output as per the given conditions

www.hackerrank.com

 

Goal

 

Print results according to two positive input values by using for loop statement.

 

 

Solution

 

To solve easily, I create vector string array included 1-9 numbers. If you use vector array, you can print numbers easily.

 

// For Loop
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;

int main() {
    // Complete the code.
    int a,b;
    cin >> a;
    cin >> b;
    
    vector<string> numbers {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    
    for(int i = a; i <= b; i++){
        
        if(i>9){
            if( i % 2 == 0){
                cout << "even" << endl;
            }
            else{
                cout << "odd" << endl;
            }
        }
        
        else{
            
            cout << numbers[i] << endl;
        }
    }
    
    return 0;
}
2023-01-24 05:55:31

https://www.hackerrank.com/challenges/c-tutorial-conditional-if-else/problem

 

Conditional Statements | HackerRank

Practice using chained conditional statements.

www.hackerrank.com

 

Goal

 

 

Solution

 

You just need to create condtion statements for each condition like below.

 

// Conditional Statements

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);



int main()
{
    string n_temp;
    getline(cin, n_temp);

    int n = stoi(ltrim(rtrim(n_temp)));

    // Write your code here
    if(n > 9){
        printf("Greater than 9");
    }
    else if(n == 1){
        printf("one");
    }
    else if(n == 2){
        printf("two");
    }
    else if(n == 3){
        printf("three");
    }
    else if(n == 4){
        printf("four");
    }
    else if(n == 5){
        printf("five");
    }
    else if(n == 6){
        printf("six");
    }
    else if(n == 7){
        printf("seven");
    }
    else if(n == 8){
        printf("eight");
    }
    else { // n == 9
        printf("nine");
    }

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}
2023-01-22 18:00:34

Basic Data Types | HackerRank

 

Basic Data Types | HackerRank

Learn about the basic data types in C++. Take the given input and print them.

www.hackerrank.com

 

Goal

1. Read space-seprated values : int, long, char, float and double, respectively

2. Print each element on a new line in the same order it was received as input. + correct decimal points of floating and double points.

 

 

Solution

Follow HackerRank explanation of c++ data types and their format specifiers.

// Basic Data Types

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
    long b;
    char c;
    float d;
    double e;

    scanf("%d %ld %c %f %lf", &a, &b, &c, &d, &e);
    printf("%d\n", a);
    printf("%ld\n", b);
    printf("%c\n", c);
    printf("%f\n", d);
    printf("%lf\n", e);
    return 0;
}
2023-01-18 06:13:37

Input and Output | HackerRank

 

Input and Output | HackerRank

Learn to take in the input and print the output. Take three number as input and print their sum as output.

www.hackerrank.com

 

Goal

 

Read 3 numbers from stdin and print their sum to stdout

 

 

Solution

 

This challenge is to practice reading input from stdin(standard input stream) and printing out to stdout(standard output stream).

 

// Input and Output

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int a, b,c ;
    int result = 0;
    cin >> a >> b >> c;
    result = a + b + c;
    cout << result << endl;
    return 0;
}

 

 

 

2023-01-18 06:06:40

Say "Hello, World!" With C++ | HackerRank

 

Say "Hello, World!" With C++ | HackerRank

Practice printing to stdout.

www.hackerrank.com

 

Goal

 

Solution

 

This is simple challenge to print "Hello, World!" to stdout. We can use easily an object of class iostream. "std::cout."

 

// Say "Hello, World!" With C++

#include <iostream>
#include <cstdio>
using namespace std;

int main() {

    cout << "Hello, World!" << endl;
    return 0;
}

 



Hanwool Codes. Designed by 코딩재개발.