m = int(input())msg = input()# len은 바이트가 아니라 글자(코드 포인트)를 센다: len("안녕") == 2print(m - len(msg))
JavaScriptmain.js · 5 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const m = Number(lines[0]);const msg = lines[1];// length는 UTF-16 단위의 수다. 한글 한 글자는 한 단위라 글자 수와 같다console.log(m - msg.length);
TypeScriptmain.ts · 6 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const m: number = Number(lines[0]);const msg: string = lines[1];// length는 UTF-16 단위의 수다. 한글 한 글자는 한 단위라 글자 수와 같다console.log(m - msg.length);
JavaMain.java · 13 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.nio.charset.StandardCharsets;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); int m = Integer.parseInt(br.readLine().trim()); String msg = br.readLine(); // length()는 UTF-16 단위의 수다. 한글 한 글자는 한 단위라 글자 수와 같다 System.out.println(m - msg.length()); }}
C#Main.cs · 12 行
using System;class Program{ static void Main() { int m = int.Parse(Console.ReadLine()); string msg = Console.ReadLine(); // Length는 UTF-16 단위의 수다. 한글 한 글자는 한 단위라 글자 수와 같다 Console.WriteLine(m - msg.Length); }}
C++main.cpp · 18 行
#include <iostream>#include <limits>#include <string>int main() { int m; std::cin >> m; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::string msg; std::getline(std::cin, msg); // size()는 바이트 수를 준다. UTF-8에서 글자의 첫 바이트만 세면 글자 수다 // (둘째 바이트부터는 모두 10xxxxxx 꼴이다) int count = 0; // size_t가 아니라 int: m - count가 음수가 될 수 있다 for (unsigned char c : msg) { if ((c & 0xC0) != 0x80) count++; } std::cout << m - count << "\n";}
Cmain.c · 17 行
#include <stdio.h>#include <stdlib.h>int main(void) { char num[16], msg[700]; /* 한글 한 글자가 3바이트라 200글자면 600바이트까지 */ if (!fgets(num, sizeof num, stdin)) return 0; if (!fgets(msg, sizeof msg, stdin)) return 0; int m = atoi(num); int count = 0; /* strlen은 바이트 수를 준다. UTF-8에서 글자의 첫 바이트만 세면 글자 수다 (둘째 바이트부터는 모두 10xxxxxx 꼴이다) */ for (int i = 0; msg[i] != '\0' && msg[i] != '\n'; i++) { if (((unsigned char)msg[i] & 0xC0) != 0x80) count++; } printf("%d\n", m - count); return 0;}
Craykonmain.cray · 4 行
m = コンソール.行読み().数に();msg = コンソール.行読み();// 길이는 글자 수다: "안녕".길이 == 2コンソール.書く(m - msg.長さ);