📑前言
本文主要是【算法】——蓝桥杯练习题(四)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
- 📑前言
- 192.等差数列
- 739.棋盘放麦子
- 2218.数数
- 1284.求解质因子
- 1151.最小质因子之和
- 📑文章末尾
192.等差数列
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
int b[] = new int[n-1];
for(int i=0;i<n-1;i++) {
b[i]=a[i+1]-a[i];
}
int g = b[0];
for(int i=0;i<n-1;i++) {
g = gcd(g, b[i]);
}
if(b[0]==0) {
System.out.println(n);
}else {
System.out.println((a[n-1]-a[0])/g+1);
}
}
public static int gcd(int a,int b) {
return b==0?a:gcd(b, a%b);
}
}
739.棋盘放麦子
package 蓝桥杯第四次;
import java.math.BigInteger;
public class 棋盘放麦子 {
/*
18446744073709551615
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger a = new BigInteger("1");
BigInteger ans = new BigInteger("0");
BigInteger t = new BigInteger("2");
for(int i=0;i<64;i++) {
ans=ans.add(a);
a=a.multiply(t);
}
System.out.println(ans);
}
}
2218.数数
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long sum = 0;
for(int i=2333333;i<=23333333;i++) {
sum+=zys(i);
}
System.out.println(sum);
}
public static int zys(int n) {
int cnt = 0;
int bak = n;
for(int i=2;i*i<=n;i++) {
while(bak%i==0) {
cnt++;
bak/=i;
}
}
if(bak > 1) {
cnt++;
}
return cnt==12?1:0;
}
}
1284.求解质因子
package 蓝桥杯第四次;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class 质因数个数 {
/*
396
3
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
System.out.println(zys(n));
}
public static int zys(long n) {
int cnt = 0;
long bak = n;
for(long i=2;i*i<=n;i++) {
if(bak%i==0) {
cnt++;
}
while(bak%i==0) {
bak/=i;
}
}
if(bak > 1) {
cnt++;
}
return cnt;
}
}
1151.最小质因子之和
package 蓝桥杯第四次;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Scanner;
public class 最小质因子之和 {
/*
3
5
10
15
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
f();
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
sc.nextToken();
for(int i=2;i<N;i++) {
ans[i]=ans[i-1]+pre1[i];
}
int n = (int)sc.nval;
int a[] = new int[n];
for(int i=0;i<n;i++) {
sc.nextToken();
a[i]=(int)sc.nval;
System.out.println(ans[a[i]]);
}
}
static int N=4000000;
static int pre[] = new int[N];
static int pre1[] = new int[N];
static int ans[] = new int[N];
static boolean isp[] = new boolean[N];
public static void f() {
for(int i=2;i<N;i++) {
if(isp[i]==false) {
pre1[i]=i;//2-n中最小的素数约数记录在pre1中
pre[++pre[0]]=i;
}
for(int j=1;j<pre[0]+1&&i*pre[j]<N;j++) {
//prime[j]就是i*prime[j]的最小素因子
pre1[i*pre[j]]=pre[j];
isp[i*pre[j]]=true;
if(i%pre[j]==0) break;
}
}
}
}