我试图用C ++复制Luhn算法,但我遇到了问题。如你所见,我有两个for循环。在MSVC中,如果我在第二个for循环中放置一个断点,或者甚至在返回MSVC时会告诉我断点不会被击中。
是什么导致了这个问题?
int luhn_checksum(std::vector<int> cardnumber[NUMBER_OF_DIGITS - 1]) {
//step 1: duouble every second number
for (int i = 1; i < NUMBER_OF_DIGITS; i + 2) {
new_digits[i] = digits[i] * 2;
if (new_digits[i] > 9) {
//if the product is larger than 9 we will add the two numbers together
//example: 9 * 2 = 18 so we will add 1 + 8 to get 9
tmp1 += new_digits[i] % 10;
new_digits[i] /= 10;
tmp1 = 0;
}
}
//step 2: sum all the values
for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
checksum += new_digits[i];
}
return checksum;
}
答案 0 :(得分:3)
在第一个for循环中,你没有增量 所以替换
for (int i = 1; i < NUMBER_OF_DIGITS; i += 2)
如果你想增加2