반응형
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. |
code>>
bool isLucky(int n) {
int temp = n;
std::vector<int> v;
while (temp > 0)
{
v.push_back(temp % 10);
temp = temp / 10;
}
int sum = 0, sum1 = 0;
for (int i = 0; i< v.size() / 2; i++)
{
sum += v[i];
sum1 += v[i + v.size() / 2];
}
return sum == sum1 ? true : false;
}
반응형
'알고리즘 > codefights' 카테고리의 다른 글
13>reverseParentheses (0) | 2017.06.28 |
---|---|
12>sortByHeight (0) | 2017.06.28 |
10>commonCharacterCount (0) | 2017.06.28 |
9>allLongestStrings (0) | 2017.06.28 |
8>matrixElementsSum (0) | 2017.06.28 |