总链接:https://www.jisuanke.com/contest/1215
A:矩阵求和
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class A {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
int n = nextInt();
int sum = 0, c = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
c++;
if (Math.abs(n / 2 - i) + Math.abs(n / 2 - j) <= n / 2) {
sum += c;
}
}
}
out.println(sum);
out.flush();
}
}
B:素数个数
全排列、0->7 如果前面的不是0、就判断是不是素数、统计输出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class B {
static boolean vis[] = new boolean[9]; // 0 -> 8
static int a[] = new int[10]; // 0 -> 8
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static long sum = 0;
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) {
sum = 0;
dfs(0);
out.println(sum);
out.flush();
}
private static void dfs(int s) {
if (s == 8) {
if(a[0] == 0){
return ;
}else{
String str = "";
for(int i = 0; i < 8; i++){
str += a[i];
}
int num = Integer.parseInt(str);
if(judge(num)){
sum++;
}
}
display();
}
for (int i = 0; i < 8; i++) {
if (!vis[i]) {
vis[i] = true;
a[s] = i;
dfs(s + 1);
vis[i] = false;
}
}
}
public static boolean judge(int num) {
for (int i = 2; i <= Math.sqrt(num); i++) {
if(num % i == 0){ // 不是素数、、
return false;
}
}
return true;
}
public static void display() {
for (int i = 0; i < 8; i++) {
out.print(a[i] + " ");
}
out.println();
out.flush();
}
}
c:连连看
dfs求解边界的点、(类似于全排列)枚举出所有的情况、然后不断的取最大的结果
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class C {
static int fd[][] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; //设置上下左右四个方向、、、
static boolean vis[][] = new boolean[21][21];
static int a[][] = new int[21][21];
static int n, ans;
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
n = nextInt();
ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = nextInt();
}
}
dfs(1, 0);
out.println(ans);
out.flush();
}
private static boolean judge(int x, int y) {
if (vis[x][y]) {
return false;
}
for (int i = 0; i < 4; i++) {
int xx = x + fd[i][0];
int yy = y + fd[i][1];
if (xx < 0 || xx >= n|| yy < 0 || yy >= n) { //在边界、、、
return true;
}
if (vis[xx][yy]) { // 使用过、、、
return true;
}
}
return false;
}
private static void dfs(int step, int s) {
ans = Math.max(ans, s);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(judge(i, j)){ // 是边界、 可以消除、、、
for(int ii = 0; ii < n; ii++){
for(int jj = 0; jj < n; jj++){
if(ii == i && jj == j){
continue;
}
if(a[i][j] == a[ii][jj] && judge(ii, jj)){
vis[i][j] = vis[ii][jj] = true;
dfs(step + 1, s + step * a[i][j]);
vis[i][j] = vis[ii][jj]= false;
}
}
}
}
}
}
}
}
D:快速幂、
快速幂的基础知识、不懂得就去百度一下吧、
E:末尾的0的个数
要想有0、那就必须有10、既然有10、那一定有5、2、而2又比5多、所以有0个个数也就是有5的数量
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;
public class E {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int tmp = n;
int ans = 0;
while (n != 0) {
ans += (n /= 5);
}
System.out.println(ans);
long sum = 1;
for(int i = 1; i <= tmp; i++){
sum = sum * i;
}
System.out.println(sum);
}
}
F:藏宝图
(待续)
G:合并数字
用堆栈来维护、每次合并的个数、如果要新加入的小于栈顶数1、那栈顶弹出、大于栈顶的话、那就继续输入、否则就压入栈中、最终输出次数
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Stack;
public class G {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
int n, cnt = 0, x;
Stack<Integer> stack = new Stack<>();
n = nextInt();
for(int i = 0; i < n; i++){
x = nextInt();
while(!stack.isEmpty() && stack.peek() - x == 1){ // 栈顶元素比这个大、删除、 不比这个大就结束循环、
stack.pop();
cnt++;
}
if(!stack.isEmpty() && x - stack.peek() == 1){ // 这个数比当前栈顶的大、而且恰好大 1
cnt++;
}else{
stack.push(x); // 扔进去称为栈顶、、、
}
}
out.println(cnt);
out.flush();
}
}
H:蒜头君下棋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class H {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int n, m;
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
n = nextInt();
m = nextInt();
solve();
}
private static void solve() {
if (n < m) { // 保证 n是最大的、、然后我们来对小的进行处理、、、
int tmp = n;
n = m;
m = tmp;
}
if(m == 1){
out.println(n);
}else if(m == 2){
out.println(n / 4 * 4 + Math.min(n % 4, 2) * 2);
}else{
out.println((n * m + 1) / 2);
}
out.flush();
}
}
I:蒜头君的数轴
考虑两对相邻点距离怎么由不同的通过加点变为全部相同, 也就是对线段进行分割, 分割成相同长度的若干段, 这个长度最大也就是原来两条线段长度的最大公约数, 我们先对点排好序然后做差,得到相邻点距离,因为最多允许有一段距离跟其它不同,可以枚举不同的是哪一段, 对其它段求最大公约数。
怎么快速求出这个值, 可以运用前缀和的思路, 预处理出前缀
gcd和后缀 gcd, 那么其它段的 gcd就可以通过这两个 gcd再求一遍 gcd得到。 得到分割后的距离后, 每一段需要加的点就可以算了, 但是如果每一次都去看每一段要加多少点还是太慢了, 我们可以预处理得到最左点和最右点的距高, 减去枚举中的那一 段, 再除以最大公约数得到一共会被分成多少小段, 需要加的点数就是这个数字减去原来有多少段,也就是减去 n-2。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class I {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static final int MAXN = 100005;
static int dist[] = new int[MAXN];
static int gcd1[] = new int[MAXN];
static int gcd2[] = new int[MAXN];
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
/*
只有一对点与其他的不同的话、就是最左边或者最右边的俩个点、、、
*/
public static void main(String[] args) throws IOException {
int n = nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
Arrays.sort(a); // 排序、、
out.println(solve(a));
out.flush();
}
public static int gcd(int a, int b) { // 求最大公因数、、
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static int solve(int[] a) {
int len = a.length;
if (len <= 3) {
return 0;
}
int sum = 0;
for (int i = 0; i < len - 1; i++) {
dist[i] = a[i + 1] - a[i];
sum += dist[i];
}
gcd1[0] = dist[0];
for (int i = 1; i < len - 1; i++) {
gcd1[i] = gcd(gcd1[i - 1], dist[i]);
}
gcd2[len - 2] = dist[len - 2];
for (int i = len - 3; i >= 0; i--) {
gcd2[i] = gcd(gcd2[i + 1], dist[i]);
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < len - 1; i++) {
int d;
if (i == 0) {
d = gcd2[i + 1];
} else if (i == len - 2) {
d = gcd1[i - 1];
} else {
d = gcd(gcd1[i - 1], gcd2[i + 1]);
}
ans = Math.min(ans, (sum - dist[i]) / d - len + 2);
}
return ans;
}
}
J:划分整数
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class J {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static final int MAXN = 305;
static long dp[][] = new long[MAXN][MAXN];
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
public static void main(String[] args) throws IOException {
int n, k;
n = nextInt();
k = nextInt();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
if(i == 1 || j == 1){
dp[i][j] = 1;
}else if(j > i){
dp[i][j] = dp[i][i];
}else if(i == j){
dp[i][j] = dp[i][j - 1] + 1;
}else{
dp[i][j] = dp[i][j - 1] + dp[i - j][j];
}
}
}
out.println(dp[n][k]);
out.flush();
}
}