Easy

Your Informatics teacher at school likes coming up with new ways to help you understand the material. When you started studying numeral systems, he introduced his own numeral system, which he's convinced will help clarify things. His numeral system has base 26, and its digits are represented by English capital letters - A for 0, B for 1, and so on.

The teacher assigned you the following numeral system exercise: given a one-digit number, you should find all unordered pairs of one-digit numbers whose values add up to the number.

Example

For number = 'G', the output should be
newNumeralSystem(number) = ["A + G", "B + F", "C + E", "D + D"].

Translating this into the decimal numeral system we get: number = 6, so it is ["0 + 6", "1 + 5", "2 + 4", "3 + 3"].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] char number

    A character representing a correct one-digit number in the new numeral system.

    Guaranteed constraints:
    'A' ≤ number ≤ 'Z'.

  • [output] array.string

    • An array of strings in the format "letter1 + letter2", where "letter1" and "letter2" are correct one-digit numbers in the new numeral system. The strings should be sorted by "letter1".

    Note that "letter1 + letter2" and "letter2 + letter1" are equal pairs and we don't consider them to be different.

[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;
// }
//
//
arr_string newNumeralSystem(char number)
{
	arr_string str=alloc_arr_string((number-'A')/2+1);
	for(int i=0;i<str.size;i++)
	{
		str.arr[i]=(char *)malloc(6*sizeof(char));
		sprintf(str.arr[i],"%c + %c",'A'+i, number-i);
	}

	return str;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Stolen Lunch  (0) 2020.05.29
<Codesignal> Cipher 26  (0) 2020.05.29
<Codesignal> Reflect String  (0) 2020.05.24
<Codesignal> Character Parity  (0) 2020.05.24
<Codesignal> Three Split  (0) 2020.05.24

+ Recent posts