这题,感觉还行。
我的答案:624
public class Main1 {
public static void main(String[] args) {
int n = 2020, sum = 0;
for (int i = 1; i <= 1000; i++) {
for (int j = 0, k = 1; j < String.valueOf(i).length(); j++, k *= 10) {
int m = i % (k * 10) / k;
if (m == 2) {
sum ++;
}
}
}
System.out.println(sum);
}
}
这题就是录入数据的时候有点麻烦,思路挺清晰的,先一行一行找,再一列一列找,最后找斜线的,就是注意控制下标越界。
我的答案:16520
public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 300, sum = 0;
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
String[] str = sc.nextLine().split("");
for (int j = 0; j < n; j++) {
a[i][j] = Integer.parseInt(str[j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 3; j++) {
// 行
int a1 = a[i][j] * 1000 + a[i][j+1] * 100 + a[i][j+2] * 10 + a[i][j+3];
if (a1 == 2020) {
sum ++;
}
// 列
int a2 = a[j][i] * 1000 + a[j+1][i] * 100 + a[j+2][i] * 10 + a[j+3][i];
if (a2 == 2020) {
sum ++;
}
// 斜线
if (i < n - 3) {
int a3 = a[i][j] * 1000 + a[i+1][j+1] * 100 + a[i+2][j+2] * 10 + a[i+3][j+3];
if (a3 == 2020) {
sum ++;
}
}
}
}
System.out.println(sum);
}
}
这题仔细观察还是有规律的,我下面的代码是找左上到右下斜线上的数。
我的答案:761
public class Main3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), sum = 0;
for (int i = 1; i <= n + n - 1; i++) {
sum += i;
}
System.out.println(sum - (n - 1));
}
}
这题题目看错,不连起来的灯发亮是不算的,据说答案是:80。
这题我有点懵,我也算了个答案出来:mnlkojihgfedcba,不知对不对,我感觉不妙。
这题,怎么说呢,感觉…没什么感觉。
public class Main6 {
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
f(a);
}
static void f(int[] a) {
int max = a[0], min = a[0];
double sum = 0.0;
for (int i = 0; i < n; i++) {
if (max < a[i])
max = a[i];
if (min > a[i])
min = a[i];
sum += a[i];
}
System.out.println(max);
System.out.println(min);
System.out.printf("%.2f", sum / n);
}
}
这道题思路清晰,我的是这么想的,双指针遍历,右指针定位,左指针查找前面的并计算个数,若个数超过前面的就替换。
public class Main7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
f(s);
}
static void f(String s) {
if (s.length() == 1) {
System.out.println(s);
System.out.println(s.length());
}
char m = s.charAt(0);
int count = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == m) {
count ++;
} else {
int num = 1;
for (int j = 0; j < i; j++) {
if (s.charAt(j) == s.charAt(i)) {
num ++;
}
}
if (num > count) {
m = s.charAt(i);
count = num;
}
}
}
System.out.println(m);
System.out.println(count);
}
}
这题,我想哭死了,看错题目,只算出每次查找最大的,没注意看到“左右向下次数的差不超过1”,20分直接没了。思路就dfs吧,额。。感觉dfs每年都会考,就控制往下和往右下走,记录往下走和往右下走的次数,在回溯的时候减一。
public class Main8 {
private static int N, sum = 0, l = 0, r = 0;
private static int[][] a, xy = {
{
1, 0}, {
1, 1}};
private static int[] b;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
sc.nextLine();
a = new int[N][N];
b = new int[N];
for (int i = 0; i < N; i++) {
String[] s = sc.nextLine().split(" ");
for (int j = 0; j < i + 1; j++) {
a[i][j] = Integer.parseInt(s[j]);
}
}
f(0, 0, 0);
System.out.println(sum);
}
static void f(int x, int y, int index) {
if (index == N - 1) {
if (Math.abs(l - r) <= 1) {
sum = sumF(b);
}
return;
}
for (int i = 0; i < xy.length; i++) {
int o = x + xy[i][0];
int p = y + xy[i][1];
if (o < N && p < N) {
if (i == 0)
l ++;
else
r ++;
b[index] = a[o][p];
f(o, p, 1 + index);
if (i == 0)
l --;
else
r --;
}
}
}
static int sumF(int[] arrb) {
int sum1 = a[0][0];
for (int i = 0; i < N - 1; i++) {
sum1 += arrb[i];
}
if (sum1 > sum)
sum = sum1;
return sum;
}
}
这题做出来了,但是时间复杂度有点高n^3,这里就麻烦广大的网友们帮我优化一下了。
public class Main9 {
static Set<String> set = new HashSet<String>();
static int sum = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
sum += num(s.substring(i, j + 1));
}
}
System.out.println(sum);
}
static int num(String str) {
set.clear();
for (int i = 0; i < str.length(); i++) {
set.add(str.substring(i, i + 1));
}
return set.size();
}
}
放弃