【洛谷-P1055】解题报告(字符串)

原始题目

P1055 ISBN号码

题目大意

对于每一个ISBN编号,最后末位是各位乘对应数组模11的结果,对于每一个ISBN检测是否符合要求。

解题思路

按字符串输入做判断即可

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<string, string> pss;

#define rep(i, a, n) for (int i = a; i < n; ++i)
#define per(i, a, n) for (int i = n - 1; i >= a; --i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define np next_permutation
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define endl '\n'

int main()
{
ios::sync_with_stdio(false);
string s;
while (cin >> s) {
stringstream ss(s);
char checkstring[20], temp;
int cnt = 1;

while (ss >> temp) {
if (temp != '-')
checkstring[cnt++] = temp;
}
int ans = 0;
rep(i, 1, 10) ans += (checkstring[i] - '0') * i;
ans %= 11;
if (ans == 10)
ans = 'X';
else
ans += '0';
if (ans != checkstring[10]) {
rep(i, 0, s.length() - 1) cout << s[i];
cout << (char)ans << endl;
} else
cout << "Right" << endl;
}
}

收获与反思