Problem Description
source:https://uva.onlinejudge.org/external/117/p11743.html
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
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
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’.
Sample Input
2
5181 2710 9900 0012
5181 2710 9900 0017
Sample Output
Invalid
Valid
source:https://uva.onlinejudge.org/external/117/p11743.html
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
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
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’.
Sample Input
2
5181 2710 9900 0012
5181 2710 9900 0017
Sample Output
Invalid
Valid
Solution:
#include <algorithm> #include <cstdio> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <iostream> #include <string> #include <vector> #include<stdio.h> #include<stdlib.h> using namespace std; void Check(int m); int sumodd, sumeven,total; int main() { int t,i,a[5],j,n,k,mod; while(scanf("%d",&t)==1) { for(i=1;i<=t;i++) { sumodd=0; sumeven=0; for(j=1;j<=4;j++) { scanf("%d",&a[j]); n=a[j]; k=1; while(n!=0) { mod=n%10; n=n/10; if(k%2==1) sumodd+=mod; else if(k%2==0) Check(mod*2); k+=1; } } total=sumeven+sumodd; if(total%10==0) printf("Valid\n"); else printf("Invalid\n"); } } return 0; } void Check(int m) { int mod1; if(m<10) sumeven+=m; else { while(m!=0) { mod1=m%10; m=m/10; sumeven+=mod1; } } }
No comments:
Post a Comment
Write your comment - Share Knowledge and Experience