柚子快報激活碼778899分享:java 藍橋杯算法記錄
柚子快報激活碼778899分享:java 藍橋杯算法記錄
算法記錄
因為最近快到藍橋杯了 所以跟練了幾道題 今天就來記錄一下
信號覆蓋
信號覆蓋 解題思路:這題將輸入的x,y的進行儲存 然后將每個點用check方法進行檢測
int t1=x-X[i];
int t2=y-Y[i];
//用這個進行判斷
t1*t1+t2*t2<=r*r
答案
import java.util.Scanner;
// 1:無需package
// 2: 類名必須Main, 不可修改
public class Main {
static int[] X = new int[101];
static int[] Y = new int[101];
static int n = 0;
static int r = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int w = scan.nextInt();
int h = scan.nextInt();
n = scan.nextInt();
r = scan.nextInt();
for (int i = 0; i < n; i++) {
X[i] = scan.nextInt();
Y[i] = scan.nextInt();
}
scan.close();
int res = 0;
for (int i = 0; i <= w; i++) {
for (int j = 0; j <= h; j++) {
if (check(i, j)) {
res++;
}
}
}
System.out.println(res);
}
public static boolean check(int x, int y) {
for (int i = 0; i < n; i++) {
int t1 = x - X[i];
int t2 = y - Y[i];
if (t1 * t1 + t2 * t2 <= r * r) {
return true;
}
}
return false;
}
}
并查集
幼兒園 第一個是初始化 將數(shù)組初始化下標為本身
for(int i=1;i arr[i]=i; } 合并 如果他倆不是一個父節(jié)點再將 public static void merge(int x,int y){ int prex=findRoot(x); int prey=findRoot(y); if(prex!=prey){ arr[prey]=prex; } 查找 傳入查找的數(shù)字 不然繼續(xù)往下查找 public static int findRoot(int key){ if(key==arr[key]){ return key; } return arr[key]=findRoot(arr[key]); } import java.util.Scanner; // 1:無需package // 2: 類名必須Main, 不可修改 public class Main { private static int[] arr = null; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n=scan.nextInt(); int m=scan.nextInt(); arr=new int[n+1]; for(int i=1;i arr[i]=i; } while(m-->0){ int op=scan.nextInt(); int x=scan.nextInt(); int y=scan.nextInt(); if(op==1){ merge(x,y); }else{ int x1=findRoot(x); int y1=findRoot(y); if(x1!=y1){ System.out.println("NO"); }else{ System.out.println("YES"); } } } scan.close(); } public static int findRoot(int key){ if(key==arr[key]){ return key; } return arr[key]=findRoot(arr[key]); } public static void merge(int x,int y){ int prex=findRoot(x); int prey=findRoot(y); if(prex!=prey){ arr[prey]=prex; } } } 簡單的動態(tài)規(guī)劃 臺階問題 答案: 動態(tài)規(guī)劃是這次的狀態(tài)由前幾次狀態(tài)或者上次狀態(tài)推導(dǎo)出來的 階 走法 1 1 2 2 3 4 4 7 5 13 看出 f(n) = f(n-1) + f(n-2) + f(n-3) 然后用這個列方程就行 import java.util.Scanner; // 1:無需package // 2: 類名必須Main, 不可修改 public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n=scan.nextInt(); int []dp=new int[n+1]; if(n<=0){ System.out.println(0); return; } if(n<=2){ System.out.println(n); return; }; if(n==3){ System.out.println(4); return; } dp[0]=0; dp[1]=1; dp[2]=2; dp[3]=4; for(int i=4;i<=n;i++){ dp[i]=dp[i-1]+dp[i-2]+dp[i-3]; } System.out.println(dp[n]); scan.close(); } } 二分求區(qū)間最大值的最小值 跳石頭 這題思路是在起點和終點之間不斷地尋找縮小范圍 如果當(dāng)前每次記錄下來的的值進入 check方法進行判斷 如果當(dāng)前的距離小于最小值搬去不做影響 對搬去的數(shù)字++ 否則就從此處開始計算后續(xù)的有沒有距離更小的 static boolean check(int d){ int cnt=0,pos=0; for(int i=0;i if(arr[i]-pos cnt++; }else{ pos=arr[i]; } } if(cnt<=m){ return true; } return false; } 答案 import java.util.Scanner; // 1:無需package // 2: 類名必須Main, 不可修改 public class Main { static int l,n,m; static int[] arr; public static void main(String[] args) { Scanner scan = new Scanner(System.in); l = scan.nextInt(); n = scan.nextInt(); m = scan.nextInt(); arr = new int[n]; for(int i=0;i arr[i] = scan.nextInt(); } int left=0,right=l; int mid,result=0; while(left<=right){ mid=(left+right)>>1; if(check(mid)){ result=mid; left=mid+1; }else{ right=mid-1; } } System.out.println(result); scan.close(); } static boolean check(int d){ int cnt=0,pos=0; for(int i=0;i if(arr[i]-pos cnt++; }else{ pos=arr[i]; } } if(cnt<=m){ return true; } return false; } } 總結(jié) 直播挺難寫的 算法也挺難寫的 感覺算法基礎(chǔ)還是不行 但是面試也要問 還是每天多寫點吧 柚子快報激活碼778899分享:java 藍橋杯算法記錄 精彩文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。