Easy
Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half.
Given a ticket number n, determine if it's lucky or not.
Example
- For n = 1230, the output should be
isLucky(n) = true; - For n = 239017, the output should be
isLucky(n) = false.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] integer n
A ticket number represented as a positive integer with an even number of digits.
Guaranteed constraints:
10 ≤ n < 10^6. -
[output] boolean
- true if n is a lucky ticket number, false otherwise.
[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
bool isLucky(int n)
{
int front=0, back=0;
char number[8]={'\0', };
sprintf(number,"%d", n);
for(int i=0;i<strlen(number)/2;i++)
{
front+=number[i]-'0';
back+=number[strlen(number)-1-i]-'0';
}
return front==back;
}
728x90
'Codesignal' 카테고리의 다른 글
<Codesignal> reverseInParentheses (0) | 2020.04.05 |
---|---|
<Codesignal> Sort by Height (0) | 2020.04.05 |
<Codesignal> commonCharacterCount (0) | 2020.04.05 |
<Codesignal> All Longest Strings (0) | 2020.04.05 |
<Codesignal> matrixElementsSum (0) | 2020.04.05 |