Luhn's Checksum Algorithm

Luhn’s Checksum Algorithm

In 1954, Hans Peter Luhn, a researcher at IBM, filed a patent describing a simple checksum algorithm for numbers written as strings of base-10 digits. If a number is chosen according to Luhn’s technique, the algorithm provides a basic integrity check. This means that with reasonably high probability it can detect whether one or more digits have been accidentally modified. (On the other hand, it provides essentially no protection against intentional modifications.) Most credit card and bank card numbers can be validated using Luhn’s checksum algorithm, as can the national identification numbers of several countries (including Canada).

Given a number n=dkdk−1…d2d1, where each di is a base-10 digit, here is how to apply Luhn’s checksum test:

1、Starting at the right end of n, transform every second digit di (i.e., d2,d4,d6,…) as follows:

  • multiply di by 2

  • if 2⋅di consists of more than one digit, i.e., is greater than 9, add
    these digits together; this will always produce a single-digit number

2、Add up all the digits of n after the transformation step. If the resulting sum is divisible by 10, n passes the Luhn checksum test. Otherwise, n fails the Luhn checksum test.

For example, consider the number n=1234567890123411 from Sample Input 1. The first row of Figure 1 gives the original digits of n, and the second row contains the digits of n after the transformation step, with transformed digits shown in bold. The sum of the digits in the second row is

2+2+6+4+1+6+5+8+9+0+2+2+6+4+2+1=60
and since 60 is divisible by 10, n passes the Luhn checksum test.
在这里插入图片描述
Figure 1: Application of Luhn’s algorithm to n=1234567890123411
Input
The first line of input contains a single integer T (1≤T≤100), the number of test cases. Each of the following T lines contains a single test case consisting of a number given as a string of base-10 digits (0–9). The length of each string is between 2 and 50, inclusive, and numbers may have leading (leftmost) zeros.

Output
For each test case, output a single line containing “PASS” if the number passes the Luhn checksum test, or “FAIL” if the number fails the Luhn checksum test.

Sample Input 1
3
00554
999
1234567890123411

Sample Output 1
PASS
FAIL
PASS

本题主要在于读懂题意,输入一串数字后,从右往左,每隔2个数字乘以2,若大于10,则取这个两位数,每个数字的和相加。
(例:数字6乘以2等于12,是一个两位数,最终取1 + 2等于3)
然后从右往左奇数位的数字相加,偶数位乘以二取个位数相加,最终将这个累加和数字对10取余,若等于0,则代表这串数字通过,输出PASS,
否则输出FAIL。 PS.本题的数字用char型存入,这样对每一位数组比较好操作。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int T;
	char a[20005];
	cin >> T;
	while (T--)
	{
		cin >> a;
		int s = 0;
		int flag = 1;
		for (int i = strlen(a)-1; i >=0; i--)
		{
			int x;
			x = a[i] - '0';
			if (flag==2)
			{
				flag=1;
				x = x * 2;
				if (x <= 9)
					s = s + x;
				else
				{
					x = x % 10 + 1;
					s = s + x;
				}
			}
			else
			{
				s = s + x;
				flag++;
			}
			//cout << "**" << x << endl;
			
		}
		//cout << "*"<<s << endl;
		if (s % 10 == 0)
			cout << "PASS" << endl;
		else
			cout << "FAIL" << endl;
		
	}
		return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值