ZZUILOJ1061: 顺序输出各位数字
题目描述
输入一个不大于10的9次方的正整数,从高位开始逐位分割并输出各位数字。
输入
输入一个正整数n,n是int型数据
输出
依次输出各位上的数字,每一个数字后面有一个空格,输出占一行。例如,输入 12345 ,输出 1 2 3 4 5
样例输入
12345
样例输出
1 2 3 4 5
提示
注意整数运算避免使用double类型的函数如pow()。
本题可先用一个循环计算出最高位的位权h,然后再用一个循环,循环内容为: 输出最高位(n/h)、扔掉最高位(n = n%h)、降低最高位位权(h = h/10),直到位权h为0。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n=input.nextInt();
int s=n;
int a=1;
while (s>9) {
s/=10;
a*=10;
}
while(a>0) {
System.out.printf(n/a+" ");
n%=a;
a/=10;
}
}
}
联想公司福利 1500人发布
查看11道真题和解析