📑前言
本文主要是【算法】——蓝桥杯练习题(五)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
- 📑前言
- 2080.求和
- 2145.求阶乘
- 2140.星期计算
- 📑文章末尾
2080.求和
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Scanner;public class Main {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubStreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));sc.nextToken();int n = (int)sc.nval;int a[] = new int[n];long sum[] = new long[n+1];for(int i=0;i<n;i++) {sc.nextToken();a[i] = (int)sc.nval;}sum[0]=a[0];for(int i=1;i<n;i++) {sum[i]=sum[i-1]+a[i];}long res = 0;for(int i=0;i<n;i++) {res+=a[i]*(sum[n-1]-sum[i]);}System.out.println(res);}}
2145.求阶乘
import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);long n = sc.nextLong();long l = 1;long r = (long)9e18;while(l<r) {long mid = (r-l)/2+l;if(f(mid)>=n) {r = mid;}else {l = mid+1;}}if(f(l)==n) {System.out.println(l);}else {System.out.println(-1);}}public static long f(Long n) {long ans = 0;while(n>0) {ans+=n/5;n/=5;}return ans;}}
2140.星期计算
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {// TODO Auto-generated method stubint a = f(20, 22);System.out.println((a+6)%7==0?7:(a+6)%7);}public static int f(int a,int b) {int ans=1,t=a;for(int i=0;i<b;i++) {ans=a%7;a=ans*t;}return ans;}
}