题目解析
找到最长的持续性,并且找到最长持续性的所有数
解题思路
按照题目意思
首先将这个数拆分成各位之积,直到各位之积为个位数。
统计操作的步数。
然后统计最大的步数,输出最大步数的所有数。
代码
import java.io.*;
import java.util.*;
public class Main
{
// 将x拆分位各位之积
static int fen(int x)
{
int ans = 1;
while (x > 0)
{
ans *= x % 10;
x /= 10;
}
return ans;
}
// 获取x到一个个位需要几步
static int get(int x)
{
int cnt = 0;
while (x >= 10)
{
x = fen(x);
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int a = sc.nextInt();
int b = sc.nextInt();
int max = 0; // 统计最大值
ArrayList<Integer> ar = new ArrayList<Integer>();
for (int i = a; i <= b; i++)
{
int j = get(i);
// 如果比最大值大
if (j > max)
{
// 替换持续性
max = j;
ar = new ArrayList<Integer>();
}
// 如果小于最大值,则不添加这个数
else if (j < max)
continue;
// 添加当前这个数
ar.add(i);
}
out.println(max);
for (int i = 0; i < ar.size(); i++)
{
if (i != 0)
out.print(" ");
out.print(ar.get(i));
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}