B-Bank Card Verifier


B(1260): Bank Card Verifier

Description

Cafe Bazaar, the famous Iranian android market, is looking for creative software developers. A group of applicants are attending an interview, and the company wants to select the fastest developer who can code simple rules accurately. As a test, all applicants should quickly develop a bank card verifier that determines whether a payment card number is valid or not. All payment card numbers are 16 digits long. The leftmost 6 digits represent a unique identification number for the bank who has issued the card. The next 2 digits determine the type of the card (e.g., debit, credit, gift). Digits 9 to 15 are the serial number of the card, and the last digit is used as a control digit to verify whether the card number is valid. Hence, if somebody enters the card number incorrectly, there is a high chance that a payment software can easily determine it. For a valid card number, the last digit is selected in such a way that the following algorithm passes: 1. Label all digits from left to right by 1 to 16. 2. Multiply each odd-labeled digit by 2. 3. If the result for any digit is greater than 9, subtract 9 from it. 4. Sum the results of the previous step, and add to it the sum of all even-labeled digits. 5. If the result is a multiple of 10, the card number is valid; otherwise, it is invalid. Your task is to read several card numbers from the input, and determine whether each one is a valid card number or not.

Input

There are multiple test cases in the input. Each test is given in one line consisting of four space-separated 4-digit strings. The leftmost digit of the given card number is guaranteed to be non-zero. The input terminates with a line containing “0000 0000 0000 0000” that should not be processed.

Output

For each test case, output a line containing “Yes” or “No” depending on whether the card number is valid or not, respec- tively.

Sample Input

6104 3376 7866 1545
6104 3376 7866 1546
5022 2910 0140 7954
0000 0000 0000 0000

Sample Output

Yes 
No 
Yes


签到题:很简单的思路,将奇数位乘二得到的数如果大于9的话就减去9,开始的时候队友说模9,其实有区别的。

然后将所有位置上的数加起来模10,余数为0输出Yes,否则输出No


#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int m[20];
string s;
int main()
{
	while(getline(cin,s))
	{
		if(s=="0000 0000 0000 0000")
		   return 0;
		else
		{
			int len=s.length();
			int cnt=1;
			for(int i=0;i<len;i++)
			{
				if(s[i]!=' ')
				{
				   m[cnt++]=s[i]-'0';
				}
			}
			for(int i=1;i<=16;i+=2)
			{
				if(m[i]*2>9)
				{
					m[i]=m[i]*2-9;
				}
				else
				{
					m[i]=m[i]*2;
				}
			}
			int sum=0;
			for(int i=1;i<=16;i++)
			{
				sum+=m[i];
			} 
			if(sum%10==0)
			{
				cout<<"Yes"<<endl;
			}
			else
			{
				cout<<"No"<<endl;
			}
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值