CS50:信用。为什么是“ 4062901840”如果确实将其他10位数字评估为无效的,则不会被评估为无效?
解决 CS50信用问题集后,我唯一的问题是卡号 4062901840 ,不会通过测试。运行Check50说,输入此号码后,它应该打印“无效” (正确,是10位数字),但而不是打印” 。
已经使用10个数字进行了20次测试,并且所有20个都正确通过了测试,因此代码非常简单,因为我尝试仅使用到目前为止所教的内容,并且不使用数组或其他库。您能指出我朝正确的方向指出,知道为什么会发生这种情况吗?
该程序应要求一个数字,并评估它是否符合Luhn算法,该算法是:
将其他所有数字乘以2,从数字开始 首先是数字,然后将这些产品的数字添加在一起。
将总和不乘以2。
的数字总和。- 中 Modulo 10与0)一致,该数字有效!
之后,该程序应检查卡号是AMEX,VISA,MasterCard还是Invelid
每种卡类型的条件是:
-American Express(15位数字) 从34或37
-MasterCard(16位数字) 开始 从51、52、53、53或55
签证(13或16位) 开始 启动4
是我的代码
#include< cs50.h>
#include< stdio.h>
int主(void) {
long cnum, cnumClone;
int count, first, tempo, sum = 0;
do{
printf("Enter card number\n");
scanf("%ld", &cnum);
} while(cnum == 0);
// Clones card number to manipulate it through the iteration
cnumClone = cnum;
//Count every digit of the entered card number
for(count = 1; cnumClone != 0; count++)
{
//Get the last digit
tempo = cnumClone % 10;
//Remove last digit
cnumClone /= 10;
//Select digits to be multiplied
if(count % 2 == 0)
{
tempo *= 2;
//In case the product is a 2 digit number
if (tempo >=10)
{
tempo = tempo % 10;
tempo += 1;
sum += tempo;
}else{
//Add to the sum
sum += tempo;
}
}else{
//Add to the sum directly if it wasn´t a every other digit
sum += tempo;
}
}
//Last step of Luhn´s algorithm
if (sum % 10 == 0)
{
//Since count initiates on 1 for iteration purposes, take away 1
count -= 1;
// If card number length is 16 checks if it´s a mastercard or visa
if(count == 16)
{
first = cnum / 100000000000000;
if(first == 51 || first== 52 || first == 53 || first == 54 || first == 55)
{
printf("MASTERCARD\n");
}else{
first = first /10;
if(first == 4)
{
printf("VISA\n");
}else{
printf("INVALID\n");
}
}
}
// If card number length is 15 checks if it´s an amex
if(count == 15)
{
first = cnum / 10000000000000;
if(first == 34 || first == 37)
{
printf("AMEX\n");
}else{
printf("INVALID\n");
}
}
// If card number length is 15 checks if it´s a visa
if (count == 13)
{
first = cnum / 1000000000000;
if(first == 4)
{
printf("VISA\n");
}
}
}else{
//If card number has a length different than 13, 15 or 16
printf("INVALID\n");
}
}
预先感谢,希望我以正确的方式使用此论坛。
After resolving the CS50 credit problem set, my only problem is that card number 4062901840, does not pass the test. Running check50 says that after enter this number, it should print "Invalid" (which is correct, is a 10 digit number) but instead prints " ".
Already ran 20 tests using 10 digit numbers and all 20 passed the test correctly, the code is pretty simple since i tried to use only what we´ve been taught so far and don´t use arrays or other libraries. Can you point me in the right direction to know why this is happening?
The program should ask for a number, and evaluates if it meets the luhn algorithm, which is:
Multiply every other digit by 2, starting with the number’s
second-to-last digit, and then add those products’ digits together.Add the sum to the sum of the digits that weren’t multiplied by 2.
If the total’s last digit is 0 (or, put more formally, if the total
modulo 10 is congruent to 0), the number is valid!After that, the program should check if the card number is amex, visa, mastercard or invalid
The conditions for every card type are:
-American Express (15 digits)
Start with 34 or 37
-Mastercard (16 digits)
Start with 51, 52, 53, 53 OR 55
Visa(13 or 16 digits)
Startt with 4
Here is my code
#include <cs50.h>
#include <stdio.h>
int main(void)
{
long cnum, cnumClone;
int count, first, tempo, sum = 0;
do{
printf("Enter card number\n");
scanf("%ld", &cnum);
} while(cnum == 0);
// Clones card number to manipulate it through the iteration
cnumClone = cnum;
//Count every digit of the entered card number
for(count = 1; cnumClone != 0; count++)
{
//Get the last digit
tempo = cnumClone % 10;
//Remove last digit
cnumClone /= 10;
//Select digits to be multiplied
if(count % 2 == 0)
{
tempo *= 2;
//In case the product is a 2 digit number
if (tempo >=10)
{
tempo = tempo % 10;
tempo += 1;
sum += tempo;
}else{
//Add to the sum
sum += tempo;
}
}else{
//Add to the sum directly if it wasn´t a every other digit
sum += tempo;
}
}
//Last step of Luhn´s algorithm
if (sum % 10 == 0)
{
//Since count initiates on 1 for iteration purposes, take away 1
count -= 1;
// If card number length is 16 checks if it´s a mastercard or visa
if(count == 16)
{
first = cnum / 100000000000000;
if(first == 51 || first== 52 || first == 53 || first == 54 || first == 55)
{
printf("MASTERCARD\n");
}else{
first = first /10;
if(first == 4)
{
printf("VISA\n");
}else{
printf("INVALID\n");
}
}
}
// If card number length is 15 checks if it´s an amex
if(count == 15)
{
first = cnum / 10000000000000;
if(first == 34 || first == 37)
{
printf("AMEX\n");
}else{
printf("INVALID\n");
}
}
// If card number length is 15 checks if it´s a visa
if (count == 13)
{
first = cnum / 1000000000000;
if(first == 4)
{
printf("VISA\n");
}
}
}else{
//If card number has a length different than 13, 15 or 16
printf("INVALID\n");
}
}
Thanks in advance, hope i use this forum the right way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在这里,
短语关闭中的第一个
}
是什么?提示:这与长度无关。建议长度测试是... else如果... else if ... else; (新的)最终其他输出无效的消息。The problem is here
What does the first
}
in the phrase close? Hint: it has nothing to do with the length. Suggest the length tests be if...else if...else if...else; the (new) final else outputs the INVALID message.在此行
考虑添加另一个块之前,要照顾不到13的块,它对我有用:就像
希望有帮助的那样
Before this line
Consider adding another else block, that takes care of less than 13, it works for me: like so
Hope that helps