蓝桥杯练习题(十)

发布时间:2024年01月19日

📑前言

本文主要是【算法】——蓝桥杯练习题(十)的文章,如果有什么需要改进的地方还请大佬指出??

🎬作者简介:大家好,我是听风与他🥇
??博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见

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://blog.csdn.net/weixin_61494821/article/details/135686530
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。