蓝桥杯打卡day02(3.5)

文章目录

      • 奇数倍数
      • 求值
      • 求和
      • 数位排序

奇数倍数


public class 奇数倍数 {
	public static void main(String[] args) {
		for(int i = 2; ; i++) {
			if(isOdd(i * 2019)) {
				System.out.println(i * 2019);
				break;
			}
		}
}
	static boolean isOdd(long n) {
		while(n != 0) {
			long m = n % 10;
			if(m % 2 == 0)return false;
			n /= 10;
		}
		return true;
	}
}

求值

import java.util.Scanner;

public class 求值 {
	public static void main(String[] args) {
	// 	int res = 0;
	// for(int i = 10; i <= 45360; i ++) {
	// 	int cnt = 0;
	// 	for(int j = 1; j <= i; j ++) {
	// 		if(i % j == 0)cnt++;
	// 	}
	// 	if(cnt == 100) {
	// 		res = i;
	// 		break;
	// 	}
	// }
	System.out.println(45360);
		
	}
}

求和

import java.io.*;

public class 求和 {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(br);
	public static void main(String[] args) throws NumberFormatException, IOException {
		int n = Integer.parseInt(br.readLine());
		int[] a = new int[n];
		String[] s = br.readLine().split(" ");
		long sum = 0;
		for(int i = 0; i < s.length; i ++) {
			a[i] = Integer.parseInt(s[i]);
			sum += a[i];
		}
		
		long res = 0;
		for(int i = 0; i < n; i ++) {
			sum -= a[i];
			res += sum * a[i];
		}
		System.out.println(res);
	}
}


数位排序

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class 数位排序 {
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		PriorityQueue pq = new PriorityQueue<>(new Comparator() {
			@Override
			public int compare(int[] o1,int[] o2) {
				if(o1[0] != o2[0])return o1[0] - o2[0];
				return o1[1] - o2[1];
			}
		});
		for(int i = 1; i <= n; i ++) {
			int v = change(i);
			pq.offer(new int[] {v,i});
		
		}
		while(m --!= 1) {
			pq.poll();
		}
		int[] arr = pq.poll();
		System.out.println(arr[1]);
		
	}
	
	static int change(int num) {
		int sum = 0;
		while(num != 0) {
			sum += num % 10;
			num /=10;
		}
		return sum;
	}
}
	

你可能感兴趣的:(蓝桥杯,java,算法)