琉璃月笙 @ 2022-03-27 21:57:50
num1 = input()
a = num1.split("-")
i = 0
k = 1
all = 0
while i < 3:
temp = int(a[i])
if i > 0:
k = 0
for j in range(0, i+1):
k += len(a[j])
for j in range(0, len(a[i])):
all += k * (temp % 10)
temp /= 10
temp = int(temp)
k -= 1
i += 1
all %= 11
if all == 10:
all = "X"
if str(all) == a[-1]:
print("Right", end="")
else:
a.pop()
a.append(str(all))
for i in range(0,4):
if i != 3:
print(a[i], end='-')
else:
print(a[i], end='')
by Jasper08 @ 2022-03-27 22:06:20
@琉璃月笙 望丰展?使MD
//P1055 ISBN 号码
#include <iostream>
using namespace std;
int main()
{
int cnt = 0, ans = 0;
char num[15];
for (int i = 0; i < 12; ++i)
{
cin >> num[i];
if ('0' <= num[i] && num[i] <= '9') //输入 9个数字
{
cnt += 1;
ans += cnt * (num[i] - '0');
}
}
char tisbn; //转换最后一位验证码
cin >> tisbn;
int isbn;
if ('0' <= tisbn && tisbn <= '9')
isbn = tisbn - '0';
else //此时验证码为 X,即 10
isbn = 10;
if (ans % 11 == isbn)
cout << "Right";
else
{
for (int i = 0; i < 12; ++i)
cout << num[i];
if (ans % 11 == 10)
cout << "X";
else
cout << ans % 11;
}
}
by 琉璃月笙 @ 2022-03-28 10:54:21
@Jasper08 感谢你的分享。
我把代码改成这样莫名其妙的过了,按理说第四个测试答案就是Right,不会涉及原数组内容。原来的是强制类型转换,将all转换成str类型,并与原来的识别码进行比较。而输出则是更新识别码,并重新输出序列,但是是str类型的。但这两种除了4号之外其他都能通过。
想不通为啥。
if "0" <= a[-1] <= "9":
new_a = int(a[-1])
else:
new_a = 10
if all % 11 == new_a:
print("Right", end="")
else:
for i in range(0, 4):
if i != 3:
print(a[i], end='-')
else:
if all % 11 == 10:
print("X",end="")
else:
print(all % 11, end='')
by xuyuanhao5212 @ 2022-04-02 14:59:19
这是其他题的输入
6-670-82162-X
这是第四题的输入
6-670-82162-X\n
所以用负索引就会出错