-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week1-credit.c
108 lines (94 loc) · 2.31 KB
/
Week1-credit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int i;
int j; //余数
long n;
int count = 0; //numeber的位数
int sum1 = 0; //倒数第二位开始运算后的和
int sum2 = 0; //倒数第一位开始
int sum3 = 0;
long number = get_long("Number:");
n = number;
for (count = 0; number > 0; count++) //获取number的位数
{
number /= 10;
}
number = n; //恢复number的数值
number /= 10; //倒数第二位数字
j = 2 * (number % 10);
if (j < 10) //判断乘以2的数是否为2位数
{
sum1 += j;
}
else
{
sum1 += j % 10;
sum1 += (j / 10) % 10;
}
while (number > 0) //对每隔2位求和
{
number /= 100;
j = 2 * (number % 10); //乘2
if (j < 10) //判断乘以2的数是否为2位数
{
sum1 += j;
}
else
{
sum1 += j % 10;
sum1 += (j / 10) % 10;
}
}
number = n; //恢复number的数值
j = n % 10;
sum2 += j;
while (number > 0) //对每隔2位求和
{
number /= 100;
j = number % 10;
sum2 += j;
}
number = n;
if ((sum1 + sum2) % 10 == 0) //输出结果
{
while (number > 10) //Visa
{
number /= 10;
j = number % 10;
}
if (j == 4)
{
printf("Visa\n");
}
else
{
number = n; //如果卡号由奇数个数字组成 则无法使用下面的算法 所以需要将奇数个转化为偶数个
if (count % 2 != 0)
{
number /= 10;
}
while (number > 100)
{
number /= 100;
}
if (number == 34 || number == 37) //AMEX
{
printf("AMEX\n");
}
if (number >= 51 && number <= 55) //MasterCard
{
printf("MasterCard\n");
}
else
{
printf("Other Bank\n");
}
}
}
else
{
printf("INVALID\n");
}
}