int sum = 0; boolean even = true; for ( int index = digits.size() - 1; index >= 0; index-- ) { int digit = digits.get( index );
if ( even ) { digit <<= 1; } if ( digit > 9 ) { digit -= 9; } sum += digit; even = !even; } return ( 10 - ( sum % 10 ) ) % 10;
python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defcheck_bank_card(card_num): """检查银行卡的合法性""" total = 0 even = True if isinstance(card_num, int): card_num = str(card_num) check_num = card_num[-1] for item in card_num[-2::-1]: item = int(item) if even: item <<= 1 if item > 9: item -= 9 total += item even = not even return int(check_num) is (10 - (total % 10)) % 10