唯一一次笔试全部AK的,百分之百,还有十分钟就直接交卷了,思路可能并不是最好的,但是都100%啦。
第1题:问n!得到的值中0的个数,比如7!为5040,输出为2
package 第一题;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 将所有数据记录在0-26的数组中,记录个数,这样就会将O(n^3)复杂度转化为O(26^3)
// 核心就是最终的值和字符串数组无关。 而字符串都是小写,长度26的数组就可以写出来了
// while (true){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int result = 1;
// 计算总和
for(int i = 1; i <= n; i++){
result = result * i;
}
System.out.println(result);
// 计算0的个数
int count = 0;
while (result != 0){
if(result % 10 == 0)
count++;
result = result / 10;
}
System.out.println(count);
// }
}
}
第2题:相邻字符串可以进行变化,比如a可以变成b,a还可以变成z。问给一个字符串,最少执行多少次变化,可以使得字符串都是一样的。其中字符串都是小写字符。
package 第二题;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// while (true){
// 将所有数据记录在0-26的数组中,记录个数
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int count =