Archive/Code_HackerRank 2018. 1. 18. 23:15

[HackerRank]Apple and Orange

Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where  is the start point, and  is the endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point , and the orange tree is at point .

Apple and orange(2).png

When a fruit falls from its tree, it lands  units of distance from its tree of origin along the -axis. A negative value of  means the fruit fell  units to the tree's left, and a positive value of  means it falls  units to the tree's right.

Complete the function countApplesAndOranges,

where,

 Starting point of Sam's house location. 
 Ending location of Sam's house location. 
 Location of the Apple tree. 
 Location of the Orange tree. 
 Number of apples that fell from the tree. 
 Distance at which each apple falls from the tree. 
 Number of oranges that fell from the tree. 
 Distance at which each orange falls from the tree.

Given the value of  for  apples and  oranges, can you determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )? Print the number of apples that fall on Sam's house as your first line of output, then print the number of oranges that fall on Sam's house as your second line of output.

Input Format

The first line contains two space-separated integers denoting the respective values of  and 
The second line contains two space-separated integers denoting the respective values of  and 
The third line contains two space-separated integers denoting the respective values of  and 
The fourth line contains  space-separated integers denoting the respective distances that each apple falls from point 
The fifth line contains  space-separated integers denoting the respective distances that each orange falls from point .

Constraints

Output Format

Print two lines of output:

  1. On the first line, print the number of apples that fall on Sam's house.
  2. On the second line, print the number of oranges that fall on Sam's house.

Sample Input 0

7 11
5 15
3 2
-2 2 1
5 -6

Sample Output 0

1
1

Explanation 0

The first apple falls at position 
The second apple falls at position 
The third apple falls at position 
The first orange falls at position 
The second orange falls at position 
Only one fruit (the second apple) falls within the region between  and , so we print  as our first line of output. 
Only the second orange falls within the region between  and , so we print  as our second line of output.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
 
using namespace std;
 
vector <int> appleAndOrange(int s, int t, int a, int b, vector <int> apple, vector <int> orange) {
    int cnt_apple = 0;
    int cnt_orange = 0;
    
    for(int i=0; i<apple.size(); i++){
        if(a+apple[i] >= s && a+apple[i]<=t)
            cnt_apple++;
    }
    for(int i=0; i<orange.size(); i++){
        if(b+orange[i] >= s && b+orange[i]<=t)
            cnt_orange++;
    }
    
    vector<int> result;
    result.push_back(cnt_apple);
    result.push_back(cnt_orange);
 
    return result;
    
}
 
int main() {
    int s;
    int t;
    cin >> s >> t;
    int a;
    int b;
    cin >> a >> b;
    int m;
    int n;
    cin >> m >> n;
    vector<int> apple(m);
    for(int apple_i = 0; apple_i < m; apple_i++){
       cin >> apple[apple_i];
    }
    vector<int> orange(n);
    for(int orange_i = 0; orange_i < n; orange_i++){
       cin >> orange[orange_i];
    }
    vector <int> result = appleAndOrange(s, t, a, b, apple, orange);
    for (ssize_t i = 0; i < result.size(); i++) {
        cout << result[i] << (i != result.size() - 1 ? "\n" : "");
    }
    cout << endl;
 
 
    return 0;
}
 
cs



Comment


입력인자가 많긴 하지만.. 어쨌든 모두 필요한 인자들.

집에 떨어지는 과일의 갯수를 세는건데.. 결국 떨어지는 모든 과일마다 집의 범위 안에 떨어지는지 체크해서 카운트해준다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Grading Students  (0) 2018.01.18
[HackerRank]Time Conversion  (0) 2018.01.17
[HackerRank]Birthday Cake Candles  (0) 2018.01.17
[HackerRank]Mini-Max Sum  (0) 2018.01.17
[HackerRank]Staircase  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 18. 23:13

[HackerRank]Grading Students

HackerLand University has the following grading policy:

  • Every student receives a  in the inclusive range from  to .
  • Any  less than  is a failing grade.

Sam is a professor at the university and likes to round each student's  according to these rules:

  • If the difference between the  and the next multiple of  is less than , round  up to the next multiple of .
  • If the value of  is less than , no rounding occurs as the result will still be a failing grade.

For example,  will be rounded to  but  will not be rounded because the rounding would result in a number that is less than .

Given the initial value of  for each of Sam's  students, write code to automate the rounding process. Complete the function solve that takes an integer array of all grades, and return an integer array consisting of the rounded grades. For each , round it according to the rules above and print the result on a new line.

Input Format

The first line contains a single integer denoting  (the number of students). 
Each line  of the  subsequent lines contains a single integer, , denoting student 's grade.

Constraints

Output Format

For each  of the  grades, print the rounded grade on a new line.

Sample Input 0

4
73
67
38
33

Sample Output 0

75
67
40
33

Explanation 0

image

  1. Student  received a , and the next multiple of  from  is . Since , the student's grade is rounded to .
  2. Student  received a , and the next multiple of  from  is . Since , the grade will not be modified and the student's final grade is .
  3. Student  received a , and the next multiple of  from  is . Since , the student's grade will be rounded to .
  4. Student  received a grade below , so the grade will not be modified and the student's final grade is .




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>
 
using namespace std;
 
vector < int > solve(vector < int > grades){
    vector<int> result;
    for(int i=0; i<grades.size(); i++){
        if(grades[i]%5>=3 && grades[i]>=38){
            result.push_back(((grades[i]/5)+1*5);
        }
        else{
       
            result.push_back(grades[i]);

        }
        
    }
    return result;
}
 
int main() {
    int n;
    cin >> n;
    vector<int> grades(n);
    for(int grades_i = 0; grades_i < n; grades_i++){
       cin >> grades[grades_i];
    }
    vector < int > result = solve(grades);
    for (ssize_t i = 0; i < result.size(); i++) {
        cout << result[i] << (i != result.size() - 1 ? "\n" : "");
    }
    cout << endl;
    
 
    return 0;
}
cs




Comment


38 이상이면 다 5 단위로 반올림.

어려운 문제는 아니었다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Apple and Orange  (0) 2018.01.18
[HackerRank]Time Conversion  (0) 2018.01.17
[HackerRank]Birthday Cake Candles  (0) 2018.01.17
[HackerRank]Mini-Max Sum  (0) 2018.01.17
[HackerRank]Staircase  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:52

[HackerRank]Time Conversion

Given a time in -hour AM/PM format, convert it to military (-hour) time.

Note: Midnight is  on a -hour clock, and  on a -hour clock. Noon is  on a -hour clock, and  on a -hour clock.

Input Format

A single string containing a time in -hour clock format (i.e.:  or ), where  and .

Output Format

Convert and print the given time in -hour format, where .

Sample Input

07:05:45PM

Sample Output

19:05:45



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
 
using namespace std;
 
 
int main() {
    int hh, mm, ss ;
    char t12[2];
    scanf("%d:%d:%d%s"&hh, &mm, &ss, t12) ;//입력받기
    
    if (strcmp(t12,"PM")==0 && hh!=12)      // PM일 경우 12를 더한다.
        hh += 12 ;
    if (strcmp(t12,"AM")==0 && hh==12)      // AM일 경우 놔둔다.
        hh = 0 ;
    
    printf("%02d:%02d:%02d", hh, mm, ss) ;  // 출력한다. 한자리수라면 앞에 0을 붙여서 출력한다.
    return 0;
}
 
cs



Comment


Warm Up 마지막 문제.

사실 이렇게 문자열을 이용하는 문제는 익숙하지가 않다. 저걸 시,분,초로 나눌생각은 했지만 어떻게 해야할지 막막했는데 scanf를 쓰니까 생각보다 쉬운 문제였다. 어차피 데이터와 데이터 사이에는 ":" 라는 문자로 나눠져있고, 그걸 기준으로 각각 값을 따로 받으면 되는거였다.

String문제.. 연습이 필요하다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Apple and Orange  (0) 2018.01.18
[HackerRank]Grading Students  (0) 2018.01.18
[HackerRank]Birthday Cake Candles  (0) 2018.01.17
[HackerRank]Mini-Max Sum  (0) 2018.01.17
[HackerRank]Staircase  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:49

[HackerRank]Birthday Cake Candles

You are in-charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones.

For example, if your niece is turning  years old, and the cake will have  candles of height , she will be able to blow out  candles successfully, since the tallest candle is of height  and there are  such candles.

Complete the function birthdayCakeCandles that takes your niece's age and an integer array containing height of each candle as input, and return the number of candles she can successfully blow out.

Input Format

The first line contains a single integer, , denoting the number of candles on the cake. 
The second line contains  space-separated integers, where each integer  describes the height of candle .

Constraints

Output Format

Print the number of candles the can be blown out on a new line.

Sample Input 0

4
3 2 1 3

Sample Output 0

2

Explanation 0

We have one candle of height , one candle of height , and two candles of height . Your niece only blows out the tallest candles, meaning the candles where . Because there are  such candles, we print  on a new line.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
 
using namespace std;
 
int birthdayCakeCandles(int n, vector <int> ar) {
    int ans = 0;
    int tmp = ar[0];
    for(int i=0; i<n; i++){
        if(tmp < ar[i]){
            tmp = ar[i];
            ans = 0;
        }
        
        if(tmp == ar[i])
            ans++;
        
    }
    
    return ans;
    
}
 
int main() {
    int n;
    cin >> n;
    vector<int> ar(n);
    for(int ar_i = 0; ar_i < n; ar_i++){
       cin >> ar[ar_i];
    }
    int result = birthdayCakeCandles(n, ar);
    cout << result << endl;
    return 0;
}
 
cs



Comment


역시 ar.size() 를 쓸 필요 없이 n값이 주어지기때문에 반복문을 돌리기 편하다.

가장 큰 초의 갯수를 세면 된다. 현재 가장 큰 초보다 더 큰 초가 나오면 ans를 0으로 초기화하고, 그 후 같은 크기의 초가 나올때마다 ans를 1씩 올려준 후 정답을 반환했다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Grading Students  (0) 2018.01.18
[HackerRank]Time Conversion  (0) 2018.01.17
[HackerRank]Mini-Max Sum  (0) 2018.01.17
[HackerRank]Staircase  (0) 2018.01.17
[HackerRank]Plus Minus  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:45

[HackerRank]Mini-Max Sum

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Input Format

A single line of five space-separated integers.

Constraints

  • Each integer is in the inclusive range .

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

Our initial numbers are , and . We can calculate the following sums using four of the five integers:

  1. If we sum everything except , our sum is .
  2. If we sum everything except , our sum is .
  3. If we sum everything except , our sum is .
  4. If we sum everything except , our sum is .
  5. If we sum everything except , our sum is .

As you can see, the minimal sum is  and the maximal sum is . Thus, we print these minimal and maximal sums as two space-separated integers on a new line.

Hints: Beware of integer overflow! Use 64-bit Integer.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
 
using namespace std;
 
long minX(vector<int> arr){
    long a=(long)arr[0];
    
    for(int i=1; i<5; i++){
        if(a > arr[i])
            a = arr[i];
    }
    
    return a;
}
long maxX(vector<int> arr){
    long a= (long)arr[0];
    for(int i=1; i<5; i++){
        if(a < arr[i])
            a = arr[i];
    }
    
    return a;
}
 
 
int main() {
    vector<int> arr(5);
    for(int arr_i = 0; arr_i < 5; arr_i++){
       cin >> arr[arr_i];
    }
    long a=0;
    for(int i=0; i<5; i++){
        a += (long)arr[i];
    }
    
    
    cout << a-maxX(arr) << " " <<a-minX(arr);
    
    
    return 0;
}
 
cs



Comment


가장 작은 값과 가장 큰 값을 뽑아내면 된다.

long int가 필요하다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Time Conversion  (0) 2018.01.17
[HackerRank]Birthday Cake Candles  (0) 2018.01.17
[HackerRank]Staircase  (0) 2018.01.17
[HackerRank]Plus Minus  (0) 2018.01.17
[HackerRank]Diagonal Difference  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:41

[HackerRank]Staircase

Consider a staircase of size :

   #
  ##
 ###
####

Observe that its base and height are both equal to , and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Input Format

A single integer, , denoting the size of the staircase.

Output Format

Print a staircase of size  using # symbols and spaces.

Note: The last line must have  spaces in it.

Sample Input

6 

Sample Output

     #
    ##
   ###
  ####
 #####
######

Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
 
int main(){
    int n;
    cin >> n;
    
    for(int i=n-1; i>=0; i--){
        for(int j=0; j<i; j++){
            cout << " ";
        }
        for(int j=i; j<n; j++){
            cout<< "#";
        }
        cout<<endl;
    }
    
    
    return 0;
}
 
cs



Comment


별찍기 문제랑 비슷하게 생각한다. 공백을 출력하다가 일정 갯수만큼 #을 출력하면 된다.

그걸 차례대로 늘려간다.

이중 for문을 연습한다고 생각하면 된다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Birthday Cake Candles  (0) 2018.01.17
[HackerRank]Mini-Max Sum  (0) 2018.01.17
[HackerRank]Plus Minus  (0) 2018.01.17
[HackerRank]Diagonal Difference  (0) 2018.01.17
[HackerRank]A Very Big Sum  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:38

[HackerRank]Plus Minus

Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. Print the decimal value of each fraction on a new line.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to  are acceptable.

Input Format

The first line contains an integer, , denoting the size of the array. 
The second line contains  space-separated integers describing an array of numbers .

Output Format

You must print the following  lines:

  1. A decimal representing of the fraction of positive numbers in the array compared to its size.
  2. A decimal representing of the fraction of negative numbers in the array compared to its size.
  3. A decimal representing of the fraction of zeroes in the array compared to its size.

Sample Input

6
-4 3 -9 0 4 1         

Sample Output

0.500000
0.333333
0.166667

Explanation

There are  positive numbers,  negative numbers, and  zero in the array. 
The respective fractions of positive numbers, negative numbers and zeroes are  and , respectively.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<float> func(int n, vector<int> v){
    vector<float> ans(30.0f);
    for(int i=0; i<n; i++){
        if(v[i]>0){
            ans[0]+= 1.0f;
        }    
        else if(v[i]<0){
            ans[1+= 1.0f;
        }
        else if(v[i]==0)
            ans[2+= 1.0f;
    }
    
    ans[0= ans[0]/(float)n;
    ans[1= ans[1]/(float)n;
    ans[2= ans[2]/(float)n;
    
    return ans;
    
    
}
int main(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int arr_i = 0;arr_i < n;arr_i++){
       cin >> arr[arr_i];
    }
    vector<float> ans = func(n, arr);
    cout.precision(7);
    for(int arr_i = 0;arr_i < 3;arr_i++){
       cout << ans[arr_i] << endl;
    }
    
    
    return 0;
}
 
cs




Comment


n값을 따로 인자로 넣어주기때문에 굳이 v.size()를 쓰지 않아도 됐다.

n은 int값으로 받아오기 때문에 float로 형변환을 해줘야 정확한 float 연산이 된다.



'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Mini-Max Sum  (0) 2018.01.17
[HackerRank]Staircase  (0) 2018.01.17
[HackerRank]Diagonal Difference  (0) 2018.01.17
[HackerRank]A Very Big Sum  (0) 2018.01.17
[HackerRank]Compare the Triplets  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:34

[HackerRank]Diagonal Difference

Given a square matrix of size , calculate the absolute difference between the sums of its diagonals.

Input Format

The first line contains a single integer, . The next  lines denote the matrix's rows, with each line containing space-separated integers describing the columns.

Constraints

Output Format

Print the absolute difference between the two sums of the matrix's diagonals as a single integer.

Sample Input

3
11 2 4
4 5 6
10 8 -12

Sample Output

15

Explanation

The primary diagonal is:

11
   5
     -12

Sum across the primary diagonal: 11 + 5 - 12 = 4

The secondary diagonal is:

     4
   5
10

Sum across the secondary diagonal: 4 + 5 + 10 = 19 
Difference: |4 - 19| = 15

Note: |x| is absolute value function


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
int func(int n, vector<vector<int>> a){
    int ans;
    int a1=0;
    int a2=0;
    for(int i=0; i<n; i++){
        a1 += a[i][i];
    }
    int tmp = n-1;
    for(int i=0; i<n; i++){
        a2 += a[i][tmp-i];
    }
    
    ans = abs(a1-a2);
    
 
    return ans;
}
 
int main(){
    int n;
    cin >> n;
    vector< vector<int> > a(n,vector<int>(n));
    for(int a_i = 0;a_i < n;a_i++){
       for(int a_j = 0;a_j < n;a_j++){
          cin >> a[a_i][a_j];
       }
    }
    
    cout << func(n,a);
    
    return 0;
}
 
cs




Comment


vector<vector<int>>는 쉽게말해 int형 2차원 배열과 같다고 생각했다. 때문에 row와 column 의 index가 같은 값을 더한 후, row와 column값이 반대인 값을 더해 그 차이를 구한 뒤 절대값을 씌워주니 답이 나왔다.

2중 반복문을 쓰지 않고 필요한 값을 모두 뽑아낼 수 있다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Staircase  (0) 2018.01.17
[HackerRank]Plus Minus  (0) 2018.01.17
[HackerRank]A Very Big Sum  (0) 2018.01.17
[HackerRank]Compare the Triplets  (0) 2018.01.17
[HackerRank]Simple Array Sum  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:30

[HackerRank]A Very Big Sum

You are given an array of integers of size . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large.

Input Format

The first line of the input consists of an integer . The next line contains  space-separated integers contained in the array.

Output Format

Print a single value equal to the sum of the elements in the array.

Constraints 
 

Sample Input

5
1000000001 1000000002 1000000003 1000000004 1000000005

Output

5000000015

Note:

The range of the 32-bit integer is .

When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
 
using namespace std;
 
long aVeryBigSum(int n, vector <long> ar) {
    // Complete this function
    long tot = 0;
    for(int i=0; i<n; i++){
        tot += ar[i];
    }
    
    return tot;
}
 
int main() {
    int n;
    cin >> n;
    vector<long> ar(n);
    for(int ar_i = 0; ar_i < n; ar_i++){
       cin >> ar[ar_i];
    }
    long result = aVeryBigSum(n, ar);
    cout << result << endl;
    return 0;
}
cs



Commnet

long int 타입의 변수. 이전에 문제를 풀다보니까 long int 혹은 long long int 타입의 변수를 이용해야 풀 수 있는 문제들이 꽤 많이 나온다. int와 long int, long long int의 차이점을 확실히 알고 있고, 사용할 수 있어야 한다.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Plus Minus  (0) 2018.01.17
[HackerRank]Diagonal Difference  (0) 2018.01.17
[HackerRank]Compare the Triplets  (0) 2018.01.17
[HackerRank]Simple Array Sum  (0) 2018.01.17
[HackerRank]Solve Me First  (0) 2018.01.17
Archive/Code_HackerRank 2018. 1. 17. 00:23

[HackerRank]Compare the Triplets

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from  to  for three categories: problem clarityoriginality, and difficulty.

We define the rating for Alice's challenge to be the triplet , and the rating for Bob's challenge to be the triplet .

Your task is to find their comparison points by comparing  with  with , and  with .

  • If , then Alice is awarded  point.
  • If , then Bob is awarded  point.
  • If , then neither person receives a point.

Comparison points is the total points a person earned.

Given  and , can you compare the two challenges and print their respective comparison points?

Input Format

The first line contains  space-separated integers, , and , describing the respective values in triplet 
The second line contains  space-separated integers, , and , describing the respective values in triplet .

Constraints

Output Format

Print two space-separated integers denoting the respective comparison points earned by Alice and Bob.

Sample Input

5 6 7
3 6 10

Sample Output

1 1 

Explanation

In this example:

Now, let's compare each individual score:

  • , so Alice receives  point.
  • , so nobody receives a point.
  • , so Bob receives  point.

Alice's comparison score is , and Bob's comparison score is . Thus, we print 1 1 (Alice's comparison score followed by Bob's comparison score) on a single line.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
 
using namespace std;
 
vector < int > solve(int a0, int a1, int a2, int b0, int b1, int b2){
    // Complete this function
    vector<int> a;
    a.push_back(0);
    a.push_back(0);
 
    if(a0>b0)
        a[0]++;
    else if(a0<b0)
        a[1]++;
    
    if(a1>b1)
        a[0]++;
    else if(a1<b1)
        a[1]++;
    
    if(a2>b2)
        a[0]++;
    else if(a2<b2)
        a[1]++;
        
    return a;
        
    
}
 
int main() {
    int a0;
    int a1;
    int a2;
    cin >> a0 >> a1 >> a2;
    int b0;
    int b1;
    int b2;
    cin >> b0 >> b1 >> b2;
    vector < int > result = solve(a0, a1, a2, b0, b1, b2);
    for (ssize_t i = 0; i < result.size(); i++) {
        cout << result[i] << (i != result.size() - 1 ? " " : "");
    }
    cout << endl;
    
 
    return 0;
}
 
cs



Comment

모든 값을 각각의 변수로 받아주는 함수였기 때문에 이렇게 작성해서 각각 비교했는데.. 이래도 되는건가 싶긴 하다. 문제에서 요구하는 방식이 이런게 맞는건가?

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Plus Minus  (0) 2018.01.17
[HackerRank]Diagonal Difference  (0) 2018.01.17
[HackerRank]A Very Big Sum  (0) 2018.01.17
[HackerRank]Simple Array Sum  (0) 2018.01.17
[HackerRank]Solve Me First  (0) 2018.01.17