局長的食物
1.題目描述
每天局長會喫一份食物,或者買一份食物(即每天只能進行喫或買其中的一種動作),這樣過了M天現在局長想知道M天后第p種食物的份數排名(從大到小,相同算並列,例如3 3 2,則排名爲1 1 3)N,M,P<=100,Ai<=1000。
輸入描述:
第一行N M P
第二行N個數Ai
接下來M行,每行Ai或者Bi分別表示買一份食物i,喫一份食物i
輸出
一個答案
輸入樣例:
3 4 2 5 3 1 B 1 A 2 A 2 A 3
輸出樣例:
1
2.解題思路
方法1:
模擬喫東西的過程:關鍵在於喫一份食物還是買一份食物數據的存儲方式,本次的數據存儲方式爲 List<List< String > >,注意後面的Integer.parseInt的轉型。更好的方法是用對象存儲
方法2:
用class存儲數據
3.代碼
方法1: 用List<List< String > >存儲數據
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
int N = sr.nextInt();
int M = sr.nextInt();
int P = sr.nextInt();
int a[] = new int[N];
for(int i = 0;i<N;i++){
a[i] = sr.nextInt();
}
List<List<String>> list = new ArrayList<>();
for(int i = 0;i<M;i++){
List<String> temp= new ArrayList<>();
temp.add(sr.next());
temp.add(sr.next());
list.add(temp);
}
System.out.println(solution(list,P,a));
}
private static int solution(List<List<String>> list, int p, int[] a) {
for(int i = 0;i<list.size();i++){
// 買一份食物
if(list.get(i).get(0).equals( "A")){
int index = Integer.parseInt(list.get(i).get(1)) - 1;
a[index] ++;
}else{
//喫一份食物
int index = Integer.parseInt(list.get(i).get(1)) - 1;
a[index] --;
}
}
//第p種食物的份數排名
int res = 1;
for(int i = 0;i<a.length;i++){
if(a[p-1]<a[i]){
res++;
}
}
return res;
}
}
方法2:用對象存儲
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
int N = sr.nextInt();
int M = sr.nextInt();
int P = sr.nextInt();
int a[] = new int[N];
for(int i = 0;i<N;i++){
a[i] = sr.nextInt();
}
List<Category> lists = new ArrayList<>();
for(int i = 0;i<M;i++){
lists.add(new Category(sr.next(),sr.nextInt()));
}
System.out.println(solution(lists,P,a));
}
private static int solution(List<Category> lists, int p, int[] a) {
for(int i = 0;i<lists.size();i++){
//買一份食物
if(lists.get(i).x.equals("A")){
a[lists.get(i).getIndex()-1]++;
}else{
//喫一份食物
a[lists.get(i).getIndex()-1]--;
}
}
//第p種食物的份數排名
int res = 1;
for(int i = 0;i<a.length;i++){
if(a[p-1]<a[i]){
res++;
}
}
return res;
}
}
//存儲 [A 2] 等數據
class Category{
String x;
int index;
public Category(String x, int index) {
this.x = x;
this.index = index;
}
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
清雨的自助餐
1.題目描述
清雨又在喫自助餐了。
排在清雨面前的有N種食物,排成一排,清雨可以選擇其中的若干種食物,但是不能連續選擇相鄰的食物。因爲清雨很挑食,當所有食物都不合口味時,他可以一種都不選,即一個都不選也算爲一種方法。
請問他有多少種選擇食物的方法呢?
輸入
一個整數n(1 <= n <= 90)
輸出
一個正整數表示答案
樣例輸入
3
樣例輸出
5
2. 解題思路
動態規劃問題:dp[i]表示有(i+1)種食物的時候有多少種選擇的方法
- 初始化
- dp[0] = 2,表示有1種食物,可以選擇喫或者不喫2種方法
- dp[1] = 3,表示有兩種食物,可以選擇不喫,喫第一種,喫第二種 3種方法
- 動態規劃狀態轉移矩陣
- 如果選這個食物(第i+1種食物),則選擇爲dp[i-2]種選法,若不選擇這個食物,則方法數爲dp[i-1]{因爲相鄰而不能選}即
- dp[i] = dp[i-2] + dp[i-1];
注:該題也可以用斐波那契數列的應用,只是初始值變了,具體參看劍指offer_【7】斐波那契數組:https://blog.csdn.net/qq_17556191/article/details/94436130
3.代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
int N = sr.nextInt();
System.out.println(solution(N));
}
public static long solution(int num) {
if (num == 0)
return 1;
else if (num == 1)
return 2;
else {
long dp[] = new long[num];
dp[0] = 2; //只有一種食物,可以選擇 喫 或者不喫2種方法
dp[1] = 3; //有2種食物,選擇 不喫 ,喫第一種食物,喫第二種食物。
for (int i = 2; i < num; i++) {
dp[i] = dp[i - 2] + dp[i - 1];
}
return dp[num - 1];
}
}
}
庫特君的麪條
1.題目描述
題目描述:
庫特君在吃麪條!
他將麪條放在了數軸上,每根麪條對應數軸上的兩個點a和b,他想知道在任意兩根麪條不重疊(端點可以重疊)的情況下最多能選出多少根麪條。
1 <= n <= 100
-999 <= a
輸入描述:
第一行爲一個整數N
接下來,N行每行N個整數a和b
輸出描述:
一個數的答案
輸出樣例:
3 6 3 1 3 2 5
輸出樣例:
2
2.解題思路
貪心算法的區間調度問題
- 貪心策略:在不與已選區域重疊的前提下,優先選擇右端點最小的區間,所以按照區間右端點排序後,依次檢查,不重疊就選擇,並更新已選區域的最右端,方便判斷重疊。
算法設計:
- 如果被檢查的活動i的開始時間小於最近選擇的活動j的結束時間,則不選擇活動i,否則選擇活動i
- 1.選擇最早結束的麪條,便是最開始要選擇的沒有重疊的第一根麪條
- 2.選擇第一根麪條結束後[右端點值]纔開始的第二根麪條[該面條的左端點大於等於第一根麪條的右端點],並且此時右端點結束最早的麪條,這將是要選擇的第二根麪條。
- 3.更新右端點值,重複以上。
3.代碼
import java.util.*;
//貪心算法的區間調度問題
public class Main {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
int N = sr.nextInt();
List<Work> lists = new ArrayList<>();
for(int i = 0;i<N;i++){
int x1 = sr.nextInt();
int x2 = sr.nextInt();
//題目說了x1<x2,但是給出的樣例出現了問題,故用個if語句
if(x1<x2)
lists.add(new Work(x1,x2));
else
lists.add(new Work(x2,x1));
}
System.out.println(solution(lists));
}
//解決方法
private static int solution(List<Work> works) {
//works裏面已經按end從小到大排序了,這時在將start進行排序,找到第一個要進行的工作
Collections.sort(works);
int count = 1;
//當前工作的結束時間
int endTime = works.get(0).getEnd();
for(int i = 1;i<works.size();i++){
// 第二根麪條的開始時間大於第一個麪條的結束時間
if(endTime<=works.get(i).getStart()){
//這個麪條可選,沒有覆蓋之前的麪條
count++;
//更新右端點
endTime = works.get(i).getEnd();
}
}
return count;
}
static class Work implements Comparable{
private int start;
private int end;
public Work(int start, int end) {
this.start = start;
this.end = end;
}
//end 從小到大排序
@Override
public int compareTo(Object o) {
Work work = (Work) o;
if(this.end > work.getEnd()){
return 1;
}else if(this.end == work.getEnd()){
return 0;
}else{
return -1;
}
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
}
}
題目整理自牛客技術篇
個人想法,如有錯誤請指出!!