📑前言
本文主要是【算法】——蓝桥杯练习题(十)的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见文章来源:https://www.toymoban.com/news/detail-813169.html
1181.数的幂次
package 蓝桥杯第十一次;
import java.util.Scanner;
public class 数的幂次 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-->0) {
System.out.println(f(sc.nextLong(),sc.nextLong(),sc.nextLong()));
}
}
public static long f(long a,long b,long p) {
long ans = 1;
while(b>0) {
if((b&1)!=0) ans=ans*a%p;
a=a*a%p;
b=b>>1;
}
return ans;
}
}
120.最大比例
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();
long a[] = new long[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextLong();
}
Arrays.sort(a);
long b[] = new long[n];//分子
long c[] = new long[n];//分母
int cnt = 0;
for(int i=1;i<n;i++) {
long tt = gcd(a[i-1], a[i]);
b[cnt]=a[i]/tt;
c[cnt]=a[i-1]/tt;
cnt++;
}
long zi = b[0];
long mu = c[0];
for(int i=1;i<cnt;i++) {
zi=fast(zi, b[i]);
mu = fast(mu, c[i]);
}
System.out.println(zi+"/"+mu);
}
public static long fast(long a,long b) {
if(a<b) {
long temp = a;
a = b;
b = temp;
}
if(b==1) {
return a;
}
return fast(b, a/b);
}
public static long gcd(long a,long b) {
return b==0?a:gcd(b, a%b);
}
}
1276.小明的彩灯
差分
package 蓝桥杯第十一次;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class 小明的彩灯1 {
/*
5 3
2 2 2 1 5
1 3 3
4 5 5
1 1 -100
0 5 5 6 10
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
sc.nextToken();
int n = (int)sc.nval;
sc.nextToken();
int q = (int)sc.nval;
long a[] = new long[n+1];
long d[] = new long[n+1];
for(int i=1;i<=n;i++) {
sc.nextToken();
a[i] = (long)sc.nval;
d[i] = a[i] - a[i-1];
}
while (q-->0) {
sc.nextToken();
int l=(int)sc.nval;
sc.nextToken();
int r=(int)sc.nval;
sc.nextToken();
long x = (long)sc.nval;
d[l]+=x;
if(r+1<n+1) {
d[r+1]-=x;
}
}
for(int i=1;i<=n;i++) {
a[i] = a[i-1]+d[i];
}
for(int i=1;i<=n;i++) {
if(a[i]<0) {
a[i]=0;
}
System.out.print(a[i]+" ");
}
}
}
17162.小明的衣服
package 蓝桥杯第十三次;
import java.util.PriorityQueue;
import java.util.Scanner;
public class 小明的衣服 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
PriorityQueue<Long> p = new PriorityQueue<>();
while(n-->0) {
p.add(sc.nextLong());
}
long sum=0;
while(p.size()!=1) {
long a = p.poll();
long b = p.poll();
sum+=a+b;
p.add(a+b);
}
System.out.println(sum);
}
}
📑文章末尾
文章来源地址https://www.toymoban.com/news/detail-813169.html
到了这里,关于蓝桥杯练习题(十)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!