数位数
第一行给出个数 n,第二行给出 n 个整数,以空格分隔。
对每个数输出它在十进制下有几位,每行一个。
负号(-)不算一位,0 算一位。
例如 0 → 1,-42 → 2,1000 → 4。
- 输入
- 1 ≤ n ≤ 1,000,-1,000,000,000,000,000 ≤ 数 ≤ 1,000,000,000,000,000(±10^15)
- 输出
- n 行,每行一个位数
6 0 7 -42 100 -1000 999
1 1 2 3 4 3
第一行给出个数 n,第二行给出 n 个整数,以空格分隔。
对每个数输出它在十进制下有几位,每行一个。
负号(-)不算一位,0 算一位。
例如 0 → 1,-42 → 2,1000 → 4。
6 0 7 -42 100 -1000 999
1 1 2 3 4 3
运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。
$ 就绪
请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。
用除以 10 来计数的循环必须“先数再判断”,0 才会算作一位:do { count++; x /= 10; } while (x != 0);。
像 while (x != 0) 那样先判断,0 一次也不执行,得到 0 位。
有 do-while 的语言:C、C++、Java、C#、JS、TS。
Python 和 Craykon 没有,用 while True: … if x == 0: break(Craykon 当 (真) … 跳出;)实现“至少一次”。
负数的陷阱:C、C++、Java、C# 的整数 / 和 Craykon 数学.商 向 0 截断(-42 → -4 → 0),负数也会自然到达 0;但 Python 的 // 是向下取整,-5 // 10 == -1,-1 // 10 == -1,永远停不下来——先取 abs(x)。
JS/TS 的 Math.floor(x / 10) 也有同样的问题,要用 Math.trunc。
也要小心捷径:字符串长度(str(x)、String(x).length)会把 '-' 也算进去;floor(log10(x)) + 1 对 0 不适用,而 999,999,999,999,999 的 log10 会被舍入成 15.0,得到 16。
数值超过 21 亿,C、C++、Java、C# 要用 long(long long)读入。
n = int(input())for x in map(int, input().split()): x = abs(x) # 파이썬 // 는 내림이라 -5 // 10 == -1: 음수는 0에 닿지 않는다 count = 0 # 파이썬에는 do-while이 없다: while True + break로 "적어도 한 번" while True: count += 1 x //= 10 if x == 0: break print(count)const [n, ...xs] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = xs.map((x) => { let count = 0; // do-while: 0도 한 자리로 센다(몸통이 적어도 한 번 돈다) do { count++; x = Math.trunc(x / 10); // Math.floor는 음수에서 -1에 멈춘다 } while (x !== 0); return count;});console.log(out.join("\n"));import { readFileSync } from "node:fs";const [n, ...xs]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: number[] = xs.map((x) => { let count = 0; // do-while: 0도 한 자리로 센다(몸통이 적어도 한 번 돈다) do { count++; x = Math.trunc(x / 10); // Math.floor는 음수에서 -1에 멈춘다 } while (x !== 0); return count;});console.log(out.join("\n"));import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); StringTokenizer st = new StringTokenizer(br.readLine()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { long x = Long.parseLong(st.nextToken()); int count = 0; // do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다 do { count++; x /= 10; } while (x != 0); out.append(count).append('\n'); } System.out.print(out); }}using System; class Program{ static void Main() { Console.ReadLine(); foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)) { long x = long.Parse(s); int count = 0; // do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다 do { count++; x /= 10; } while (x != 0); Console.WriteLine(count); } }}#include <iostream> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; long long x; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> x; int count = 0; // do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다 do { count++; x /= 10; } while (x != 0); std::cout << count << '\n'; }}#include <stdio.h> int main(void) { int n; long long x; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &x); int count = 0; /* do-while: 0도 한 자리로 센다. / 는 0 쪽으로 버려서 음수도 0에 닿는다 */ do { count++; x /= 10; } while (x != 0); printf("%d\n", count); } return 0;}n = 控制台.读行().转数字();수들 = 控制台.读数字();遍历 (x 在 수들){ 개수 = 0; // 크레이콘에는 do-while이 없다: 동안 (참) + 그만으로 "적어도 한 번" 当 (真) { 개수++; x = 数学.商(x, 10); // 0 쪽으로 버려서 음수도 0에 닿는다 如果 (x == 0) { 跳出; } } 控制台.输出(개수);}