试题 历届真题 最大比例【第七届】【省赛】【B组】java 蓝桥杯

package zhenti;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Scanner;

public class zuidabili {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long n = scanner.nextLong();
		HashSet<Long> hashSet = new HashSet<>();
		for (int i = 0; i < n; i++) {
			hashSet.add(scanner.nextLong());
		}
		ArrayList<Long> arrayList = new ArrayList<>(hashSet);
		arrayList.sort(new Comparator<Long>() {
			@Override
			public int  compare(Long o1, Long o2) {
				// TODO Auto-generated method stub
				return o1.compareTo(o2);
			}
		});
		fenshu zhiFenshu = new fenshu(arrayList.get(0), arrayList.get(1)).huajian().qiuzhifenshu();
		long zuixiaogongyingshux;
		zuixiaogongyingshux = new fenshu(arrayList.get(0), arrayList.get(1)).huajian().qiuduishu(zhiFenshu);
		long temp;
		long size = arrayList.size();
		for (long i = 2; i < size; i++) {
			temp = new fenshu(arrayList.get((int) (i - 1)), arrayList.get((int) i)).huajian().qiuduishu(zhiFenshu);
			zuixiaogongyingshux = zuidagongyinshu(zuixiaogongyingshux, temp);
		}
		System.out.print((long)Math.pow(zhiFenshu.fenzi,zuixiaogongyingshux));
		System.out.print("/");
		System.out.print((long)Math.pow(zhiFenshu.fenmu, zuixiaogongyingshux));
		
	}
	
	public static long zuidagongyinshu(long a, long b) {
		long temp;
		if(a < b) {
			temp = a;
			a = b;
			b = temp;
		}
		while(a % b != 0) {
			temp = a  % b;
			a = b;
			b = temp;
		}
		return b;
	}
}

class fenshu{
	long fenzi;
	long fenmu;
	
	public fenshu(long fenzi, long fenmu) {
		this.fenmu = fenmu;
		this.fenzi = fenzi;
	}
	
	public fenshu huajian() {
		long beichushu = zuidabili.zuidagongyinshu(fenzi, fenmu);
		return new fenshu(fenzi / beichushu, fenmu / beichushu);
	}
	
	public fenshu qiuzhifenshu() {
		long a = this.fenzi;
		long b = this.fenmu;
		long i;
		long j;
		for (i = 2; i <= a; i++) {
			if (a % i == 0) {
				break;
			}
		}
		for (j = 2; j <= b; j++) {
			if (b % j == 0) {
				break;
			}
		}
		long k;
		long i2 = 1;
		long j2 = 1;
		while(Math.pow(i, i2) != a) {
			i2++;
		}
		while(Math.pow(j, j2) != b) {
			j2++;
		}
		
		if (i2 >= j2) {
			k = i2 / j2;
			return new fenshu((long)Math.pow(i, k), j);
		}else {
			k = j2 / i2;
			return new fenshu(i, (long)Math.pow(j, k));
		}
	}
	
	public long qiuduishu(fenshu zhifenshu) {
		long i = 1;
		while(Math.pow(zhifenshu.fenzi, i) != this.fenzi) {
			i++;
		}
		return i;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值