본문 바로가기

문제풀이/C 문제풀이

[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 1 to 100 for three categories: problem clarity, originality, and difficulty.
We define the rating for Alice's challenge to be the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge to be the triplet b = (b[0], b[1], b[2].
Your task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].
If a[i] > b[i], then Alice is awarded 1 point.
If a[i] < b[i], then Bob is awarded 1 point.
If a[i] = b[i], then neither person receives a point.
Comparison points is the total points a person earned.
Given a and b, determine their respective comparison points.
Function Description
Complete the function compareTriplets in the editor below. It must return an array of two integers, the first being Alice's score and the second being Bob's.
compareTriplets has the following parameter(s):
a: an array of integers representing Alice's challenge rating
b: an array of integers representing Bob's challenge rating
Input Format
The first line contains 3 space-separated integers, a[0], a[1], and a[2], describing the respective values in triplet a.
The second line contains 3 space-separated integers, ,b[0], b[1], b[2], describing the respective values in triplet b.
Output Format
Return an array of two integers denoting the respective comparison points earned by Alice and Bob.

CODE

int* compareTriplets(int a_count, int* a, int b_count, int* b, int* result_count) { // a_count랑 b_count는 의미를 몰라서 뺐다. a는 배열 a, b는 배열 b의 주 result_count는 내가 return할 배열의 개수
int i; // for문의 index
static int arr[2] = {0, } // return할 배열, 그냥 배열로 선언하면 함수가 끝나면 없어지므로 static 선언을 했다
*result_count=2; // return할 배열의 개수
for(i=0;i<3;i++){ // 3번동안 비교할 예정
if (a[i]>b[i])arr[0]++; // a가 b보다 크다면 배열의 첫 원소의 값 증가
else if(a[i]<b[i]) arr[1]++; // b가 a보다 크다면 배열의 두번째 원소의 값 증가
}
return arr; // 배열 return
}

감상

문제 이해는 어렵지 않았는데 입출력을 어떻게 주고 받아야 하는지 헷갈렸다.

'문제풀이 > C 문제풀이' 카테고리의 다른 글

[HackerRank]Diagonal Difference  (0) 2019.05.21
[Hackerrank]A Very Big Sum  (0) 2019.05.20
[HackerRank]Simple Array Sum  (0) 2019.05.17
1181  (0) 2019.03.01
1427  (0) 2019.03.01