QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 64 MB Points totaux : 50

#13588. Lun

Statistiques

Tihana 妈妈想在一家网上商店给她的女儿 Leda 买一个毛绒玩具。在购买过程中,系统要求输入信用卡号。然而,由于 Tihana 不小心写错了卡号中的一位数字,购买失败了。互联网搜索表明,此类系统是通过 Luhn 算法来识别错误卡号的。

该算法使用一个校验位(总是卡号的最后一位)来确认卡号的正确性。确定卡号有效性的步骤如下:

  • 从卡号右起第二位(十位)开始,向左每隔一位将该位数字乘以 $2$。如果乘积大于 $9$,则将该乘积的各位数字相加(即求数位和)。
  • 计算上一步中获得的所有数值的和(未被加倍的数字保持原样,最后一位校验位不参与求和)。
  • 将得到的总和乘以 $9$,然后求其除以 $10$ 的余数。
  • 如果得到的余数等于卡号的最后一位(个位),则该卡号被认为是有效的。

例如,卡号 79927398713 被认为是有效的,因为最右边的数字 $3$ 可以通过上述方法从其余数字中计算得出。

卡号 7 9 9 2 7 3 9 8 7 1 3
隔位翻倍 7 18 9 4 7 6 9 16 7 2 -
求和 7 9 (1+8) 9 4 7 6 9 7 (6+1) 7 2 = 67

$$(\text{求和结果} \times 9) \bmod 10 = (67 \times 9) \bmod 10 = 603 \bmod 10 = 3$$

编写一个程序,读入一个长度为 $N$ 的字符串表示卡号,该字符串仅由数字和恰好一个字符 x 组成。输出可以替换字符 x 的最小的一位数($0$ 到 $9$ 之间的整数),使得该卡号有效。

输入格式

第一行包含一个整数 $N$ ($1 \le N \le 100$),表示字符串的长度。

第二行包含一个长度为 $N$ 的字符串,仅由数字字符 09 以及恰好一个字符 x 组成。

输出格式

输出唯一的一行,包含所求的一位数。

子任务

在价值 $25$ 分的测试数据中,字符 x 总是位于字符串的最后一位。

样例

输入样例 1

11
7992739871x

输出样例 1

3

输入样例 2

5
x2464

输出样例 2

5

输入样例 3

10
93380x1696

输出样例 3

1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.