洛谷 P1887

源题目:https://www.luogu.com.cn/problem/P1887

乘积最大3

题目描述

请你找出 \(M\) 个和为 \(N\) 的正整数,他们的乘积要尽可能的大。

输出字典序最小的一种方案。

输入格式

一行,两个正整数 \(N,M\)

输出格式

\(M\) 个和为 \(N\) 的,乘积尽可能的大的正整数。

样例 #1

样例输入 #1

6 3

样例输出 #1

2 2 2

提示

对于100%的数据,\(1 \le N \le 10^{9},1 \le M \le 10^{6}\)。数据保证 \(N \geq M\)

我的思路:

就是
思想很简单,因为你会发现,当两数之和相等时 ,他们越趋于平均乘积越大
The idea is simple, because you will find that when two numbers add up to the same thing, they tend to average out

代码思路

直接M/N,然后再N减去数组的总和,最后的N还剩多少数组就加多少个1
当然,题目要求了按字典序,那就从数组的最后开始+1

我的代码:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		/*
		 * in: 6 3
		 * 
		 * out: 2 2 2
		 * 
		 * M ge he is N
		 * 
		 * The idea is very simple
		 * 
		 * because you will find that when two numbers add up to the same thing
		 * 
		 * they tend to have a larger average product
		 */
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(), m = in.nextInt();
		int a[] = new int[m];
		for (int i = 0; i < m; i++) {
			a[i] = n / m;
		}
		n -= a[0] * m;
		int j = m - 1;
		while (n > 0) {
			a[j]++;
			n--;
			j--;
		}
		for (int i : a) {
			System.out.print(i + " ");
		}
		in.close();
	}
}


posted on 2022-10-10 22:17  Hex酱  阅读(92)  评论(0)    收藏  举报

导航