Valid Card Number
Я прошел обычный тест и рандомный, но не могу пройти advanced_tests
Условие - In this Kata, you will implement the Luhn Algorithm, which is used to help validate credit card numbers.
Given a positive integer of up to 16 digits, return true if it is a valid credit card number, and false if it is not.
Here is the algorithm:
Double every other digit, scanning from right to left, starting from the second digit (from the right).
Another way to think about it is: if there are an even number of digits, double every other digit starting with the first; if there are an odd number of digits, double every other digit starting with the second:
1714 ==> [1*, 7, 1*, 4] ==> [2, 7, 2, 4]
12345 ==> [1, 2*, 3, 4*, 5] ==> [1, 4, 3, 8, 5]
891 ==> [8, 9*, 1] ==> [8, 18, 1] If a resulting number is greater than 9, replace it with the sum of its own digits (which is the same as subtracting 9 from it):
[8, 18*, 1] ==> [8, (1+8), 1] ==> [8, 9, 1]
or:
[8, 18*, 1] ==> [8, (18-9), 1] ==> [8, 9, 1] Sum all of the final digits:
[8, 9, 1] ==> 8 + 9 + 1 = 18 Finally, take that sum and divide it by 10. If the remainder equals zero, the original credit card number is valid.
#include <vector>
#include <algorithm>
using namespace std;
class Kata {
public:
static bool validate(long long int n) {
long long a, b=0;
std::vector<int>arr;
while (n > 0)
{
a = n % 10;
b = b * 10 + a;
n = n / 10;
}
int z = 0;
while (b > 0)
{
z = b % 10;
arr.push_back(z);
b = b / 10;
}
int sum = 0;
if (arr.size() % 2 == 0) {
for (unsigned long i = 0; i < arr.size(); i = i + 2) {
arr[i] = arr[i] + arr[i];
cout << arr[i] << " ";
}
cout << "\n";
for (unsigned long i = 0; i < arr.size(); ++i) {
cout << arr[i] << " ";
}
cout << "\n";
for (unsigned long i = 0; i < arr.size(); i = i + 2) {
if (arr[i] > 10) {
arr[i] = arr[i] - 9;
}
cout << arr[i] << " ";
}
for (unsigned long i = 0; i < arr.size(); i++) {
sum = sum + arr[i];
}
cout << sum << endl;
}else{
for (unsigned long i = 1; i < arr.size(); i = i + 2) {
arr[i] = arr[i] + arr[i];
cout << arr[i] << " ";
}
cout << "\n";
for (unsigned long i = 0; i < arr.size(); ++i) {
cout << arr[i] << " ";
}
cout << "\n";
for (unsigned long i = 0; i < arr.size(); i = i + 1) {
if (arr[i] >=10){
arr[i] = arr[i] - 9;
}
cout << arr[i] << " ";
}
for (unsigned long i = 0; i < arr.size(); i++) {
sum = sum + arr[i];
}
cout<<" "<<endl;
cout << sum << endl;
}
if (sum % 10 == 0) {
return true;
} else {
return false;
}
}
};
Ответы (2 шт):
Вот так попробуйте:
bool validate(long long int n){
std::string input = std::to_string(n);
int len = input.length(),
number = 0,
sum = 0;
bool len_ = len%2
for(int i=len; i>0; --i){
number = input[i]-'0';
if((len_==1 && i%2==0) || (len_==0 && i%2==1)) {
number *= 2;
if(number > 9)
number -= 9;
}
sum += number;
}
return !(sum%10);
}
Воистину, зачем просто, если можно сложно...
static bool validate(long long int n) {
int s = 0;
for(int dbl = 2; n; n/= 10)
{
int m = (n%10)*(dbl = 3-dbl);
if (m > 9) m -= 9;
s += m;
}
return s%10==0;
}
P.S. В следующий раз в таких случаях давайте URL проверялки, а то самому искать - лишние затраты времени, а без него - сплошной испорченный телефон :)