📑前言
本文主要是【算法】——蓝桥杯练习题(一)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
- 📑前言
- [252.查找两个总和为特定值的索引 - 蓝桥云课 (lanqiao.cn)]
- 720.[世纪末的星期 - 蓝桥云课 (lanqiao.cn)]
- [614.第几天 - 蓝桥云课 (lanqiao.cn)]
- [1463.货物摆放 - 蓝桥云课 (lanqiao.cn)]
- [2080.求和 - 蓝桥云课 (lanqiao.cn)]
- [315.寻找 3 个数的最大乘积 - 蓝桥云课 (lanqiao.cn)]
- 📑文章末尾
[252.查找两个总和为特定值的索引 - 蓝桥云课 (lanqiao.cn)]
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;public class Main {
/*
6
1 2 3 -2 5 7
7*/public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int n=sc.nextInt();int num[] = new int[n];for(int i=0;i<n;i++) {num[i]=sc.nextInt();}int target = sc.nextInt();Map<Integer, Integer> map = new HashMap<>();int a[]=new int[2];for(int i=0;i<n;i++) {map.put(num[i], i);}//索引最小,只需要保证第一个数最小for(int i=0;i<n;i++) {int num1=target-num[i];if(map.containsKey(num1)) {System.out.println(i+" "+map.get(num1));break;}else {map.put(num[i], i);}}}}
720.[世纪末的星期 - 蓝桥云课 (lanqiao.cn)]
import java.util.Calendar;public class Main {public static void main(String[] args) {// TODO Auto-generated method stub//创建一个日历类Calendar calendar = Calendar.getInstance();//星期日用1来表示
// calendar.set(2000, 0,2);
// System.out.println(calendar.get(Calendar.DAY_OF_WEEK));int i=0;for(i=1999;;i+=100) {calendar.set(i, 11,31);//注意,月份从0开始表示,0表示1月if(calendar.get(Calendar.DAY_OF_WEEK)==1) {break;}}System.out.println(i);}}
[614.第几天 - 蓝桥云课 (lanqiao.cn)]
import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);System.out.println(2*31+1*30+4+29);}}
[1463.货物摆放 - 蓝桥云课 (lanqiao.cn)]
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);List<Long> list = new ArrayList<>();long ans=0;long n=2021041820210418L;//依次遍历,不可能出现重复的情况for(long i=1;i<=n/i;i++) {if(n%i==0) {list.add(i);list.add(n/i);}}for(Long i:list) {for(Long j:list) {for(Long k:list) {if(i*j*k==n) {ans++;}}}}System.out.println(ans);}}
[2080.求和 - 蓝桥云课 (lanqiao.cn)]
import java.util.Scanner;public class Main {
/*
4
1 3 6 9
117*/
//注意数据范围,本题sum数组使用longpublic static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int n=sc.nextInt();long ans=0;int a[]=new int[n+1];long sum[]=new long[n+1];for(int i=1;i<=n;i++) {a[i]=sc.nextInt();}//累积和for(int i=1;i<=n;i++) {sum[i]=a[i]+sum[i-1];}for(int i=1;i<=n;i++) {ans+=a[i]*(sum[n]-sum[i]);}System.out.println(ans);}}
[315.寻找 3 个数的最大乘积 - 蓝桥云课 (lanqiao.cn)]
import java.util.Arrays;
import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int n=sc.nextInt();int a[] = new int[n];int k=0,t=0;long ans=0;for(int i=0;i<n;i++) {a[i]=sc.nextInt();if(a[i]<0) {k++;}else {t++;}}Arrays.sort(a);//负数要么2个,要么0个,才能保证最后的乘积最大ans = Math.max(a[0]*a[1]*a[n-1], a[n-1]*a[n-2]*a[n-3]);System.out.println(ans);}}