蓝桥杯解析
子串分值和-第十一届蓝桥杯
子串分值和【第十一届】【省赛】
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int sum = 0;
Scanner scanner = new Scanner(System.in);
char[] str = scanner.next().toCharArray();
int j,k=0;
for(int i = 0; i<str.length;i++) {
for(j = i+1;j<str.length;j++) {
if(str[i] == str[j]) break;
}
for(k = i-1;k>=0;k--) {
if(str[i] == str[k])break;
}
sum += (i-k)*(j-i);
}
System.out.println(sum);
}
}
会问日期-第十一届蓝桥杯
import java.util.Scanner;
public class Main {
static int[] mo = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
boolean flag = false;
for(int i = num+1;i<=99999999;i++) {
String data = Integer.toString(i);
int year = i/10000;
int month = i%10000/100;
int day = i%100;
if(check(year, month, day) && huiwen(data)) {
if(!flag) {
System.out.println(data);
flag = true;
}
if(ABAB(data)) {
System.out.println(data);
break;
}
}
}
}
public static boolean ABAB(String data) {
char[] str = data.toCharArray();
return str[0] == str[2] && str[1] == str[3];
}
public static boolean huiwen(String data) {
StringBuffer stringBuffer = new StringBuffer(data);
String string = stringBuffer.reverse().toString();
return data.equals(string);
}
public static boolean check(int year,int month,int day) {
if (month>12||month<1)
return false;
if(year % 400 == 0 ||(year % 4 ==0 && year%100 !=0)) {
mo[2] = 29;
}else mo[2] = 28;
return day <= mo[month] && day >= 1;
}
}
成绩分析-第十一届蓝桥杯
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
int maxNum = Integer.MIN_VALUE;
int minNum = Integer.MAX_VALUE;
float sum = 0;
for(int i =0;i<n;i++) {
nums[i] = scanner.nextInt();
}
for(int i =0;i<n;i++) {
maxNum = Math.max(maxNum,nums[i]);
minNum = Math.min(minNum,nums[i]);
sum += nums[i];
}
System.out.println(maxNum);
System.out.println(minNum);
System.out.printf("%.2f",sum/n);
}
}
时间显示-第十二届蓝桥杯
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
n = n/1000;
n = n %(24*60*60);
long h = n/3600;
long m = n/60%60;
long s = n%60;
System.out.printf("%02d:",h);
System.out.printf("%02d:",m);
System.out.printf("%02d",s);
}
}
双向排序-第十二届蓝桥杯
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
Integer[] num = new Integer[n];
for(int i = 0;i<n;i++) num[i] = i+1;
for(int i=0;i<m;i++) {
int control = scanner.nextInt();
int index = scanner.nextInt();
MyComparator cmpComparator = new MyComparator();
if(control == 0) {
Arrays.sort(num, 0, index,cmpComparator);
}else {
Arrays.sort(num,index-1,num.length);
}
}
for(int a :num) {
System.out.print(a+" ");
}
}
}
class MyComparator implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
if(o1 > o2) return -1;
else if(o1 < o2) return 1;
else return 0;
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Set<Integer> set = new HashSet<>();
int n = scan.nextInt();
int[] nums = new int[n];
for(int i = 0; i<n;i++) {
nums[i] = scan.nextInt();
}
set.add(0);
for(int i = 0;i<n;i++) {
ArrayList<Integer> list = new ArrayList<>(set);
for(int k:list){
set.add(k+nums[i]);
set.add(Math.abs(k-nums[i]));
}
}
set.remove(0);
System.out.println(set.size());
}
}
货物摆放-第十二届蓝桥杯
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
long n = 2021041820210418l;
Set<Long> set = new HashSet<>();
for(long i =1;i<=Math.sqrt(n);i++) {
if(n%i==0) {
set.add(i);
if(n/i!=i) set.add(n/i);
}
}
int counrt = 0;
ArrayList<Long> list = new ArrayList<Long>(set);
for(long i = 0;i<set.size();i++) {
for(long j = 0;j<set.size();j++) {
for(long k = 0;k<set.size();k++) {
if(list.get((int) i)*list.get((int) j)*list.get((int) k) == n) counrt ++;
}
}
}
System.out.println(counrt);
}
}
直线-第十二届蓝桥杯
import java.util.*;
public class Main {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
HashSet<String> hashSet = new HashSet<>();
int x = 0;
int y = 0;
for(x=0;x<20;x++) {
for(y=0;y<21;y++) {
set.add(x*100+y);
}
}
List<Integer> list = new ArrayList<>(set);
for(int i=0;i<set.size();i++) {
int x1 = list.get(i)/100;
int y1 = list.get(i)%100;
for(int j = i+1;j<set.size();j++) {
int x2 = list.get(j)/100;
int y2 = list.get(j)%100;
int kup = y1-y2;
int kdown = x1-x2;
if(kdown == 0) {
hashSet.add("x=" + x1);
continue;
}
int kgcd = gcd(kup, kdown);
kup = kup/kgcd;
kdown = kdown/kgcd;
int bup = kdown*y1-kup*x1;
int bdown = kdown;
int bgcd = gcd(bup, bdown);
bup = bup/bgcd;
bdown = bdown/bgcd;
hashSet.add(kup+"/"+kdown+"x"+"+"+bup+"/"+bdown);
}
}
System.out.println(hashSet.size());
}
public static int gcd(int a,int b) {
if(b == 0) return a;
return gcd(b, a%b);
}
}
Fibonacci 数列与黄金分割-第十届
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double n = sc.nextInt();
if (n <= 21) {
double x = f(n);
double y = f(n+1);
double ratio =(double)(x/y);
System.out.println(String.format("%.8f", ratio));
}else {
System.out.println(0.61803399);
}
}
public static double f(double n) {
if (n == 1 || n == 2) {
return 1;
}
return f(n-1)+f(n-2);
}
}
平方和-第十届
import java.util.*;
public class Main {
public static void main(String[] args) {
HashSet<Character> set = new HashSet<>();
HashSet<Integer> set2 = new HashSet<>();
set.add('0');
set.add('1');
set.add('2');
set.add('9');
for(int i = 1; i<=2019;i++) {
String s = Integer.toString(i);
for(int j = 0; j<s.length();j++) {
if (set.contains(s.charAt(j))) {
count++;
set2.add(i);
break;
}
}
}
long sumh = 0l;
List<Integer> list = new ArrayList<>(set2);
for(int i = 0; i<list.size();i++) {
int temp = list.get(i);
sumh += temp*temp;
}
System.out.println(sumh);
}
}
数列求值-第十届
import java.util.*;
public class Main {
public static void main(String[] args) {
long[] dp = new long[20190324];
dp[0] = 1;
dp[1] = 1;
dp[2] = 1;
for(int i = 3;i<20190324;i++) {
dp[i] = dp[i-3]+dp[i-2]+dp[i-1];
dp[i] = dp[i] % 10000;
}
System.out.println(dp[20190323]);
}
}
完全二叉树的权值
package hello;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
for(int i = 0;i<n;i++) {
nums[i] = scanner.nextInt();
}
int res = Integer.MIN_VALUE;
int depth = 0;
int count = 0;
int index = 0;
boolean flag = true;
while(flag) {
depth ++;
int sum = 0;
for(int j = 0; j<Math.pow(2,depth-1);j++) {
if(index >= n) {
flag = false;
break;
}
sum += nums[index++];
}
if(res < sum) {
res = sum;
count = depth;
}
}
System.out.println(count);
}
}
外卖优先级-第十届
package hello;
import java.util.*;
public class Main {
static Scanner in = new Scanner(System.in);
static int n, m, t;
static Map<Integer, ArrayList<Integer>> map = new TreeMap<Integer, ArrayList<Integer>>();
static int result;
public static void main(String[] args) {
n = in.nextInt();
m = in.nextInt();
t = in.nextInt();
for (int i = 1; i <= m; ++i) {
int time = in.nextInt();
int id = in.nextInt();
if (map.containsKey(id)) {
map.get(id).add(time);
} else {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(time);
map.put(id, temp);
}
}
ArrayList<Map.Entry<Integer, ArrayList<Integer>>> list = new ArrayList<Map.Entry<Integer, ArrayList<Integer>>>(
map.entrySet());
for (int i = 0; i < list.size(); ++i) {
Map.Entry<Integer, ArrayList<Integer>> entry = list.get(i);
ArrayList<Integer> list2 = entry.getValue();
int level = 0;
int[] count = new int[t + 2];
boolean flag = false;
for (int j = 0; j < list2.size(); ++j)
count[list2.get(j)]++;
for (int j = 1; j <= t; ++j) {
if (count[j] == 0) {
if (level > 0)
level--;
if (level <= 3)
flag = false;
} else {
level += count[j] * 2;
if (level > 5)
flag = true;
}
}
if (flag)
result++;
}
System.out.println(result);
}
}
修改数组-第十届
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
for(int i = 0; i<n;i++) {
nums[i] = scanner.nextInt();
}
HashSet<Integer> set = new HashSet<>();
set.add(nums[0]);
for(int i = 1; i<n;i++) {
while(set.contains(nums[i])) {
nums[i] ++;
}
set.add(nums[i]);
}
for(int num:nums) {
System.out.println(num);
}
}
}
import java.util.Scanner;
public class Main {
static int[] a = new int[100010];
static int[] f = new int[100010];
static int n;
static Scanner cin = new Scanner(System.in);
public static void main(String[] args) {
for (int i = 1; i <= 100009; i++) {
f[i] = i;
}
n = cin.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = cin.nextInt();
a[i] = find(a[i]);
f[a[i]] = find(a[i] + 1);
}
for (int i = 1; i <= n; i++) {
System.out.print(a[i] + " ");
}
}
static int find(int x) {
if (f[x] == x) {
return x;
} else {
f[x] = find(f[x]);
return f[x];
}
}
}
分数-第九届蓝桥杯
public class Main {
public static void main(String[] args) {
System.out.println("1048575/524288");
}
}
星期一-第九届蓝桥杯
public class Main {
public static void main(String[] args) {
int count = 0;
for(int i = 1901;i<2001;i++) {
if(i % 400 == 0 || (i % 4 == 0 && i %100 != 0)) {
count ++;
}
}
int sum = (100-count)*365 + count*366;
int total = sum/7;
System.out.println(total);
}
}
3
17:48:19 21:57:24
11:05:18 15:14:23
17:21:07 00:31:46
23:02:41 16:13:20
10:19:19 20:41:24
22:19:04 16:41:09
航班时间-第九届蓝桥杯
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws ParseException {
int n = sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++) {
long t1 = getTime();
long t2 = getTime();
long t = (t1+t2)/2;
System.out.printf("%02d:%02d:%02d\n",t/3600,t/60%60,t%60);
}
}
private static long getTime() throws ParseException {
String line = sc.nextLine();
String[] split = line.split(" ");
SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
Date t1 = sd.parse(split[0]);
Date t2 = sd.parse(split[1]);
int day=0;
if(split.length==3) {
day = Integer.parseInt(split[2].substring(2, 3));
}
return day*24*60*60 + t2.getTime()/1000 - t1.getTime()/1000;
}
}
|