2022java藍橋杯演算法訓練
阿新 • • 發佈:2022-03-07
1.印章:
程式碼:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner s=new Scanner(System.in); 6 int n=s.nextInt(); 7 int m=s.nextInt(); 8 double p=1.0/n; 9 10 double[][] b=new double[m+1][n+1]; 11 ifView Code(n==1){ //當n=1時m>n機率為1 12 b[m][n]=1; 13 System.out.printf("%.4f",b[m][n]); 14 return; 15 } 16 if (n>1&&m<n){ //當m<n時機率為0 17 b[m][n]=0; 18 System.out.printf("%.4f",b[m][n]); 19 return; 20 }21 for (int i = 1; i <=m; i++) { 22 for (int j = 1; j <=n; j++) { 23 if (i<j) b[i][j]=0; //當m<n時機率為0 24 if (j==1){ 25 b[i][j]=Math.pow(p,i-1); //當i張集齊1種的概率 26 }else { 27 b[i][j]=b[i-1][j]*(j*1.0/n)+b[i-1][j-1]*((n-j+1)*1.0/n); //當i張集齊j種的概率 28 } 29 30 } 31 32 } 33 34 System.out.printf("%.4f",b[m][n]); 35 36 } 37 }
2.拿金幣:
程式碼:
1 import java.util.Scanner; 2 3 4 public class Main { 5 6 /* 7 * 輸入:第一行,整數n,表示N*N的一個矩陣。 8 * n行,初始化,每個矩陣的大小。每個數不能找過1000. short型別。 9 * 格式要求:n小於1000 short型別 10 * */ 11 12 /* 13 * 輸出:一行整數sum,表示的是矩陣從左上角到右下角的最大和。 int型別 14 * w*/ 15 16 //程式思路: 17 //1.接受n,建立一個數組 18 //2.初始化陣列。 19 //3.求和。 20 21 public static void main(String[] args){ 22 //1.接受n,建立一個數組 23 Scanner input = new Scanner(System.in); 24 short n = input.nextShort(); 25 26 //2.初始化陣列。 27 int[][] arrys = new int[n][n]; 28 for (int i = 0; i <n ; i++) { 29 for (int j = 0; j <n ; j++) { 30 arrys[i][j] = input.nextInt(); 31 32 } 33 34 } 35 36 37 38 39 40 41 42 43 //3.求和 44 for (int i = 0; i < n; i++) { 45 for (int j = 0; j < n; j++) { 46 //初始化固定部分,arrys[0][0]-a[0][n-1]。a[0][0]-a[n-1][0]。 47 //因為從這一部分走,方向是唯一,也就是和是唯一的,沒有別的路。 48 if (i==0&&j==0)//初始化首位。 49 continue; 50 51 52 if (i == 0) {//首行非首位。 53 arrys[i][j] = (arrys[i][j-1]+arrys[i][j]);//運算+ - 至少要int型別,所以會被強制轉換,而不是下標問題。 54 continue; 55 56 } 57 58 if (j == 0) {//首列,非首位。 59 arrys[i][j] = (arrys[i-1][j]+arrys[i][j]); 60 continue; 61 } 62 63 if (arrys[i - 1][j] > arrys[i][j - 1]) {//上比左大。 64 arrys[i][j] = (arrys[i][j]+arrys[i-1][j]); 65 }else arrys[i][j] =(arrys[i][j]+arrys[i][j-1]);//左比上大。 66 67 68 } 69 } 70 71 72 System.out.println(arrys[n-1][n-1]); 73 74 } 75 76 }View Code
3.數字遊戲:
程式碼:
1 import java.util.Scanner; 2 /* 3 * 4 * */ 5 public class Main { 6 7 static int n;//初始的n個數 8 static int sum;//相加的結果 9 static int arr1[]; 10 static boolean bool = true;//標記是否找到n個元素 11 12 public static void main(String[] args) { 13 Scanner sc = new Scanner(System.in); 14 n = sc.nextInt(); 15 sum = sc.nextInt(); 16 17 int array[] = new int[n]; 18 int visit[] = new int[n+1]; //訪問標記 19 20 dfs(0, array, visit); 21 } 22 23 //step:當前已經遍歷的元素數;array:存放當前遍歷的元素; visit:標記當前哪些元素已被訪問; 24 public static void dfs(int step,int arr[], int vis[]){ 25 if(step == n){//1.找到n個數字,檢查這n個數相加是否等於sum 26 int arr1[] = new int[n]; 27 for(int i=0;i<n;i++){//先將array陣列中的元素 放到arr1 28 arr1[i] = arr[i]; 29 }//for 30 31 for(int i=1;i<n;i++){//累加到arr1[0] 32 for(int j=0;j<n-i;j++){ 33 arr1[j]=arr1[j]+arr1[j+1]; 34 } 35 }//for 36 37 if(arr1[0] == sum){//累加後與sum相等,則輸出 38 for(int i:arr){ 39 System.out.print(i+" "); 40 } 41 bool = false;//已經找到符合要求的n個元素 42 return; 43 }else{//累加後與sum不相等,退出重新查詢 44 return; 45 } 46 47 }//if 48 if(bool == true){//2.沒有找到符合要求的n個元素 49 for(int i=1;i<=n;i++){ 50 if(vis[i]==0){//i值沒有使用過 51 arr[step]=i;//儲存當前訪問的元素 52 vis[i]=1; 53 dfs(step+1,arr,vis);//從當前這個元素起,重新進行遍歷 54 vis[i]=0; 55 } 56 } 57 }//if 58 return; 59 } 60 }View Code
4.無聊的逗:
程式碼:
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner cin=new Scanner(System.in); 5 int ans=Integer.MIN_VALUE; 6 int n = cin.nextInt(); 7 int []array=new int[1<<n]; 8 int []nums=new int[n]; 9 for(int i=0;i<n;++i){ 10 nums[i]=cin.nextInt(); 11 array[1<<i]=nums[i]; 12 } 13 for(int i=0;i<1<<n;++i){ 14 for(int j=0;j<n;++j){ 15 if((i&(1<<j))==0)continue; 16 array[i]=array[i-(1<<j)]+nums[j]; 17 break; 18 } 19 } 20 for(int i=1;i<(1<<n);++i){ 21 int j=(1<<n)-i-1; 22 for(int k=j;k>0;k=(k-1)&j){ 23 if(array[k]==array[i]) 24 ans=Math.max(array[k],ans); 25 } 26 } 27 System.out.print(ans); 28 } 29 }View Code
5.禮物:
程式碼:
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 public class Main{ 5 static int count =0; 6 static long S; 7 public static void main(String args[]) { 8 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 String s,s2; 10 String[] b1 = null,b2 = null; 11 try { 12 s=br.readLine(); 13 b1=s.split(" "); 14 s2=br.readLine(); 15 b2=s2.split(" "); 16 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 int N=Integer.parseInt(b1[0]); 22 S=Long.parseLong(b1[1]); 23 24 int[] a=new int[N+1]; 25 long[] sum=new long[N+1]; 26 for(int i=1;i<=b2.length;i++) { 27 a[i]=Integer.parseInt(b2[i-1]); 28 sum[i]=sum[i-1]+a[i]; 29 } 30 int l=1,r=N; 31 while(l<r) { 32 int mid=(l+r+1)/2; 33 if(fun(mid,N,sum)) { 34 l=mid; 35 }else { 36 r=mid-1; 37 } 38 } 39 System.out.print(2*l); 40 } 41 public static boolean fun(int mid,int n,long[] sum) { 42 for(int i=mid;i<=n-mid;i++) { 43 if(sum[i]-sum[i-mid]<=S&&sum[i+mid]-sum[i]<=S) { 44 return true; 45 } 46 } 47 return false; 48 } 49 }View Code