import sysdef convert(v, step): signed = step[0] == "i" bits = int(step[1:].rstrip("!")) size = 1 << bits # 2^bits lo = -(size // 2) if signed else 0 hi = lo + size - 1 if step.endswith("!"): # 끝값에 맞춰 담기 return max(lo, min(hi, v)) r = v % size # Python의 %는 늘 0 이상 size 미만 if r > hi: # 부호 있는 타입의 위쪽 절반은 음수 r -= size return rlines = sys.stdin.read().split("\n")q = int(lines[0])out = []changed = 0for line in lines[1:q + 1]: parts = line.split() v = int(parts[0]) row = [] for step in parts[1:]: nv = convert(v, step) # 바로 앞 단계의 결과를 다시 옮겨 담는다 if nv != v: changed += 1 v = nv row.append(str(v)) out.append(" ".join(row))out.append(str(changed))print("\n".join(out))
JavaScriptmain.js · 43 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const q = Number(lines[0]);const RANGE = { i8: [-128, 127], u8: [0, 255], i16: [-32768, 32767], u16: [0, 65535], i32: [-2147483648, 2147483647], u32: [0, 4294967295],};// 비트 연산은 먼저 값을 32비트로 자른 뒤에 한다function wrap(v, type) { switch (type) { case "i8": return (v << 24) >> 24; // 아래 8비트를 맨 위로 올렸다가 부호를 채우며 내린다 case "u8": return v & 0xff; case "i16": return (v << 16) >> 16; case "u16": return v & 0xffff; case "i32": return v | 0; default: return v >>> 0; // u32: >>>만 결과를 부호 없는 수로 준다 }}const out = [];let changed = 0;for (let i = 1; i <= q; i++) { const parts = lines[i].trim().split(" "); let v = Number(parts[0]); const row = []; for (const step of parts.slice(1)) { let nv; if (step.endsWith("!")) { const [lo, hi] = RANGE[step.slice(0, -1)]; nv = Math.max(lo, Math.min(hi, v)); } else { nv = wrap(v, step); } if (nv !== v) changed++; v = nv; row.push(v); } out.push(row.join(" "));}out.push(changed);console.log(out.join("\n"));
TypeScriptmain.ts · 47 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const q = Number(lines[0]);type IntType = "i8" | "u8" | "i16" | "u16" | "i32" | "u32";const RANGE: Record<IntType, [number, number]> = { i8: [-128, 127], u8: [0, 255], i16: [-32768, 32767], u16: [0, 65535], i32: [-2147483648, 2147483647], u32: [0, 4294967295],};// 타입 배열의 칸에 넣으면 그 칸의 크기에 맞게 잘려 들어간다const CELL: Record<IntType, Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array> = { i8: new Int8Array(1), u8: new Uint8Array(1), i16: new Int16Array(1), u16: new Uint16Array(1), i32: new Int32Array(1), u32: new Uint32Array(1),};function wrap(v: number, type: IntType): number { const cell = CELL[type]; cell[0] = v; return cell[0];}const out: string[] = [];let changed = 0;for (let i = 1; i <= q; i++) { const parts: string[] = lines[i].trim().split(" "); let v = Number(parts[0]); const row: number[] = []; for (const step of parts.slice(1)) { let nv: number; if (step.endsWith("!")) { const [lo, hi] = RANGE[step.slice(0, -1) as IntType]; nv = Math.max(lo, Math.min(hi, v)); } else { nv = wrap(v, step as IntType); } if (nv !== v) changed++; v = nv; row.push(v); } out.push(row.join(" "));}out.push(String(changed));console.log(out.join("\n"));
JavaMain.java · 44 行
import java.io.*;public class Main { // 잘라 담기: Java의 정수 캐스트는 아래쪽 비트만 남긴다 static long wrap(long v, String type) { switch (type) { case "i8": return (byte) v; case "u8": return v & 0xFF; // Java에는 부호 없는 byte가 없다 case "i16": return (short) v; case "u16": return (char) v; // char는 부호 없는 16비트 case "i32": return (int) v; default: return v & 0xFFFFFFFFL; // u32: L을 빼면 0xFFFFFFFF는 int -1이다 } } static long clamp(long v, String type) { int bits = Integer.parseInt(type.substring(1)); long lo = type.charAt(0) == 'i' ? -(1L << (bits - 1)) : 0; long hi = lo + (1L << bits) - 1; return Math.max(lo, Math.min(hi, v)); } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); int changed = 0; for (int i = 0; i < q; i++) { String[] parts = in.readLine().trim().split(" "); long v = Long.parseLong(parts[0]); for (int j = 1; j < parts.length; j++) { String step = parts[j]; long nv = step.endsWith("!") ? clamp(v, step.substring(0, step.length() - 1)) : wrap(v, step); if (nv != v) changed++; v = nv; if (j > 1) out.append(' '); out.append(v); } out.append('\n'); } out.append(changed).append('\n'); System.out.print(out); }}
C#Main.cs · 59 行
using System;using System.Text;class Program{ // 잘라 담기: unchecked 캐스트는 아래쪽 비트만 남긴다 static long Wrap(long v, string type) { unchecked { switch (type) { case "i8": return (sbyte)v; case "u8": return (byte)v; case "i16": return (short)v; case "u16": return (ushort)v; case "i32": return (int)v; default: return (uint)v; } } } static long Clamp(long v, string type) { switch (type) { case "i8": return Math.Clamp(v, sbyte.MinValue, sbyte.MaxValue); case "u8": return Math.Clamp(v, byte.MinValue, byte.MaxValue); case "i16": return Math.Clamp(v, short.MinValue, short.MaxValue); case "u16": return Math.Clamp(v, ushort.MinValue, ushort.MaxValue); case "i32": return Math.Clamp(v, int.MinValue, int.MaxValue); default: return Math.Clamp(v, uint.MinValue, uint.MaxValue); } } static void Main() { int q = int.Parse(Console.ReadLine()); var output = new StringBuilder(); int changed = 0; for (int i = 0; i < q; i++) { string[] parts = Console.ReadLine().Trim().Split(' '); long v = long.Parse(parts[0]); for (int j = 1; j < parts.Length; j++) { string step = parts[j]; long nv = step.EndsWith("!") ? Clamp(v, step.TrimEnd('!')) : Wrap(v, step); if (nv != v) changed++; v = nv; if (j > 1) output.Append(' '); output.Append(v); } output.Append('\n'); } output.Append(changed).Append('\n'); Console.Write(output); }}
C++main.cpp · 53 行
#include <algorithm>#include <cstdint>#include <iostream>#include <sstream>#include <string>// 잘라 담기: static_cast로 크기가 정해진 타입에 넣으면 아래쪽 비트만 남는다long long wrap(long long v, const std::string& type) { if (type == "i8") return static_cast<std::int8_t>(v); if (type == "u8") return static_cast<std::uint8_t>(v); if (type == "i16") return static_cast<std::int16_t>(v); if (type == "u16") return static_cast<std::uint16_t>(v); if (type == "i32") return static_cast<std::int32_t>(v); return static_cast<std::uint32_t>(v);}long long clampTo(long long v, const std::string& type) { int bits = std::stoi(type.substr(1)); long long lo = type[0] == 'i' ? -(1LL << (bits - 1)) : 0; long long hi = lo + (1LL << bits) - 1; return std::clamp(v, lo, hi);}int main() { std::string line; std::getline(std::cin, line); int q = std::stoi(line); int changed = 0; for (int i = 0; i < q; i++) { std::getline(std::cin, line); std::istringstream in(line); long long v; in >> v; std::string step; bool first = true; while (in >> step) { long long nv; if (step.back() == '!') { step.pop_back(); nv = clampTo(v, step); } else { nv = wrap(v, step); } if (nv != v) changed++; v = nv; if (!first) std::cout << ' '; std::cout << v; // long long으로 쓴다. int8_t를 그대로 쓰면 글자로 찍힌다 first = false; } std::cout << '\n'; } std::cout << changed << '\n';}
Cmain.c · 51 行
#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>/* 잘라 담기: 크기가 정해진 타입으로 캐스트하면 아래쪽 비트만 남는다 */static long long wrap(long long v, const char *type) { if (strcmp(type, "i8") == 0) return (int8_t)v; if (strcmp(type, "u8") == 0) return (uint8_t)v; if (strcmp(type, "i16") == 0) return (int16_t)v; if (strcmp(type, "u16") == 0) return (uint16_t)v; if (strcmp(type, "i32") == 0) return (int32_t)v; return (uint32_t)v;}static long long clamp(long long v, const char *type) { int bits = atoi(type + 1); long long lo = type[0] == 'i' ? -(1LL << (bits - 1)) : 0; long long hi = lo + (1LL << bits) - 1; return v < lo ? lo : v > hi ? hi : v;}int main(void) { static char line[256]; int q, changed = 0; if (!fgets(line, sizeof line, stdin)) return 0; q = atoi(line); for (int i = 0; i < q; i++) { if (!fgets(line, sizeof line, stdin)) break; char *tok = strtok(line, " \n"); long long v = atoll(tok); int first = 1; while ((tok = strtok(NULL, " \n")) != NULL) { size_t len = strlen(tok); long long nv; if (tok[len - 1] == '!') { tok[len - 1] = '\0'; nv = clamp(v, tok); } else { nv = wrap(v, tok); } if (nv != v) changed++; v = nv; printf(first ? "%lld" : " %lld", v); first = 0; } putchar('\n'); } printf("%d\n", changed); return 0;}
Craykonmain.cray · 62 行
// 2를 bits번 곱한 수둘의제곱(bits){ 결과 = 1; 循环 (k = 0; k < bits; k++) { 결과 = 결과 * 2; } 返回 결과;}옮겨담기(v, 단계){ 맞추기 = 단계.结尾是("!"); 이름 = 단계; 如果 (맞추기) { 이름 = 단계.截取(0, 단계.长度 - 1); } 크기 = 둘의제곱(이름.截取(1).转数字()); lo = 0; 如果 (이름[0] == "i") { lo = 0 - 크기 / 2; } hi = lo + 크기 - 1; 如果 (맞추기) { 返回 数学.较大值(lo, 数学.较小值(hi, v)); } r = v % 크기; // %는 왼쪽 수의 부호를 따라서 음수가 나올 수 있다 如果 (r < 0) { r += 크기; } 如果 (r > hi) { r -= 크기; } 返回 r;}q = 控制台.读行().转数字();바뀐수 = 0;循环 (i = 0; i < q; i++){ parts = 控制台.读行().去空白().分割(" "); v = parts[0].转数字(); 줄 = []; 循环 (j = 1; j < parts.数量; j++) { nv = 옮겨담기(v, parts[j]); 如果 (nv != v) { 바뀐수 += 1; } v = nv; 줄.添加(v); } 控制台.输出(줄);}控制台.输出(바뀐수);