刷題計劃
阿新 • • 發佈:2022-05-06
刷題的計劃
小結
最好在每次的刷題結束後都總結一下,刷題的經驗
為了以後的進步
就比如最近刷了幾個入門的題目
P1534
package com.LG.Base; import java.util.Scanner; public class P1534 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), a, b, s = 0, c = 0; for (int i = 0; i < n; i++) { a = in.nextInt(); b = in.nextInt(); c += a + b - 8; // c = a + b - 8; s += c; } System.out.println(s); in.close(); } }
要注意看清題意
P1548
package com.LG.Base; import java.util.Scanner; public class P1548 { // It can be solved by formula // But I don't know how to do it. public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(), b = in.nextInt(); int c = 0, d = 0; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { for (int j2 = i + 1; j2 <= a; j2++) { for (int k = j + 1; k <= b; k++) { if (j2 - i == k - j) { c++; } else { d++; } } } } } System.out.println(c + " " + d); in.close(); } }
其實是有公式的,只是當時還不知道
下面引用的別人的推導方法
“
說說公式是怎麼推導的吧
找規律:
正方形:
邊長為1的正方形個數為n*m
邊長為2的正方形個數為(n-1)*(m-1) (自己動手想想)
邊長為3的正方形為個數(n-2)*(m-2)
邊長為min(n,m)的正方形為個數s1=(n-min(n,m)+1)*(m-min(n,m)+1)
然後從邊長為1到min(m,m)的正方形個數全部加起來;
長方形:(包括正方形,好像正方形屬於長方形來著?)
長為1的長方形(包括正方形)有n個
長為2的長方形(包括正方形)有n-1個
長為n的長方形(包括正方形)有1個
長為1到n的長方形1+2+...+n個
同理 寬為1的長方形(包括正方形)有m個
寬為2的長方形(包括正方形)有m-1個
寬為m的長方形(包括正方形)有1個
寬為1-m的長方形1+2+...+m個
然後把它們乘起來,根據乘法原理,總數s2=((1+n)(1+m)n*m)/4;
題目要求的是“非正方形的長方形”,因此要減去s1;
”
P1554
沒什麼好說的,第一次用的是迴圈,結果超時了,足足4s多,好傢伙
後來就直接用分支了
package com.LG.Base;
import java.util.Scanner;
public class P1554 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt(), b = in.nextInt();
int c[] = new int[10];
for (int i = a; i <= b; i++) {
for (int j2 = 0; j2 < (i + "").length(); j2++) {
if ((i + "").charAt(j2) == 0 + 48) {
c[0]++;
} else if ((i + "").charAt(j2) == 1 + 48) {
c[1]++;
} else if ((i + "").charAt(j2) == 2 + 48) {
c[2]++;
} else if ((i + "").charAt(j2) == 3 + 48) {
c[3]++;
} else if ((i + "").charAt(j2) == 4 + 48) {
c[4]++;
} else if ((i + "").charAt(j2) == 5 + 48) {
c[5]++;
} else if ((i + "").charAt(j2) == 6 + 48) {
c[6]++;
} else if ((i + "").charAt(j2) == 7 + 48) {
c[7]++;
} else if ((i + "").charAt(j2) == 8 + 48) {
c[8]++;
} else if ((i + "").charAt(j2) == 9 + 48) {
c[9]++;
}
}
}
for (int i : c) {
System.out.print(i + " ");
}
in.close();
}
}
P1567
其實是可以不需要陣列的
package com.LG.Base;
import java.util.Scanner;
public class P1554 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt(), b = in.nextInt();
int c[] = new int[10];
for (int i = a; i <= b; i++) {
for (int j2 = 0; j2 < (i + "").length(); j2++) {
if ((i + "").charAt(j2) == 0 + 48) {
c[0]++;
} else if ((i + "").charAt(j2) == 1 + 48) {
c[1]++;
} else if ((i + "").charAt(j2) == 2 + 48) {
c[2]++;
} else if ((i + "").charAt(j2) == 3 + 48) {
c[3]++;
} else if ((i + "").charAt(j2) == 4 + 48) {
c[4]++;
} else if ((i + "").charAt(j2) == 5 + 48) {
c[5]++;
} else if ((i + "").charAt(j2) == 6 + 48) {
c[6]++;
} else if ((i + "").charAt(j2) == 7 + 48) {
c[7]++;
} else if ((i + "").charAt(j2) == 8 + 48) {
c[8]++;
} else if ((i + "").charAt(j2) == 9 + 48) {
c[9]++;
}
}
}
for (int i : c) {
System.out.print(i + " ");
}
in.close();
}
}
懶得說了。