1.结果填空:矩阵求和

import java.math.BigInteger;
import java.util.HashSet;
public class Main{
public static void main(String[] args) {
int ct = 1;
int mp[][] = new int[102][102];
int n = 101;
int ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
mp[i][j] = ct++;
}
}
// 上面半三角
for(int i = 1; i <= (n + 1) / 2; i++) {
for(int j = (n + 1) / 2 + 1 - i; j <= (n + 1) / 2 + i - 1; j++) {
// System.out.println(mp[i][j] + " ");
ans += mp[i][j];
}
}
System.out.println(":-----");
// 下面半三角
int ss = 1;
for(int i = (n + 1) / 2 + 1; i <= n; i++) {
for(int j = 1 + ss; j <= n - ss; j++) {
// System.out.println(mp[i][j] + " ");
ans += mp[i][j];
}
ss++;
}
System.out.println(ans);
}
}
2.结果填空:素数个数
用 0,1,2,3 cdots 70,1,2,3⋯7 这 88 个数组成的所有整数中,质数有多少个(每个数字必须用到且只能用一次)。
提示:以 00 开始的数字是非法数字。
注意:每个字母只用一次,故能遍历,明显是全排列。
import java.math.BigInteger;
import java.util.HashSet;
public class Main{
public static int mp[] = new int[77543210];
public static int visit[] = new int[8];
public static int a[] = new int[8];
public static int ans = 0;
public static void main(String[] args) {
int n = 76543210;
db();
dfs(0);
System.out.println(ans);
}
public static void dfs(int ct) {
if(ct == 8) {
int s = 0;
int t = 1;
if(a[0] != 0) {
for(int i = 0; i < 8; i++) {
s = s * 10 + a[i];
}
// System.out.println(s);
if(mp[s] == 0) {
ans++;
}
}
return ;
}
for(int i = 0; i < 8; i++) {
if(visit[i] == 0) {
visit[i] = 1;
a[ct] = i;
dfs(ct + 1);
visit[i] = 0;
}
}
}
public static void db() {
mp[1] = 1;
mp[2] = 0;
for(int i = 2; i * i <= 76543210; i++) {
if(mp[i] == 0) {
for(int j = i + i; j <= 76543210; j += i) {
mp[j] = 1;
}
}
}
}
}
N! 末尾有多少个 00 呢?
N! = 1 imes 2 imes cdots imes NN!=1×2×⋯×N。
代码框中的代码是一种实现,请分析并填写缺失的代码。
因为:0 的来源就是2 * 5 , 所以有多少对2和5,就有多少个零,而且明显2的个数多余5的个数,所以简化为5 的个数了。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int ans = 0;
while (n != 0) {
ans += n / 5; n /= 5;
}
System.out.println(ans);
}
}