第五届河南省程序设计大赛 B 最强DE 战斗力

将一个数字尽可能多分成3相乘的形式,余下来的乘2,注意ans会超过longlong,我用的java

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
    public static void main (String[] args)
    {
        // your code goes here
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n =sc.nextInt();
            if(n==1){System.out.println(1);continue;}
            if(n==2){System.out.println(2);continue;}
            int cnt1=n/3;int cnt2=n%3;
            BigInteger ans=new BigInteger("1");
            if(n>=3)
            {
                BigInteger b=new BigInteger("3");
                BigInteger c=new BigInteger("2");
                BigInteger e=new BigInteger("4");
                if(cnt2==2||cnt2==0)
                {
                    for(int i=1;i<=cnt1;i++)
                        ans=ans.multiply(b);
                    if(cnt2==2) ans=ans.multiply(c);
                }
                else
                {
                    for(int i=1;i<cnt1;i++)
                        ans=ans.multiply(b);
                    ans=ans.multiply(e);
                }
            }
            System.out.println(ans);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值