蓝桥杯官网题解——基础训练:特殊回文数、杨辉三角形、查找整数、数列特征

文章目录

    • 特殊的回文数
    • 杨辉三角形
    • 查找整数
    • 数列特征

特殊的回文数

蓝桥杯官网题解——基础训练:特殊回文数、杨辉三角形、查找整数、数列特征_第1张图片

import java.util.Scanner;
public class lanqiao1{
	public static void main(String[] args) {
		   Scanner scanner = new Scanner(System.in);
		   int n = scanner.nextInt();//
           for(int i = 10000 ; i < 100000 ; i++){
        	   int a = i/10000;
        	   int b = i%10000/1000;
        	   int c = i%1000/100;
        	   int d = i%100/10;
        	   int e = i%10;            
        	   if(a == e & b == d & (a+b+c+d+e)==n) {
        		   System.out.println(i); 
        	   }
           }
           for (int i = 100000; i < 1000000; i++) {
			   int a = i/100000;
			   int b = i%100000/10000;
			   int c = i%10000/1000;
			   int d = i%1000/100;
			   int e = i%100/10;
			   int f = i%10;
			   if(a == f & b == e & c == d & (a+b+c+d+e+f)==n ) {
				   System.out.println(i);
			   }
		}
	}
}

获取N位数的每位数直接按规律记住!

杨辉三角形

蓝桥杯官网题解——基础训练:特殊回文数、杨辉三角形、查找整数、数列特征_第2张图片

import java.util.Scanner;
public class lanqiao1 {
		public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          int n = scanner.nextInt();
          scanner.close();
          int [][]arr = new int[n+1][n+1];
          for(int i=1;i<=n;i++){
        	  for(int j=1;j<=i;j++){
        		  if(j == 1  || i == j)  arr[i][j] = 1;
        		  else arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
        		  
        	  }
		}
          for(int i=1;i<=n;i++){
        	  for(int j=1;j<=i;j++){
        		  System.out.print(arr[i][j]+" ");
        	  }
        	  System.out.println();
         }
}
}

查找整数

蓝桥杯官网题解——基础训练:特殊回文数、杨辉三角形、查找整数、数列特征_第3张图片

import java.util.Scanner;
public class lanqiao1 {
		public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          int n = scanner.nextInt();
          int []arr = new int[n];
          for(int i = 0;i < n;i++)
        	  arr[i] = scanner.nextInt();
          int a= scanner.nextInt();
          scanner.close();
          int count = 0;
          int temp = 0;
          for(int i = 0;i < n;i++)
          {
        	  if(arr[i] == a) {
        		  count++;
        	    if(count==1)  
        	    	System.out.println(i+1);
        	  }
          }
          for(int i = 0;i < n;i++){
        	  if(arr[i] != a){
        		  temp++;
        	     if(temp == n)
        	    	 System.out.println(-1);
        	  }
          }
   }
}

数列特征

蓝桥杯官网题解——基础训练:特殊回文数、杨辉三角形、查找整数、数列特征_第4张图片

import java.util.Arrays;
import java.util.Scanner;
public class lanqiao1 {
		public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          int n = scanner.nextInt();
          int []arr = new int[n];
          for(int i = 0;i < n;i++)
        	  arr[i] = scanner.nextInt();
          scanner.close();
        
          int sum = 0;
          Arrays.sort(arr);
          for(int i = 0;i < n;i++){
        	 sum+=arr[i];
          }
          System.out.println(arr[n-1]);
          System.out.println(arr[0]);
          System.out.println(sum);
}
}

你可能感兴趣的:(算法)