填空題
第1題
問題描述
在計算機存儲中,15.125GB是多少MB?
> Answer:15488
> 15.125 × 1024 = 15488
第2題
問題描述
1200000有多少個約數(只計算正約數)。
> Answer:96
> 直接暴力遍歷得出結果填上去
第3題
問題描述
一棵包含有2019個結點的二叉樹,最多包含多少個葉結點?
> Answer:1010
> 套用公式,當時不記得了,基礎不紮實。。。結果算錯了!
n = n0 + n1 + n2,爲使葉子節點數(n0)最多,必須n1最小,設爲0,而n0 = n2 + 1
得2019 = n0 + 0 + n0 -1
所以n0=1010
第4題
問題描述
在1至2019中,有多少個數的數位中包含數字9?
注意,有的數中的數位中包含多個9,這個數只算一次。例如,1999這個數包含數字9,在計算只是算一個數。
> Answer:544
> excel 序列生成1到2019,然後篩選
編程題
第5題
問題描述
在數列 a[1], a[2], …, a[n] 中,如果對於下標 i, j, k 滿足 0<i<j<k<n+1 且 a[i]<a[j]<a[k],則稱 a[i], a[j], a[k] 爲一組遞增三元組,a[j]爲遞增三元組的中心。
給定一個數列,請問數列中有多少個元素可能是遞增三元組的中心。
輸入格式
輸入的第一行包含一個整數 n。
第二行包含 n 個整數 a[1], a[2], …, a[n],相鄰的整數間用空格分隔,表示給定的數列。
輸出格式
輸出一行包含一個整數,表示答案。
樣例輸入
5
1 2 5 3 5
樣例輸出
2
樣例說明
a[2] 和 a[4] 可能是三元組的中心。
評測用例規模與約定
對於 50% 的評測用例,2 <= n <= 100,0 <= 數列中的數 <= 1000。
對於所有評測用例,2 <= n <= 1000,0 <= 數列中的數 <= 10000。
> 大致思路是從前往後遍歷
> 2個全局變量min和max,分別表示當前最小值和最大值的下標
> 每次先和min比較,arr[min]>=arr[current],直接返回false,否則更新min
> 接着和max比較(注意max要比current大),如果當前的最大值不符合要求,則向後面尋找,
> 找到了符合要求的值,則更新最大值並記錄下標,返回true。找不到,返回false。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = reader.nextInt();
}
int count = 0;
for (int i = 1; i < arr.length - 1; i++) {
if (check(arr, i)) {
count++;
}
}
System.out.println(count);
}
static int min = 0;
static int max = 0;
static boolean check(int[] arr, int index) {
for (int i = max; i < arr.length; i++) {
if (arr[min] >= arr[index]) {
min = index;
return false;
}
if (index < i && arr[index] < arr[i]) {
max = i;
return true;
}
}
return false;
}
}
第6題
問題描述
一個正整數如果任何一個數位不大於右邊相鄰的數位,則稱爲一個數位遞增的數,例如1135是一個數位遞增的數,而1024不是一個數位遞增的數。
給定正整數 n,請問在整數 1 至 n 中有多少個數位遞增的數?
輸入格式
輸入的第一行包含一個整數 n。
輸出格式
輸出一行包含一個整數,表示答案。
樣例輸入
30
樣例輸出
26
評測用例規模與約定
對於 40% 的評測用例,1 <= n <= 1000。
對於 80% 的評測用例,1 <= n <= 100000。
對於所有評測用例,1 <= n <= 1000000。
沒有想到很好的方法,簡單的從後往前一位一位判斷
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int count = 0;
for (int i = 1; i <= n; i++) {
if (check(i)) {
count++;
}
}
System.out.println(count);
}
static boolean check(int num) {
int pre = num % 10;
int current = num / 10;
while (current > 0) {
int tmp = current % 10;
if (tmp > pre) {
return false;
}
pre = tmp;
current /= 10;
}
return true;
}
}
第7題
問題描述
小明對類似於 hello 這種單詞非常感興趣,這種單詞可以正好分爲四段,第一段由一個或多個輔音字母組成,第二段由一個或多個元音字母組成,第三段由一個或多個輔音字母組成,第四段由一個或多個元音字母組成。
給定一個單詞,請判斷這個單詞是否也是這種單詞,如果是請輸出yes,否則請輸出no。
元音字母包括 a, e, i, o, u,共五個,其他均爲輔音字母。
輸入格式
輸入一行,包含一個單詞,單詞中只包含小寫英文字母。
輸出格式
輸出答案,或者爲yes,或者爲no。
樣例輸入
lanqiao
樣例輸出
yes
樣例輸入
world
樣例輸出
no
評測用例規模與約定
對於所有評測用例,單詞中的字母個數不超過100。
直接使用正則表達式,100個字母,不算長
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String str = reader.next();
if(str.matches("[^aeiou]+[aeiou]+[^aeiou]+[aeiou]+")){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}
第8題
問題描述
小明想知道,滿足以下條件的正整數序列的數量:
1. 第一項爲 n;
2. 第二項不超過 n;
3. 從第三項開始,每一項小於前兩項的差的絕對值。
請計算,對於給定的 n,有多少種滿足條件的序列。
輸入格式
輸入一行包含一個整數 n。
輸出格式
輸出一個整數,表示答案。答案可能很大,請輸出答案除以10000的餘數。
樣例輸入
4
樣例輸出
7
樣例說明
以下是滿足條件的序列:
4 1
4 1 1
4 1 2
4 2
4 2 1
4 3
4 4
評測用例規模與約定
對於 20% 的評測用例,1 <= n <= 5;
對於 50% 的評測用例,1 <= n <= 10;
對於 80% 的評測用例,1 <= n <= 100;
對於所有評測用例,1 <= n <= 1000。
直接暴力搜索顯然會超時,但是我沒有找出規律,只好簡單的優化了一下
昨天在網上看到一種方式,打表,即直接把答案寫在程序裏,用數組存儲,這樣時間複雜度就o(1)了...這樣的方法算是取巧了
測試了一下,我的方法只能通過80%的測試,
今天看了下官方的題解,把自己的理解記錄一下。也是自己做題做太少了,
f(pre,cur)表示前面一個數字是pre,後面一個數字是cur。
例如n=4的時候,
f(4,1)表示
4 1
4 1 1
4 1 2
f(4,2)表示
4 1
4 1 1
4 1 2
4 2
4 2 1
f(4,2) = f(4,1) + “4 2“ 開頭的組合數量
f(4,3) = f(4,2) + “4 3“ 開頭的組合數量
f(4,4) = f(4,3) + “4 4“ 開頭的組合數量
f(4,4)就是n=4的時候的解了
那麼接下來就是怎麼計算上面遞推式後面這部分的內容了
用(“4 2“ 開頭的組合數量)爲例子
首先 ”4 2“單獨成一個序列,所以先 + 1
然後計算出後面序列即 4 2 ? 中 ? 的最大值,? = abs(pre-cur)-1 = abs(4-2)-1 = 1 (從第三項開始,每一項小於前兩項的差的絕對值。)
則f(4,2) = 1 + f(4,1) + f(2,1)
那麼就可以得到下面的遞推式
f(i,j) = 1 + f(i,j-1) + f(j,abs(i-j)-1)
原來的思路
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
record = new int[n][n];
int sum = 0;
for (int i = 1; i <= n; i++) {
sum+= fun(n, i);
sum %= 10000;
}
System.out.println(sum);
}
static int[][] record;
static int fun(int a, int b) {
if (record[a - 1][b - 1] != 0) {
return record[a - 1][b - 1];
}
int tmp = Math.abs(a - b);
int sum = 1;
for (int i = 1; i < tmp; i++) {
sum += fun(b, i);
sum %= 10000;
}
record[a-1][b-1] = sum;
return sum;
}
}
題解中的思路
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
record = new int[n][n];
System.out.println(fun(n, n));
}
static int[][] record;
static final int MOD = 10000;
static int fun(int a, int b) {
if (b <= 0)
return 0;
if (record[a - 1][b - 1] != 0) {
return record[a - 1][b - 1];
}
return record[a - 1][b - 1] = (1 + fun(a, b - 1) + fun(b, Math.abs(a - b) - 1)) % MOD;
}
}
第9題
問題描述
小明有一塊空地,他將這塊空地劃分爲 n 行 m 列的小塊,每行和每列的長度都爲 1。
小明選了其中的一些小塊空地,種上了草,其他小塊仍然保持是空地。
這些草長得很快,每個月,草都會向外長出一些,如果一個小塊種了草,則它將向自己的上、下、左、右四小塊空地擴展,這四小塊空地都將變爲有草的小塊。
請告訴小明,k 個月後空地上哪些地方有草。
輸入格式
輸入的第一行包含兩個整數 n, m。
接下來 n 行,每行包含 m 個字母,表示初始的空地狀態,字母之間沒有空格。如果爲小數點,表示爲空地,如果字母爲 g,表示種了草。
接下來包含一個整數 k。
輸出格式
輸出 n 行,每行包含 m 個字母,表示 k 個月後空地的狀態。如果爲小數點,表示爲空地,如果字母爲 g,表示長了草。
樣例輸入
4 5
.g…
…
…g…
…
2
樣例輸出
gggg.
gggg.
ggggg
.ggg.
評測用例規模與約定
對於 30% 的評測用例,2 <= n, m <= 20。
對於 70% 的評測用例,2 <= n, m <= 100。
對於所有評測用例,2 <= n, m <= 1000,1 <= k <= 1000。
一個一個月慢慢來,簡單說就是暴力
昨天在網絡上吸取的經驗,還是自己練習太少,
這題使用過隊列只考慮長了草的空地比起遍歷整個二維數組要快。
原先的思路
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int m = reader.nextInt();
char[][] map = new char[n][];
for (int i = 0; i < map.length; i++) {
map[i] = reader.next().toCharArray();
}
int k = reader.nextInt();
while (k-- > 0) {
map = fun(map);
}
for (int i = 0; i < map.length; i++) {
System.out.println(map[i]);
}
}
static char[][] fun(char[][] old) {
char[][] next = new char[old.length][old[0].length];
int[] x = { 1, -1, 0, 0 };
int[] y = { 0, 0, 1, -1 };
char g = 'g';
for (int i = 0; i < next.length; i++) {
for (int j = 0; j < next[i].length; j++) {
if (old[i][j] == g) {
next[i][j] = g;
for (int k = 0; k < y.length; k++) {
try {
next[i + x[k]][j + y[k]] = g;
} catch (Exception e) {
continue;// 可能越界,直接用異常處理,省去判斷
}
}
} else if (next[i][j] != g) {
next[i][j] = '.';
}
}
}
return next;
}
}
學習的思路
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int m = reader.nextInt();
Queue<Node> grass = new LinkedList<Node>();
map = new char[n][];
for (int i = 0; i < map.length; i++) {
map[i] = reader.next().toCharArray();
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == g) {
Node node = new Node(i, j);
grass.add(node);
}
}
}
int k = reader.nextInt();
while (k-- > 0) {
grass = fun(grass);
}
for (int i = 0; i < map.length; i++) {
System.out.println(map[i]);
}
}
static char[][] map;
static char g = 'g';
static Queue<Node> fun(Queue<Node> grass) {
Queue<Node> next = new LinkedList<Node>();
int[] x = { 1, -1, 0, 0 };
int[] y = { 0, 0, 1, -1 };
while (!grass.isEmpty()) {
Node node = grass.poll();
if (map[node.x][node.y] == g) {
for (int i = 0; i < y.length; i++) {
int tmpX = node.x + x[i];
int tmpY = node.y + y[i];
try {
map[tmpX][tmpY] = g;
} catch (Exception e) {
continue; // 可能越界,直接用異常處理,省去判斷
}
Node nextNode = new Node(tmpX, tmpY);
next.add(nextNode);
}
}
}
return next;
}
}
class Node {
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
第10題
問題描述
小明要組織一臺晚會,總共準備了 n 個節目。然後晚會的時間有限他只能最終選擇其中的 m 個節目。
這 n 個節目是按照小明設想的順序給定的,順序不能改變。
小明發現,觀衆對於晚上的喜歡程度與前幾個節目的好看程度有非常大的關係,他希望選出的第一個節目儘可能好看,在此前提下希望第二個節目儘可能好看,依次類推。
小明給每個節目定義了一個好看值,請你幫助小明選擇出 m 個節目,滿足他的要求。
輸入格式
輸入的第一行包含兩個整數 n, m ,表示節目的數量和要選擇的數量。
第二行包含 n 個整數,依次爲每個節目的好看值。
輸出格式
輸出一行包含 m 個整數,爲選出的節目的好看值。
樣例輸入
5 3
3 1 2 5 4
樣例輸出
3 5 4
樣例說明
選擇了第1, 4, 5個節目。
評測用例規模與約定
對於 30% 的評測用例,1 <= n <= 20;
對於 60% 的評測用例,1 <= n <= 100;
對於所有評測用例,1 <= n <= 100000,0 <= 節目的好看值 <= 100000。
JAVA
做題的時候想錯了,錯誤思路如下
建立一個類
class Node{ int index; int data}
先對data屬性降序排序,然後取前m個元素,按index升序排序
錯誤原因:
這樣取的是好看程度的總和最大,與題意不相符
錯誤思路
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int m = reader.nextInt();
Node[] arr = new Node[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Node();
arr[i].index = i;
arr[i].data = reader.nextInt();
}
Arrays.sort(arr, new Comparator<Node>() {
@Override
public int compare(Node arg0, Node arg1) {
return arg1.data - arg0.data;
}
});
Arrays.sort(arr, 0, m, new Comparator<Node>() {
@Override
public int compare(Node arg0, Node arg1) {
return arg0.index - arg1.index;
}
});
for (int i = 0; i < m-1; i++) {
System.out.print(arr[i].data+" ");
}
System.out.println(arr[m-1].data);
}
}
class Node {
int index;
int data;
}
官方題解https://blog.csdn.net/zhengwei223/article/details/105065566