BZOJ 1263: [SCOI2006]整数划分

本文探讨了一种使用贪心算法和高精度计算方法来解决特定数学问题的策略,详细解释了如何通过尽量凑取3和不足时凑取2的方法来简化问题,并提供了实例代码实现。

/*
* 分析:
* 贪心+高精度。。。尽量凑3,不足凑2
*
* */

http://www.lydsy.com/JudgeOnline/problem.php?id=1263 

import java.util.Scanner;
import java.math.*;

public class Main{
	public static void main(String [] args){
		BigInteger ans;
		BigInteger th = BigInteger.valueOf(3);
		int n;
		
		Scanner cin = new Scanner(System.in);

		while(cin.hasNext()){
			n = cin.nextInt();
			if(n==1){
				System.out.println(1);
				System.out.println(1);
				continue;
			}
			ans = BigInteger.ONE;
			int three = 0;
			while(n>4){
				three ++;
				n -= 3;
			}
			if(n==4||n==2)
				ans = ans.multiply(BigInteger.valueOf(n));
			else if(n==3)
				three ++;
			for(int i=0;i<three;i++)
				ans = ans.multiply(th);
			String s = ans.toString();
			System.out.println(s.length());
			if(s.length()>100){
				for(int i=0;i<100;i++)
					System.out.print(s.charAt(i));
				System.out.println();
			}
			else
				System.out.println(ans);
		}
	}
}

  

 

转载于:https://www.cnblogs.com/yejinru/archive/2012/12/18/2823022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值