一、蓝桥杯试题 历届真题 后缀表达式【第十届】【省赛】【B组】
(1)题目描述
(2)代码描述
import java.io.*;
import java.math.*;
public class Main{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
public static int Int(String s){
return Integer.parseInt(s);
}
public static void main(String[] args) throws Exception{
String[] s = in.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
String[] arr = in.readLine().split(" ");
int len = n + m + 1;
long sum = 0;
if(m == 0){ // 全是加号
for(int i = 0; i < len; i++)
sum += Integer.parseInt(arr[i]);
}
else{
int max = 0, min = 0;
for(int i = 0; i < len; i++){
if(Int(arr[min]) > Int(arr[i]))
min = i;
if(Int(arr[max]) < Int(arr[i]))
max = i;
}
sum = Int(arr[max]) - Int(arr[min]);
for(int i = 0; i < len; i++){
if(i != min && i != max)
sum += Math.abs(Int(arr[i]));
}
}
out.write(sum + "");
out.flush();
}
}
二、蓝桥杯试题 历届真题 特别数的和【第十届】【省赛】【B组】
(1)问题描述
问题描述
小明对数位中含有 2、0、1、9 的数字很感兴趣(不包括前导 0),在 1 到
40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。请问,在 1 到 n 中,所有这样的数的和是多少?
输入格式
输入一行包含两个整数 n。
输出格式
输出一行,包含一个整数,表示满足条件的数的和。
样例输入
40
样例输出
574
评测用例规模与约定
对于 20% 的评测用例,1 ≤ n ≤ 10。 对于 50% 的评测用例,1 ≤ n ≤ 100。对于 80% 的评测用例,1 ≤ n ≤ 1000。对于所有评测用例,1 ≤ n ≤ 10000。
(2)代码实现
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
long sum = 0;
for (int i = 1; i <= n; i++) {
int tempI = i;
while (tempI != 0) {
int temp = tempI % 10;
if (temp == 1 || temp == 2 || temp == 9 || temp == 0) {
sum += i;
break;
}
tempI /= 10;
}
}
System.out.println(sum);
}
}
三、蓝桥杯试题 历届真题 等差数列【第十届】【省赛】【B组】
(1)题目描述
问题描述
数学老师给小明出了一道等差数列求和的题目。但是粗心的小明忘记了一部分的数列,只记得其中 N 个整数。
现在给出这 N 个整数,小明想知道包含这 N 个整数的最短的等差数列有几项?
输入格式
输入的第一行包含一个整数 N。
第二行包含 N 个整数 A₁, A₂, · · · , AN。(注意 A₁ ∼ AN 并不一定是按等差数列中的顺序给出)
输出格式
输出一个整数表示答案。
样例输入
5
2 6 4 10 20
样例输出
10
样例说明
包含 2、6、4、10、20 的最短的等差数列是 2、4、6、8、10、12、14、16、 18、20。
(2)问题分析
import java.util.Scanner;
public class Main {
public static void quickPX(long arr[]){
for (int i = 1; i < arr.length; i++) {
long temp = arr[i];
int j = i-1;
while (j>=0&&temp<arr[j]) {
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
long arr[]=new long[num];
for (int i = 0; i < arr.length; i++) {
arr[i]=sc.nextLong();
}
quickPX(arr);
long min = arr[1]-arr[0];
for (int i = 1; i < arr.length-1; i++) {
if (min>arr[i+1]-arr[i]) {
min=arr[i+1]-arr[i];
}
}
System.out.println((arr[num-1]-arr[0])/min+1);
}
}