FZU 1698 最大乘积
2014-10-20 22:49
183 查看
链接:http://acm.fzu.edu.cn/problem.php?pid=1698
Problem 1698 最大乘积
Accept: 244 Submit: 1020
一个正整数一般可以分为几个互不相同的自然数的和,如3=1+2,4=1+3,5=1+4=2+3,6=1+5=2+4,…。现在你的任务是将指定的正整数n分解成若干个互不相同的自然数的和,且使这些自然数的乘积最大。
只一个正整数n,(3<= n< 10000)。
第一行是分解方案,相邻的数之间用一个空格分开,并且按由小到大的顺序。 第二行是最大的乘积。
10
2 3 5
30
FOJ-2009年4月月赛 Coral
思路: 题目意思很明显,贪心,一个数分解的越多越好,因为a*b>=a+b(a>1&&b>1),贪心从分解2,直到无法再继续分解为止,剩下的值分别逆序一一累加到已分解的值上,最后因为最多可以分解到140多,所以需要用高精度乘法,嫌麻烦用java大数水过了,,
注意: 因为至少要分解成两个数,所以n=3和n=4时要特判一下,,最坑人的是java的换行符,在windows系统是"\r\n",在unix系统下才是"\n",PE根本停不下来,这让C++党情何以堪,,
Problem 1698 最大乘积
Accept: 244 Submit: 1020
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
一个正整数一般可以分为几个互不相同的自然数的和,如3=1+2,4=1+3,5=1+4=2+3,6=1+5=2+4,…。现在你的任务是将指定的正整数n分解成若干个互不相同的自然数的和,且使这些自然数的乘积最大。
Input
只一个正整数n,(3<= n< 10000)。
Output
第一行是分解方案,相邻的数之间用一个空格分开,并且按由小到大的顺序。 第二行是最大的乘积。
Sample Input
10
Sample Output
2 3 530
Source
FOJ-2009年4月月赛 Coral思路: 题目意思很明显,贪心,一个数分解的越多越好,因为a*b>=a+b(a>1&&b>1),贪心从分解2,直到无法再继续分解为止,剩下的值分别逆序一一累加到已分解的值上,最后因为最多可以分解到140多,所以需要用高精度乘法,嫌麻烦用java大数水过了,,
注意: 因为至少要分解成两个数,所以n=3和n=4时要特判一下,,最坑人的是java的换行符,在windows系统是"\r\n",在unix系统下才是"\n",PE根本停不下来,这让C++党情何以堪,,
import java.util.*; import java.math.BigInteger; public class Main{ public static void main(String[]args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ int n=scan.nextInt(); if(n==3){ System.out.print(1+" "+2+"\r\n"+2+"\r\n"); continue; } if(n==4){ System.out.print(1+" "+3+"\r\n"+3+"\r\n"); continue; } int ans[] = new int[200]; int tot=0,tmp=2; while(n-tmp>=0){ ans[tot++]=tmp; n-=tmp; tmp++; } while(n>0){ for(int i=tot-1;i>=0;--i){ ans[i]++; n--; if(n==0) break; } } BigInteger mul = new BigInteger("1"); for(int i=0;i<tot;++i){ mul=mul.multiply(BigInteger.valueOf(ans[i])); } for(int i=0;i<tot-1;++i){ System.out.print(ans[i]+" "); } System.out.print(ans[tot-1]+"\r\n"+mul+"\r\n"); } } }
相关文章推荐
- fzu1698最大乘积
- http://acm.fzu.edu.cn/problem.php?pid=1698 最大乘积
- FOJ--1698--最大乘积--解题报告(大数乘小数的问题)
- FOJ 1698 最大乘积
- FZU_1208_最大k乘积问题
- FZU_1208_最大k乘积问题
- 最大k乘积问题
- vijos p1347(最大乘积(整数划分?))(25—100分)
- 最大乘积(Maximum Product,UVa 11059)
- Vijos1347 乘积最大
- 算法提高 最大乘积
- poj 1698 Alice's Chance 拆点最大流
- 蓝桥杯练习系统算法提高—最大乘积
- acm题目及我的程序(4)——正整数n的加法组合的最大乘积(修改)
- fzu_noip 1033 (作业问题-拼最大的2,3,5倍数)
- Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)
- POJ 1698 Alice's Chance 最大流
- poj1698 [ Usaco2007 Feb ] --最大流
- 白书 7.1.2最大连续乘积
- UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)