题目解析:题目的意思是从N开始,最少需要累加几步可以变成指定的数字M,每次累加的值为当前值的一个约数。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] step = new int[m + 1];
for (int i = 0; i < m + 1; i++) {
step[i] = Integer.MAX_VALUE;
}
step[n] = 0;
for (int i = n; i < m; i++) {
if (step[i] == Integer.MAX_VALUE) {
continue;
}
List<Integer> list = div(i);
// j 代表一次可以跳几块石板
// i 代表当前石板的编号
for (int j: list) {
if (i + j <= m && step[i + j] != Integer.MAX_VALUE) {
step[i +j] = Math.min(step[i + j], step[i] + 1);
} else if (i + j <= m) {
step[i + j] = step[i] + 1;
}
}
}
if (step[m] == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(step[m]);
}
}
public static List<Integer> div(int num){
List<Integer> list = new ArrayList<Integer>();
for (int i = 2; i * i <= num ; i++) {
if (num % i == 0) {
list.add(i);
if (num / i != i) {
list.add(num / i);
}
}
}
return list;
}
}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
// 参数个数
int count = 0;
for (int i = 0; i < str.length(); i ++) {
// 遇到双引号的时候,要一直遍历,直到碰到第二个双引号
// 才说明双引号中的参数遍历完了
if (str.charAt(i) == '"') {
do {
i ++;
} while (str.charAt(i) != '"');
}
// 碰到双引号以外的空格 count++
if (str.charAt(i) == ' ') {
count ++;
}
}
// 参数的总个数 = 空格个数 + 1
System.out.println(count + 1);
int flag = 1;
for (int i = 0; i < str.length(); i ++) {
// 当碰到第一个双引号flag变为0
// 当碰到第二个双引号flag变为1
// 说明在flag == 0的时候,我们一直在遍历双引号中的参数
if (str.charAt(i) == '"') {
flag ^= 1;
}
// 除了双引号中的空格和双引号,其他字符都输出
if (str.charAt(i) != ' ' && str.charAt(i) != '"') {
System.out.print(str.charAt(i));
}
// 双引号里面的空格需要输出
if (str.charAt(i) == ' ' && flag == 0) {
System.out.print(str.charAt(i));
}
// 碰到双引号以外的空格需要换行
if (str.charAt(i) == ' ' && flag == 1) {
System.out.println(str.charAt(i));
}
}
}
}