枚举1-2020中的每一位数含2的个数
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int res = 0;
for(int i = 1 ;i <= 2020;i++) {
res += cal(i);
}
System.out.println(res);
}
//枚举1-2020中的每一个数字的每一位
public static int cal(int x) {
int res = 0;
while(x > 0 ) {
int k = x % 10;
if(k == 2) res++;
x/=10;
}
return res; //624
}
}
分别枚举行、列、对角线中‘2020’的个数即可
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\eclipse_workspace\\蓝桥杯\\src\\蓝桥杯112\\test.txt")));
char[][] ch = new char[300][300];
for(int i = 0 ;i<300;i++) {
ch[i] = br.readLine().toCharArray();
}
int count = 0;
//枚举行
for(int i = 0;i<ch.length-3;i++) {
for(int j = 0;j<ch[i].length;j++) {
if(ch[i][j]=='2'&&ch[i+1][j]=='0'&&ch[i+2][j]=='2'&&ch[i+3][j]=='0') {
count++;
}
}
}
//枚举列
for(int i = 0;i<ch.length;i++) {
for(int j = 0;j<ch[i].length-3;j++) {
if(ch[i][j]=='2'&&ch[i][j+1]=='0'&&ch[i][j+2]=='2'&&ch[i][j+3]=='0') {
count++;
}
}
}
//枚举对角线
for(int i = 0;i<ch.length-3;i++) {
for(int j = 0;j<ch[i].length-3;j++) {
if(ch[i][j]=='2'&&ch[i+1][j+1]=='0'&&ch[i+2][j+2]=='2'&&ch[i+3][j+3]=='0') {
count++;
}
}
}
System.out.println(count);
}
}
将横纵坐标之和作为开始,奇数为从右上到左下,偶数从坐下到右上方。
public class Main{
public static void main(String[] args) {
int a[][] = new int[81][81];
int res = 1;
// i为横纵坐标之和
for (int i = 2; i <= 80 * 2; i++) {
if (i % 2 == 1) {
for (int j = 1; j < i; j++) {
if (j >= 1 && j <= 80 && i - j <= 80 && i - j >= 1) {
a[j][i - j] = res++;
}
}
} else {
for (int j = i - 1; j >= 1; j--) {
if (j >= 1 && j <= 80 && i - j >= 1 && i - j <= 80) {
a[j][i - j] = res++;
}
}
}
}
System.out.println(a[20][20]);/761
}
}
80
奈何本人智商有限
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int i = 0;i < n ;i++)
a[i] =in.nextInt();
int max = -1, min = 101,sum = 0;
float avg = 0.00f;
for(int i = 0;i < n;i++) {
max =Math.max(max,a[i]);
min =Math.min(min, a[i]);
sum+=a[i];
}
System.out.println(max);
System.out.println(min);
System.out.printf("%.2f",sum * 1.0/n);
}
}
用数组计数0-25,最后取其中最大的数即可
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String string = in.nextLine();
int a[] = new int[26];
char[] ch = string.toCharArray();
for(int i = 0;i<ch.length;i++) {
a[ch[i]-'a']++;
}
int idx=-1, max =0;
for(int i = 0;i<=25;i++) {
if(a[i]>max) {
idx = i;
max = Math.max(max, a[i]);
}
}
System.out.println((char)('a'+idx));
System.out.println(max);
}
}
先通过dp求得最后一行的值,再根据奇偶性判断.
当为奇数行时,相当于走偶数次,向左向右走的步数步数差为0,1所以输出dp[n][n/2]
当为偶数行时,相当于走奇数行,左右步数差的绝对值为一,则需要判断dp[n][n/2和dp[n][n/2+1],;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int f[][] = new int[110][110];
for(int i =1;i<=n;i++) {
for(int j = 1;j<=i;j++) {
f[i][j] = in.nextInt();
}
}
int dp[][] = new int[110][110];
int l = 0,r = 0;
for(int i = 1 ;i <=n;i++) {
for(int j = 1;j<=i;j++) {
if(j==1)
dp[i][j] = dp[i-1][j]+f[i][j];
else {
dp[i][j] = Math.max(dp[i-1][j], dp[i -1][j -1])+f[i][j];
}
}
}
if(n % 2== 1) {
System.out.println(dp[n][n/2 +1]);
}else {
System.out.println(Math.max(dp[n][n/2],dp[n][n/2+1]));
}
}
}
分割字符串,用hashset存储每个字串中的字符个数,将子串中的字符个数相加即可.时间复杂度高,请各位大佬指点指点
import java.util.HashSet;
import java.util.Scanner;
public class Main{
static String string = null;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in = new Scanner(System.in);
string = in.nextLine();
int len =string.length();
int res = 0;
for(int i = 0 ;i <len;i++){
for(int j=0;j<len;j++) {
res+=cal(i,j);
}
}
System.out.println(res);
}
static int cal(int i, int j) {
int res = 0;
HashSet<Character> set = new HashSet<Character>();
for( ;i<=j;i++) {
if(!set.contains(string.charAt(i))) {
set.add(string.charAt(i));
res++;
}
}
return res;
}
}
放弃
如有错误请各位大佬指点。