自学内容网 自学内容网

【ZZULIOJ】1061: 顺序输出各位数字(Java)

目录

题目描述

输入

输出

样例输入 Copy

样例输出 Copy

提示

code


题目描述

 输入一个不大于10的9次方的正整数,从高位开始逐位分割并输出各位数字。 

输入

 输入一个正整数n,n是int型数据 

输出

依次输出各位上的数字,每一个数字后面有一个空格,输出占一行。例如,输入 12345 ,输出 1 2 3 4 5 

样例输入 Copy

12345

样例输出 Copy

1 2 3 4 5

提示

注意整数运算避免使用double类型的函数如pow()。
本题可先用一个循环计算出最高位的位权h,然后再用一个循环,循环内容为: 输出最高位(n/h)、扔掉最高位(n = n%h)、降低最高位位权(h =  h/10),直到位权h为0。

code

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
int x = (int)Math.log10(n)+1, h = (int)Math.pow(10, x);
while(h/10 > 0) {
h /= 10;
System.out.printf("%d ", n/h);
n %= h;
}
}
}


原文地址:https://blog.csdn.net/m0_64604482/article/details/137696921

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!