Hanwool Codes RSS Tag Admin Write Guestbook
전체 글 (45)
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;
}


Hanwool Codes. Designed by 코딩재개발.