Easy

A boy is walking a long way from school to his home. To make the walk more fun he decides to add up all the numbers of the houses that he passes by during his walk. Unfortunately, not all of the houses have numbers written on them, and on top of that the boy is regularly taking turns to change streets, so the numbers don't appear to him in any particular order.

At some point during the walk the boy encounters a house with number 0 written on it, which surprises him so much that he stops adding numbers to his total right after seeing that house.

For the given sequence of houses determine the sum that the boy will get. It is guaranteed that there will always be at least one 0 house on the path.

Example

For inputArray = [5, 1, 2, 3, 0, 1, 5, 0, 2], the output should be
houseNumbersSum(inputArray) = 11.

The answer was obtained as 5 + 1 + 2 + 3 = 11.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    5 ≤ inputArray.length ≤ 10,
    0 ≤ inputArray[i] ≤ 10.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
int houseNumbersSum(arr_integer inputArray)
{
	int sum=0;

	for(int i=0;inputArray.arr[i]!=0;i++)
		sum+=inputArray.arr[i];

	return sum;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Alphabet Subsequence  (0) 2020.05.04
<Codesignal> House of Cats  (0) 2020.05.04
<Codesignal> Numbers of Clans  (0) 2020.05.03
<Codesignal> Numbers Grouping  (0) 2020.05.02
<Codesignal> Most Frequent Digit Sum  (0) 2020.04.30

+ Recent posts