题目:
正序分解正整数
输入:123
输出:1 2 3
代码如下
import java.util.Scanner;
/*
正序分解正整数
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int count = 1;
int numberChar = number;
int mode = 1;
while (numberChar>9)//大于9才能分解
{
numberChar = numberChar/10;//去掉个位数并且减位 123-->12————12-->1
mode = mode*10;//叠加倍数 1-->10————10-->100
count++;//计算位数 1-->2————2-->3
}
for (int i = 0;i<count;i++)
{
int digit = number / mode;//存储去掉首位的数值 123-->1
number = number % mode;//取余去掉首位并减低位数 123%100-->23————23%10-->3
mode = mode/10;//减低倍数 100-->10————10-->1
System.out.print(digit+" ");
}
}
}
123
1 2 3
进程已结束,退出代码0