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.
题意:给几个银行卡号,然后判断该银行卡是否有效,至于怎么判断,全在红字了
翻译一下就是
1.从左到右标记所有数字1到16。
2.将每个奇数标记的数字乘以2。
3.如果任何数字的结果大于9,从中减去9。
4.把上一步的结果相加,再加上所有偶数标号的总和。
5.如果结果是10的倍数,则卡号是有效的;否则,它是无效的。
其实主要是英文看不懂 很绝望...加上样例有点毛病.
下面上代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;
int mm[20];
char card[5][5],ling[5]="0000";
int main()
{
while(1)
{
int k=0;
for(int i=0;i<4;i++)
{
cin>>card[i];
for(int j=0;j<4;j++)
mm[k++]=card[i][j]-'0';
}
int flag=1;
for(int i=0;i<4;i++)
{
if(strcmp(card[i],ling)!=0)
{
flag=0;
break;
}
}
if(flag) return 0;//结束输入
int ans=0;//暴力模拟条件特判
for(int i=0;i<16;i++)
{
if(i%2==0)//如果是奇数
{
mm[i]=mm[i]*2;
if(mm[i]>9)//如果值大于9
mm[i]-=9;
ans+=mm[i];
}
else
ans+=mm[i];
}
if(ans%10==0)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
ps:2018-04-12 22:56:24
b题花了太长时间... 这题题目还没完全看明白的时候已经没时间了
很绝望,看来还是要多练练英文题...英语一定要好啊