对字符串的每个数字进行排序
Sort every digit of a string
我正在尝试用 C 实现 Luhn 算法。我的问题是我的循环根本不起作用。
我尝试将其除以 10 以对不同变量中的数字进行排序。
对于下面示例中的数字 378282246310005,我需要获取:
5+0+1+6+2+8+8+3 = 33 (total_odd_numbers).
然后我将所有其他数字乘以 2(得到 0+0+6+8+4+4+14)并将它们的所有数字相加:0+0+6+8+4+4+ 1+4=27 (total_even_numbers).
最后我将偶数和奇数相加,然后除以 10。
在这种情况下,33+27=60,所以我的下一个测试 ((60%10) != 0) 通过了。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
for(int check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)
{
if ((check_digit_basis % 2) > 0)
{
printf("line 53 worked\n");
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
printf("%i", even_number);
}
else
{
total_even_numbers = total_even_numbers + (2 * even_number);
}
}
}
Luhn_sum = total_even_numbers + total_odd_numbers;
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
我刚刚得到:
Provide your credit card number:
378282246310005
15
Line 43 worked
line 49 executed
INVALID
有人能告诉我为什么 "line 53 worked" 没有显示吗?
编辑:我改变了一些东西,total_odd_numbers 似乎是正确的,但 total_even_number 不是。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
long long check_digit_basis = credit_card_number;
for(int i =1 ; check_digit_basis > 0 ; i++)
{
if ((i % 2) > 0)
{
printf("line 53 worked\n");
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
printf("%i total odd\n", total_odd_numbers);
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
printf("%i test total even\n", total_even_numbers);
}
else
{
total_even_numbers = total_even_numbers + (2 * even_number);
printf("%i test total even\n", total_even_numbers);
}
}
check_digit_basis = (check_digit_basis / 10);
}
Luhn_sum = total_even_numbers + total_odd_numbers;
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
我得到:
Provide your credit card number:
378282246310005
15
Line 43 worked
line 49 executed
line 53 worked
5 total odd
0 test total even
line 53 worked
5 total odd
0 test total even
line 53 worked
6 total odd
6 test total even
line 53 worked
12 total odd
14 test total even
line 53 worked
14 total odd
18 test total even
line 53 worked
22 total odd
22 test total even
line 53 worked
30 total odd
36 test total even
line 53 worked
33 total odd
INVALID
因为 total_odd_numbers 看起来是正确的,所以更令人满意,但我想知道我的 total_even_number 公式有什么问题。
我 运行 另一个测试(获取每个单独的值 even_number 需要同时使输出更清晰),我看到程序看到我的偶数 (0, 0, 3, 4, 2 , 2, 7) 但加法不正确...
问题出在这一行:
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
我似乎无法将数字分开来制作 22+1+5 而不是我得到的 22+14。
编辑 2:
经过 运行 几次测试后,我设法找出了我的问题所在。在这里,我看不出我做错了什么,因为我给 Luhn_validity 赋了一个值,但它仍然希望它等于 60。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
long long check_digit_basis = credit_card_number;
for(int i =1 ; check_digit_basis > 0 ; i++)
{
if ((i % 2) > 0)
{
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
printf("%i >5\n", even_number);
int testouille = (( 2 * even_number) % 10);
printf("%i testouille\n", testouille);
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 1 );
printf("%i test total even\n", total_even_numbers);
}
else
{
printf("%i <5\n", even_number);
total_even_numbers = total_even_numbers + (2 * even_number);
printf("%i test total even\n", total_even_numbers);
}
}
check_digit_basis = (check_digit_basis / 10);
}
Luhn_sum = total_even_numbers + total_odd_numbers;
printf("%i", Luhn_sum);
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
printf("%c\n", Luhn_validity);
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
提前致谢。
您正在尝试将 long long
放入 int
。这不适合这种情况。尝试更改第 32 行的 for
:
for (long long check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)
check_digit_basis = (int)(378282246310005LL)
在我的系统上可能为负数 (-1293252491
),在这种情况下 (check_digit_basis % 2)
将为负数。
我正在尝试用 C 实现 Luhn 算法。我的问题是我的循环根本不起作用。
我尝试将其除以 10 以对不同变量中的数字进行排序。 对于下面示例中的数字 378282246310005,我需要获取: 5+0+1+6+2+8+8+3 = 33 (total_odd_numbers).
然后我将所有其他数字乘以 2(得到 0+0+6+8+4+4+14)并将它们的所有数字相加:0+0+6+8+4+4+ 1+4=27 (total_even_numbers).
最后我将偶数和奇数相加,然后除以 10。 在这种情况下,33+27=60,所以我的下一个测试 ((60%10) != 0) 通过了。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
for(int check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)
{
if ((check_digit_basis % 2) > 0)
{
printf("line 53 worked\n");
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
printf("%i", even_number);
}
else
{
total_even_numbers = total_even_numbers + (2 * even_number);
}
}
}
Luhn_sum = total_even_numbers + total_odd_numbers;
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
我刚刚得到:
Provide your credit card number:
378282246310005
15
Line 43 worked
line 49 executed
INVALID
有人能告诉我为什么 "line 53 worked" 没有显示吗?
编辑:我改变了一些东西,total_odd_numbers 似乎是正确的,但 total_even_number 不是。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
long long check_digit_basis = credit_card_number;
for(int i =1 ; check_digit_basis > 0 ; i++)
{
if ((i % 2) > 0)
{
printf("line 53 worked\n");
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
printf("%i total odd\n", total_odd_numbers);
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
printf("%i test total even\n", total_even_numbers);
}
else
{
total_even_numbers = total_even_numbers + (2 * even_number);
printf("%i test total even\n", total_even_numbers);
}
}
check_digit_basis = (check_digit_basis / 10);
}
Luhn_sum = total_even_numbers + total_odd_numbers;
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
我得到:
Provide your credit card number:
378282246310005
15
Line 43 worked
line 49 executed
line 53 worked
5 total odd
0 test total even
line 53 worked
5 total odd
0 test total even
line 53 worked
6 total odd
6 test total even
line 53 worked
12 total odd
14 test total even
line 53 worked
14 total odd
18 test total even
line 53 worked
22 total odd
22 test total even
line 53 worked
30 total odd
36 test total even
line 53 worked
33 total odd
INVALID
因为 total_odd_numbers 看起来是正确的,所以更令人满意,但我想知道我的 total_even_number 公式有什么问题。
我 运行 另一个测试(获取每个单独的值 even_number 需要同时使输出更清晰),我看到程序看到我的偶数 (0, 0, 3, 4, 2 , 2, 7) 但加法不正确...
问题出在这一行:
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
我似乎无法将数字分开来制作 22+1+5 而不是我得到的 22+14。
编辑 2:
经过 运行 几次测试后,我设法找出了我的问题所在。在这里,我看不出我做错了什么,因为我给 Luhn_validity 赋了一个值,但它仍然希望它等于 60。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
long long check_digit_basis = credit_card_number;
for(int i =1 ; check_digit_basis > 0 ; i++)
{
if ((i % 2) > 0)
{
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
printf("%i >5\n", even_number);
int testouille = (( 2 * even_number) % 10);
printf("%i testouille\n", testouille);
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 1 );
printf("%i test total even\n", total_even_numbers);
}
else
{
printf("%i <5\n", even_number);
total_even_numbers = total_even_numbers + (2 * even_number);
printf("%i test total even\n", total_even_numbers);
}
}
check_digit_basis = (check_digit_basis / 10);
}
Luhn_sum = total_even_numbers + total_odd_numbers;
printf("%i", Luhn_sum);
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
printf("%c\n", Luhn_validity);
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
提前致谢。
您正在尝试将 long long
放入 int
。这不适合这种情况。尝试更改第 32 行的 for
:
for (long long check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)
check_digit_basis = (int)(378282246310005LL)
在我的系统上可能为负数 (-1293252491
),在这种情况下 (check_digit_basis % 2)
将为负数。