我正在对这段代码进行排序:
bool checkLuhn(const string& cardNo)
{
int nDigits = cardNo.length();
int nSum = 0, isSecond = false;
for (int i = nDigits - 1; i >= 0; i--) {
int d = cardNo[i] - '0';
if (isSecond == true)
d = d * 2;
nSum += d / 10;
nSum += d % 10;
isSecond = !isSecond;
}
return (nSum % 10 == 0);
} 有一个我不知道的谜团。已经用谷歌搜索过了,但仍然是个谜。
该代码:
if (isSecond == true)
d = d * 2;在代码程序的什么地方检测到它可以与2拆分?我知道如果它不能被2拆分,程序就把它乘以2。
我理解程序的工作原理,但必须有一些方法或东西来告诉程序什么是isSecond。帮帮我。
发布于 2020-01-22 02:22:37
现在我明白了。
在for循环之前,isSecond为false,因为first ordernumber为(0)。然后,此isSecond = !isSecond;将false更改为true。然后轮到二阶isSecond = true; (1),方法将其与2相乘,然后再次变为false,不将数字2相乘,然后变为真,并将数字3相乘。
脑筋急转弯:D
https://stackoverflow.com/questions/59845506
复制相似问题