11743 - Credit Check

These days, it has become commonplace to make purchases over the internet using a credit card. However, because credit card numbers are relatively long, it is easy to make a mistake while typing them in. In order to quickly identify errors like typos, most e-commerce websites use a checksum algorithm to verify credit card numbers.

One popular checksum algorithm is the Luhn algorithm, which can detect any single-digit error as well as many common multiple-digit errors:

  1. Starting with the second-last digit and moving backwards, double every other digit to obtain a list of numbers.
  2. Add up the digits of these numbers, then add the undoubled digits from the original number. Sum the two results.
  3. If the total ends in a 0, the credit card number is valid, and it is invalid otherwise.

For example, using the number 5181 2710 9900 0012:

  1. Double the appropriate digits (5181 2710 9900 0012) to obtain the values: 10, 16, 4, 2, 18, 0, 0, 2.
  2. Add up the digits of these values to get (1+0) + (1+6) + 4 + 2 + (1+8) + 0 + 0 + 2 = 25. The sum of the undoubled digits is 1+1+7+0+9+0+0+2 = 20, so the total is 20+25=45.
  3. 45 does not end in a 0, so this credit card number is invalid.

For this problem, you must write a program that checks the validity of credit card numbers according to the Luhn algorithm.

Input Format

The input begins with a number N on a single line, followed by N lines each containing a single credit card number. Each credit card number consists of 16 decimal digits in groups of four separated by single spaces.

Output Format

The output consists of one line for each input credit card number. If the credit card number is valid, this line consists of the string "Valid", otherwise it reads "Invalid".

今日,用信用卡网路购物已经变得相当普遍,由于使用者可能打错信用卡号,所以一般电子商务型网站都会对信用卡号作检查。
其中一种错误检查机制称为Luhn algorithm,它可以把所有打错一个位数的错误找出来,甚至于能挑出打错多个位数的错误,它的检查规则如下:
用一个例子来讲解会比较方便,例如信用卡号(5181 2710 9900 0012):

   
1. 将偶数位置上的数字乘2,也就是将( 5181 2710 9900 0012 )中粗体底线的数字乘2,得到10, 16, 4, 2, 18, 0, 0, 2。
   
2. 将刚刚所得到的数字中每一个位数数值加总,即(1+0) + (1+6) + 4 + 2 + (1+8) + 0 + 0 + 2 = 25。将信用卡号中奇数位数的数字作加总,即1+1+7+0+9+0+0+2 = 20,再将两数相加25+20=45。
   
3. 45的个位数并非0,所以这个信用卡号并不合法。

本题请你用此法检查信用卡号是否正确。

Sample Input

2
5181 2710 9900 0012
5181 2710 9900 0017

Sample Output

Invalid
Valid

 

解题思路:题目已经给定公式,注意输入方式每四个数之间要有一个空行,输入的是char类型字符%d输出的是ACSII值

#include<stdio.h>
int main()
{int i,n,sum;
char a[5],b[5],c[5],d[5];
scanf("%d",&n);
while(n--){
           scanf("%s %s %s %s",a,b,c,d);
           sum=(2*(a[0]-'0'))%10+(2*(a[2]-'0'))%10+(2*(b[0]-'0'))%10+(2*(b[2]-'0'))%10;
           sum+=(2*(c[0]-'0'))%10+(2*(c[2]-'0'))%10+(2*(d[0]-'0'))%10+(2*(d[2]-'0'))%10;
           sum+=(2*(a[0]-'0'))/10+(2*(a[2]-'0'))/10+(2*(b[0]-'0'))/10+(2*(b[2]-'0'))/10;
           sum+=(2*(c[0]-'0'))/10+(2*(c[2]-'0'))/10+(2*(d[0]-'0'))/10+(2*(d[2]-'0'))/10;
           sum+=a[1]-'0'+a[3]-'0'+b[1]-'0'+b[3]-'0'+c[1]-'0'+c[3]-'0'+d[1]-'0'+d[3]-'0';
           if(sum%10==0)printf("Valid\n");
           else printf("Invalid\n");
           }
return 0;
}

posted on 2013-02-18 11:11  喂喂还债啦  阅读(577)  评论(0编辑  收藏  举报