1.门牌制作
解题思路:
简单模拟题。
枚举数字 1∼2020,把每个数按十进制分解,再统计 2 出现的次数即可。
也可以将每个数字都转换为字符串,然后直接统计这些字符串总共有多少个字符 2。
最后的答案为 624。
public class Main {
public static void main(String[] args) {
int ans = 0;
for(int i = 1 ; i <= 2020 ; i ++) {
int x = i;
while(x != 0) {
if(x % 10 == 2) {
ans ++;
}
x /= 10;
}
}
System.out.println(ans);
}
}
2.寻找2020
import java.util.*;
public class Main {
public static int ans;
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
String[] s = new String[310];
for(int i = 1 ; i <= 300 ; i ++) {
s[i] = cin.nextLine();
}
for(int i = 1 ; i < s.length; i ++) {
for(int j = 0 ; j < s[i].length() ; j ++) {
if(s[i].charAt(j) == '2') {
if(j + 3 < s[i].length() && s[i].charAt(j + 1) == '0' && s[i].charAt(j + 2) == '2' && s[i].charAt(j + 3) == '0') ans ++ ;
if(i + 3 < s.length && s[i + 1].charAt(j) == '0' && s[i + 2].charAt(j) == '2' && s[i + 3].charAt(j) == '0') ans ++ ;
if(j + 3 < s[i].length() && i + 3 < s.length && s[i + 1].charAt(j + 1) == '0' && s[i + 2].charAt(j + 2) == '2' && s[i + 3].charAt(j + 3) == '0') ans ++;
}
}
}
System.out.println(ans);
cin.close();
}
}
3.蛇形填数
解题思路
定义 r 表示当前位置所在行,c 表示当前位置所在列,ans 记录当前位置的数值。
那么下一个位置将会有以下六种情况:
r=1,c 为奇数:那么下一步将会到达的位置为 (r,c+1)。
r=1,c 为偶数:那么下一步将会到达的位置为 (r+1,c−1)。
c=1,r 为偶数,那么下一步将会到达的位置为 (r+1,c)。
c=1,r 为奇数且 r≠1,那么下一步将会到达的位置为 (r−1,c+1)。
r≠1,c≠1,r+c 为奇数,那么下一步将会到达的位置为 (r+1,c−1)。
r≠1,c≠1,r+c为偶数,那么下一步将会到达的位置为 (r−1,c+1)。
按照这 6 种情况模拟,每走一步 ans 就加 1,直到 r=20,c=20 时结束模拟输出 ans。
最后的答案为 761。
import java.util.*;
public class Main {
public static void main(String[] args) {
int r = 1 , c = 1 , ans = 1;
while(r != 20 || c != 20){
if(r == 1){
if(c % 2 == 1) c ++ ;
else {
r ++ ; c --;
}
}
else if(c == 1){
if(r % 2 == 0) r ++;
else {
r -- ; c ++ ;
}
}
else if((r + c) % 2 == 1) {
r ++ ; c -- ;
}
else {
r -- ; c ++ ;
}
ans ++ ;
}
System.out.println(ans);
}
}
4.七段码
import java.util.*;
public class Main {
private static int ans;
private static int[][] g = new int[7][7];
private static boolean[] vis = new boolean[7];
private static boolean[] flag = new boolean[7];
private static void bfs(int x) {
LinkedList<Integer> que = new LinkedList<Integer>();
que.offer(x);
vis[x] = true;
while (!que.isEmpty()) {
int u = que.peek();
que.poll();
for (int i = 0; i <= 6; i++) {
if (g[u][i] != 0 && flag[i] && !vis[i]) {
vis[i] = true;
que.offer(i);
}
}
}
}
private static boolean check(int x) {
for (int i = 0; i <= 6; i++) {
flag[i] = vis[i] = false;
}
int cnt = 0;
for (int i = 6; i >= 0; i--) {
if ((x >> i & 1) != 0) {
flag[i] = true;
}
}
for (int i = 0; i <= 6; i++) {
if (flag[i] && !vis[i]) {
bfs(i);
cnt++;
}
}
return cnt == 1;
}
public static void main(String[] args) {
g[0][1] = g[0][5] = 1;
g[1][0] = g[1][2] = g[1][6] = 1;
g[2][1] = g[2][3] = g[2][6] = 1;
g[3][2] = g[3][4] = 1;
g[4][3] = g[4][5] = g[4][6] = 1;
g[5][0] = g[5][4] = g[5][6] = 1;
g[6][1] = g[6][2] = g[6][4] = g[6][5] = 1;
for (int i = 0; i < (1 << 7); i++) {
if (check(i)) {
ans++;
}
}
System.out.print(ans);
System.out.print('\n');
}
}
5.排序
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("jonmlkihgfedcba");
}
}
6.成绩分析
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int mi = 101 , ma = 0 , sum = 0;
for(int i = 1 ; i <= n ; i ++){
int x ;
x = cin.nextInt();
mi = Math.min(mi , x);
ma = Math.max(ma , x);
sum += x;
}
System.out.println(ma);
System.out.println(mi);
System.out.printf("%.2f\n" , 1.0 * sum / n);
cin.close();
}
}
7.单词分析
import java.util.*;
public class Main {
private static int[] cnt = new int[30];
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String s = cin.next();
int ch = 0 , ma = 0;
for (int i = 0 ; i < s.length() ; i ++) {
cnt[s.charAt(i) - 'a']++;
}
for (int j = 0; j < 26; j++) {
if (ma < cnt[j]) {
ma = cnt[j];
ch = j;
}
}
System.out.println((char) (ch + 'a'));
System.out.println(ma);
}
}
8.数字三角形
import java.util.*;
public class Main {
private static int[][] a = new int[110][110];
private static int[][] dp = new int[110][110];
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
a[i][j] = cin.nextInt();
}
}
for(int i = 0 ; i <= n ; i ++) for(int j = 0 ; j <= n ; j ++) dp[i][j] = -100000000;
dp[1][1] = a[1][1];
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
dp[i][j] = Math.max(dp[i - 1][j - 1], dp[i - 1][j]) + a[i][j];
}
}
if (((n - 1) & 1) != 0) {
System.out.println(Math.max(dp[n][1 + (n - 1) / 2], dp[n][1 + (n - 1) / 2 + 1]));
} else {
System.out.println(dp[n][1 + (n - 1) / 2]);
}
}
}
9.字串分值和
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
String s = cin.nextLine();
int[] suf = new int[s.length() + 1];
int[] last = new int[30];
long ans = 0;
Arrays.fill(last , s.length());
for(int i = s.length() - 1 ; i >= 0 ; i --){
int x = s.charAt(i) - 'a';
suf[i] = last[x];
last[x] = i;
}
for(int i = 0 ; i < s.length() ; i ++){
ans += (long)1 * (i + 1) * (suf[i] - i);
}
System.out.println(ans);
}
}
10.装饰珠
import java.io.*;
import java.util.*;
class InputReader{
private final static int BUF_SZ = 65536;
BufferedReader in;
StringTokenizer tokenizer;
public InputReader(InputStream in) {
super();
this.in = new BufferedReader(new InputStreamReader(in),BUF_SZ);
tokenizer = new StringTokenizer("");
}
private String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
public class Main {
private static int m;
private static int x;
private static int ans;
private static int[] sum = new int[5];
private static int[] dp = new int[310];
private static Vector<Vector<Integer>> vec1 = new Vector<Vector<Integer>>();
private static Vector<Vector<Integer>> vec2 = new Vector<Vector<Integer>>();
private static Vector<Vector<Integer>> vec3 = new Vector<Vector<Integer>>();
private static Vector<Vector<Integer>> vec4 = new Vector<Vector<Integer>>();
private static Vector<Vector<Integer>> vec = null;
private static int calc() {
int V = 0;
for (int l = 4; l >= 1; l--) {
V += sum[l];
vec = null;
if(l == 1) vec = vec1;
else if(l == 2) vec = vec2;
else if(l == 3) vec = vec3;
else if(l == 4) vec = vec4;
for (Vector<Integer> i : vec) {
for (int k = V; k >= 0; k--) {
for (int j = 0; j < i.size(); j++) {
int w = j + 1;
int v = i.get(j);
if (k - w >= 0) {
dp[k] = Math.max(dp[k], dp[k - w] + v);
}
else break;
}
}
}
}
for (int i = 0; i <= V; i++) {
ans = Math.max(ans, dp[i]);
}
return ans;
}
public static void main(String[] args) {
InputReader cin = new InputReader(System.in);
for (int i = 1; i <= 6; i++) {
int n = cin.nextInt();
for (int j = 1; j <= n; j++) {
x = cin.nextInt();
sum[x]++;
}
}
m = cin.nextInt();
for (int i = 1; i <= m; i++) {
int l = cin.nextInt(), p = cin.nextInt();
Vector<Integer> b = new Vector<Integer>();
for (int j = 1; j <= p; j++) {
x = cin.nextInt();
b.add(x);
}
if(l == 1) vec1.add(b);
else if(l == 2) vec2.add(b);
else if(l == 3) vec3.add(b);
else if(l == 4) vec4.add(b);
}
System.out.println(calc());
}
}