Warehouse shelf labels carry a code of English letters and digits.
The labels were copied by hand, so they may contain lowercase letters, and spaces and '-' may appear anywhere.
The first line gives the number of labels n, and each of the next n lines gives one label.
For each label, remove every space and '-' and turn lowercase letters into uppercase; the result is the code.
Each character of the code has a value: '0'–'9' are 0–9 and 'A'–'Z' are 10–35.
Multiply the value of the i-th character of the code from the left (counting from 1) by i, add them all up, and let r be the remainder of that sum divided by 36.
The check character is the digit '0'–'9' when r is 0–9, and 'A'–'Z' when r is 10–35 (10 is 'A', 35 is 'Z').
For each label print the code, '-', and the check character joined together on one line.
Input
First line n (1 ≤ n ≤ 1000). Each of the next n labels is 1–80 characters long without the newline, and consists only of English letters (either case), digits, spaces and '-'. A line may start or end with spaces, and every label has at least one letter or digit
Output
n lines, each "code-check"
Example 1 · stdin
3
sh-04 b
q7-X
9
stdout
SH04B-P
Q7X-V
9-9
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Two things are compared. (1) Reading a whole line that contains spaces: Python input()/sys.stdin.readline(), JS/TS read everything and split("\n"), Java BufferedReader.readLine(), C# Console.ReadLine(), C fgets, C++ std::getline, Craykon Console.ReadLine(). C++ cin >> s, Java Scanner.next() and C scanf("%s") stop at a space and read sh-04 b as two pieces. Mixing numbers and lines has its own trap: C scanf("%d"), C++ cin >> n and Java Scanner.nextInt() leave the newline of the first line behind, so the next fgets/getline/nextLine() reads an empty line. Reading the first line as a line and converting it is safe. (2) Moving between characters and numbers: in C, C++, Java and C# a char is a number, so c - '0' and c - 'A' + 10 give the value directly. Going back, 'A' + r - 10 is an int, so handing it to Java StringBuilder.append, C# StringBuilder.Append or C++ std::cout << appends 25 instead of P. Convert it with (char) (static_cast<char> in C++). C printf("%c", ...) takes an int and prints the character. Python, JS, TS and Craykon have no character type, so you go through codes: Python ord/chr (or int(c, 36) gives 0–35 directly), JS/TS charCodeAt/String.fromCharCode, Craykon Str.Code/Str.FromCode. In JS "7" - "0" is 7, but that does not work for letters: using charCodeAt(0) - 48 for every character makes 'B' 18 instead of 11. And if you do not uppercase first, 'b' - 'A' + 10 is 43.
Pythonmain.py · 22 lines
import sysdef value(c): # '0'~'9' → 0~9, 'A'~'Z' → 10~35 (글자 코드의 차이로 계산) if c.isdigit(): return ord(c) - ord("0") return ord(c) - ord("A") + 10def check_char(r): if r < 10: return chr(ord("0") + r) return chr(ord("A") + r - 10)n = int(sys.stdin.readline())for _ in range(n): line = sys.stdin.readline().rstrip("\n") # 빈칸이 든 한 줄을 통째로 code = "".join(c.upper() for c in line if c not in " -") total = sum(value(c) * i for i, c in enumerate(code, 1)) print(f"{code}-{check_char(total % 36)}")
JavaScriptmain.js · 20 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const ZERO = "0".charCodeAt(0);const A = "A".charCodeAt(0);// '0'~'9' → 0~9, 'A'~'Z' → 10~35const value = (c) => { const k = c.charCodeAt(0); return k <= "9".charCodeAt(0) ? k - ZERO : k - A + 10;};const checkChar = (r) => String.fromCharCode(r < 10 ? ZERO + r : A + r - 10);const out = [];for (let i = 1; i <= n; i++) { const code = lines[i].replace(/[ -]/g, "").toUpperCase(); let total = 0; for (let j = 0; j < code.length; j++) total += value(code[j]) * (j + 1); out.push(`${code}-${checkChar(total % 36)}`);}console.log(out.join("\n"));
TypeScriptmain.ts · 23 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const ZERO: number = "0".charCodeAt(0);const A: number = "A".charCodeAt(0);// '0'~'9' → 0~9, 'A'~'Z' → 10~35function value(c: string): number { const k: number = c.charCodeAt(0); return k <= "9".charCodeAt(0) ? k - ZERO : k - A + 10;}function checkChar(r: number): string { return String.fromCharCode(r < 10 ? ZERO + r : A + r - 10);}const out: string[] = [];for (let i = 1; i <= n; i++) { const code: string = lines[i].replace(/[ -]/g, "").toUpperCase(); let total = 0; for (let j = 0; j < code.length; j++) total += value(code[j]) * (j + 1); out.push(`${code}-${checkChar(total % 36)}`);}console.log(out.join("\n"));
JavaMain.java · 30 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { // '0'~'9' → 0~9, 'A'~'Z' → 10~35 (char는 수처럼 뺄 수 있다) static int value(char c) { return Character.isDigit(c) ? c - '0' : c - 'A' + 10; } static char checkChar(int r) { return r < 10 ? (char) ('0' + r) : (char) ('A' + r - 10); // int → char로 되돌린다 } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String line = in.readLine(); // 빈칸이 든 한 줄을 통째로 StringBuilder code = new StringBuilder(); for (char c : line.toCharArray()) if (c != ' ' && c != '-') code.append(Character.toUpperCase(c)); int total = 0; for (int j = 0; j < code.length(); j++) total += value(code.charAt(j)) * (j + 1); out.append(code).append('-').append(checkChar(total % 36)).append('\n'); } System.out.print(out); }}
C#Main.cs · 27 lines
using System;using System.Text;class Program{ // '0'~'9' → 0~9, 'A'~'Z' → 10~35 (char끼리 빼면 int) static int Value(char c) => char.IsDigit(c) ? c - '0' : c - 'A' + 10; static char CheckChar(int r) => r < 10 ? (char)('0' + r) : (char)('A' + r - 10); static void Main() { int n = int.Parse(Console.ReadLine()); var out_ = new StringBuilder(); for (int i = 0; i < n; i++) { string line = Console.ReadLine(); // 빈칸이 든 한 줄을 통째로 var code = new StringBuilder(); foreach (char c in line) if (c != ' ' && c != '-') code.Append(char.ToUpperInvariant(c)); int total = 0; for (int j = 0; j < code.Length; j++) total += Value(code[j]) * (j + 1); out_.Append(code).Append('-').Append(CheckChar(total % 36)).Append('\n'); } Console.Write(out_); }}
C++main.cpp · 27 lines
#include <cctype>#include <iostream>#include <string>// '0'~'9' → 0~9, 'A'~'Z' → 10~35int value(char c) { return std::isdigit(static_cast<unsigned char>(c)) ? c - '0' : c - 'A' + 10;}int main() { std::string line; std::getline(std::cin, line); int n = std::stoi(line); // cin >> n 뒤에 getline을 쓰면 남은 줄 끝을 빈 줄로 읽는다 std::string out; for (int i = 0; i < n; i++) { std::getline(std::cin, line); // 빈칸이 든 한 줄을 통째로 std::string code; for (char c : line) if (c != ' ' && c != '-') code += static_cast<char>(std::toupper(static_cast<unsigned char>(c))); int total = 0; for (std::size_t j = 0; j < code.size(); j++) total += value(code[j]) * static_cast<int>(j + 1); int r = total % 36; char check = static_cast<char>(r < 10 ? '0' + r : 'A' + r - 10); // int → char out += code + '-' + check + '\n'; } std::cout << out;}
Cmain.c · 26 lines
#include <ctype.h>#include <stdio.h>#include <stdlib.h>/* '0'~'9' → 0~9, 'A'~'Z' → 10~35: C의 char는 작은 정수라 그대로 뺀다 */static int value(char c) { return isdigit((unsigned char)c) ? c - '0' : c - 'A' + 10;}int main(void) { char line[128], code[128]; fgets(line, sizeof line, stdin); int n = atoi(line); /* scanf("%d")를 쓰면 줄 끝 '\n'이 남아 다음 fgets가 빈 줄을 읽는다 */ for (int i = 0; i < n; i++) { fgets(line, sizeof line, stdin); int len = 0; for (char *p = line; *p && *p != '\n'; p++) if (*p != ' ' && *p != '-') code[len++] = (char)toupper((unsigned char)*p); code[len] = '\0'; int total = 0; for (int j = 0; j < len; j++) total += value(code[j]) * (j + 1); int r = total % 36; printf("%s-%c\n", code, r < 10 ? '0' + r : 'A' + r - 10); } return 0;}
Craykonmain.cray · 39 lines
// '0'~'9' → 0~9, 'A'~'Z' → 10~35값(c){ k = Str.Code(c); if (k <= Str.Code("9")) { return k - Str.Code("0"); } return k - Str.Code("A") + 10;}확인글자(r){ if (r < 10) { return Str.FromCode(Str.Code("0") + r); } return Str.FromCode(Str.Code("A") + r - 10);}n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){ 줄 = Console.ReadLine().ToUpper(); 코드 = ""; foreach (c in 줄) { if (c != " " && c != "-") { 코드 = 코드 + c; } } 합 = 0; for (j = 0; j < 코드.Length; j++) { 합 = 합 + 값(코드[j]) * (j + 1); } Console.WriteLine(코드 + "-" + 확인글자(합 % 36));}