這題,感覺還行。
我的答案: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 = sc.nextInt(), sum = 0;
int[][] a = new int[n][n];
sc.nextLine();
for (int i = 0; i < n; i++) {
String s = sc.nextLine();
String[] str = s.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 ++;
}
}
}
// 斜線
for (int i = 0; i < n-3; i++) {
for (int j = 0; j < n - 3; j++) {
int a1 = a[i][j] * 1000 + a[i+1][j+1] * 100 + a[i+2][j+2] * 10 + a[i+3][j+3];
if (a1 == 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();
}
}
放棄