文章目录
A:门牌制作(5分)
import java.util.*;
public class Main {
public static void main(String args[]) {
int res = 0;
for (int i = 1; i <= 2020; i ++ ) {
int x = i;
while (x != 0) {
if (x % 10 == 2)
res ++ ;
x /= 10;
}
}
System.out.println(res);
}
}
正确答案: 624 ! \color{Red}{624!} 624!
B:寻找2020(5分)
正确答案:
624
!
\color{Red}{624!}
624!
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String[][] str = new String[300][];
int res = 0;
for (int i = 0; i < 300; i ++ ) {
String[] s = br.readLine().split("");
str[i] = s;
}
for (int i = 0; i < 300; i ++ ) {
for (int j = 0; j < str[i].length; j ++ ) {
String ss = "";
/*看同一行*/
if (j + 3 < str[i].length) ss = str[i][j] + str[i][j + 1] + str[i][j + 2] + str[i][j + 3];
if (check(ss))
res ++ ;
/*看同一列*/
ss = "";
if (i + 3 < 300) ss = str[i][j] + str[i + 1][j] + str[i + 2][j] + str[i + 3][j];
if (check(ss))
res ++ ;
/*看同一斜线*/
ss = "";
if (i + 3 < 300 && j + 3 < str[i].length)
ss = str[i][j] + str[i + 1][j + 1] + str[i + 2][j + 2] + str[i + 3][j + 3];
if (check(ss))
res ++ ;
}
}
System.out.println(res);
}
public static boolean check(String str) {
if (str.equals("2020"))
return true;
return false;
}
}
正确答案: 16520 ! \color{Red}{16520!} 16520!
C:蛇形填数(10分)
import java.util.*;
public class Main {
public static void main(String args[]) {
int[][] g = new int[150][150];
boolean flag = true;
int cnt = 1;
int i = 1, j = 1;
while (true)
{
if (i == 20 && j == 20)
break;
g[i][j] = cnt ++ ;
if (flag && i - 1 == 0) {
flag = false;
j ++ ;
}
else if (!flag && j - 1 == 0) {
flag = true;
i ++ ;
}
else {
if (flag) {
i -- ;
j ++ ;
}
else {
i ++ ;
j -- ;
}
}
}
System.out.println(cnt);
}
}
正确答案: 761 ! \color{Red}{761!} 761!
D:七段码(10分)
import java.util.*;
public class Main {
static int[][] e = new int[8][8];
static int res = 0;
static int p[] = new int[15];
static int light[] = new int[10];
public static void main(String args[]) {
init();
dfs(1);
System.out.println(res);
}
public static void init() {
e[1][2] = e[1][6] = 1;
e[2][1] = e[2][7] = e[2][3] = 1;
e[3][2] = e[3][4] = e[3][7] = 1;
e[4][3] = e[4][5] = 1;
e[5][4] = e[5][6] = e[5][7] = 1;
e[6][1] = e[6][5] = e[6][7] = 1;
e[7][2] = e[7][3] = e[7][5] = e[7][6] = 1;
}
public static int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
public static void dfs(int u) {
if (u > 7) {
for (int i = 1; i <= 7; i ++ ) p[i] = i;
for (int i = 1; i <= 7; i ++ ) {
for (int j = 1; j <= 7; j ++ ) {
if (e[i][j] == 1 && light[i] == 1 && light[j] == 1) {
int a = find(i), b = find(j);
if (a != b) p[a] = b;
}
}
}
int k = 0;
for (int i = 1; i <= 7; i ++ )
if (light[i] == 1 && p[i] == i)
k ++ ;
if (k == 1) res ++ ;
return;
}
light[u] = 1;
dfs(u + 1);
light[u] = 0;
dfs(u + 1);
}
}
正确答案: 80 ! \color{Red}{80!} 80!
E:排序(15分)
!
要想使得字符串长度最小,则交换次数一定最多。
而交换次数最多,则字符串一定是逆序的~
OK~,那我们浅浅的算上一波,算一下大概需要多长的字符串,根据
n
∗
(
n
−
1
)
2
=
100
\frac{n*(n-1)}{2}=100
2n∗(n−1)=100。
算出当
n
=
14
n=14
n=14的时候,
n
∗
(
n
−
1
)
2
=
91
\frac{n*(n-1)}{2}=91
2n∗(n−1)=91,而当
n
=
15
n=15
n=15的时候,
n
∗
(
n
−
1
)
2
=
105
\frac{n*(n-1)}{2}=105
2n∗(n−1)=105!
如此的话当
n
=
15
n=15
n=15(长度15)的逆序字符串为:
o
n
m
l
k
j
i
h
g
f
e
d
c
b
a
onmlkjihgfedcba
onmlkjihgfedcba
我们要字典序最短的字符串,并且要将105的交换次数降低到100,所以我们可以如下图这样子做:
import java.util.*;
public class Main {
public static void main(String args[]) {
System.out.println("jonmlkihgfedcba");
}
}
F:成绩分析(15分)
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[] a = new int[10010];
int n = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i ++ ) {
a[i] = sc.nextInt();
sum += a[i];
}
Arrays.sort(a, 0, n);
System.out.println(a[n - 1]);
System.out.println(a[0]);
System.out.format("%.2f\n", sum * 1.0 / n);
}
}
G:单词分析(20分)
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int[] st = new int[30];
String str = br.readLine();
for (int i = 0; i < str.length(); i ++ ) {
char c = str.charAt(i);
st[c - 'a'] ++ ;
}
int x = 0, max = -1;
for (int i = 0; i < 25; i ++ )
if (max < st[i])
{
x = i;
max = st[i];
}
System.out.println((char)(x + 'a'));
System.out.println(max);
}
}
H:数字三角形(20分)
import java.util.*;
import java.io.*;
public class Main {
static int[][] g = new int[110][110];
public static void main(String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int n = Integer.parseInt(br.readLine());
for (int i = 1; i <= n; i ++ ) {
String[] s = br.readLine().split(" ");
for (int j = 1; j <= i; j ++ )
g[i][j] = Integer.parseInt(s[j - 1]);
}
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
g[i][j] += Math.max(g[i - 1][j], g[i - 1][j - 1]);
if (n % 2 == 1)
System.out.println(g[n][n / 2 + 1]);
else
System.out.println(Math.max(g[n][n / 2], g[n][n / 2 + 1]));
}
}
I:字串分值和(25分)
import java.util.*;
import java.io.*;
public class Main {
static int[][] g = new int[110][110];
public static void main(String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
int res = 0;
int cnt = 0;
for (int i = 0; i < str.length(); i ++ ) {
int left = 0;
int right = 0;
char c = str.charAt(i);
for (int j = i - 1; j >= 0 && str.charAt(j) != c; j -- )
left ++ ;
for (int j = i + 1; j < str.length() && str.charAt(j) != c; j ++ )
right ++ ;
res += (right + 1) * (left + 1);
}
System.out.println(res);
}
}
J:装饰珠(25分)