FBI Universal Control Numbers(UVALive 7784)

Description

The FBI has recently changed its Universal Control Numbers (UCN) for identifying individuals who
are in the FBI’s fingerprint database to an eight digit base 27 value with a ninth check digit. The digits
used are:
0123456789ACDEFHJKLMNPRTVWX
Some letters are not used because of possible confusion with other digits:
B->8, G->C, I->1, O->0, Q->0, S->5, U->V, Y->V, Z->2
The check digit is computed as:
(2 ∗ D1 + 4 ∗ D2 + 5 ∗ D3 + 7 ∗ D4 + 8 ∗ D5 + 10 ∗ D6 + 11 ∗ D7 + 13 ∗ D8) mod 27
Where Dn is the n-th digit from the left.
This choice of check digit detects any single digit error and any error transposing an adjacent pair
of the original eight digits.
For this problem, you will write a program to parse a UCN input by a user. Your program should
accept decimal digits and any capital letter as digits. If any of the confusing letters appear in the input,
you should replace them with the corresponding valid digit as listed above. Your program should
compute the correct check digit and compare it to the entered check digit. The input is rejected if they
do not match otherwise the decimal (base 10) value corresponding to the first eight digits is returned.

Input

The first line of input contains a single decimal integer P, (1 ≤ P ≤ 10000), which is the number of
data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by a
single space, followed by 9 decimal digits or capital (alphabetic) characters.

Output

For each data set there is one line of output. The single output line consists of the data set number, K,
followed by a single space followed by the string ‘Invalid!’ (without the quotes) or the decimal value
corresponding to the first eight digits.

Sample Input

3
1 12345678A
2 12435678A
3 12355678A

Sample Output

1 11280469652
2 Invalid
3 Invalid

题解:

根据公式和字符对应值求是否满足条件,满足输出这个27进制数,不满足输出“Invalid”。

代码如下:

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 27
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const ll maxn=;
using namespace std;


char a[9];
ll b[9];
ll c[28]={10,8,11,12,13,14,11,15,1,16,17,18,19,20,0,21,0,22,5,23,24,24,25,26,24,2};
int main()
{
    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        ll k;
        memset(a,0);
        cin>>k>>a;
        cout<<k<<" ";
        for(ll j=0;j<9;j++)
        {
            if(a[j]>='0'&&a[j]<='9')
                b[j]=a[j]-'0';
            if(a[j]>='A'&&a[j]<='Z')
                b[j]=c[a[j]-'A'];
        }
        ll qa=2*b[0]+4*b[1]+5*b[2]+7*b[3]+8*b[4]+10*b[5]+11*b[6]+13*b[7];
        if(qa%27!=b[8])
        {
            cout<<"Invalid"<<endl;
            continue;
        }
        ll s=1,sum=0;
        for(ll j=7;j>=0;j--)
        {
            sum+=b[j]*s;
            s*=27;
        }
        cout<<sum<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值