图中显示了6个正方形,其边长为1、1、2、3、5、8。很容易看出这些方块的周长之和是:4*(1+1+2+3+5+8)=4*20=80

图中显示了6个正方形,其边长为1、1、2、3、5、8。很容易看出这些方块的周长之和是:4*(1+1+2+3+5+8)=4*20=80

当有n+1个正方形以与图中相同的方式排列时,你能给出矩形中所有正方形的周长之和吗?

可选文字

#提示:参见斐波那契序列

#参考:http://oeis.org/a00045

函数周长具有参数n,其中n+1是平方数(从0到n编号),并返回所有平方的总周长。

周长(5)应返回80

周长(7)应返回216

代码:

import org.apache.commons.lang3.StringUtils;

import java.math.BigInteger;

/**
 * @description:
 * @create: 2019/04/28 19:38
 */
public class SumFct1 {
    //方法一
    public static BigInteger perimeter1(BigInteger n) {

        BigInteger a = BigInteger.ZERO;
        BigInteger b = BigInteger.ONE;
        BigInteger c = BigInteger.ONE;
        BigInteger sum = BigInteger.ZERO;

        for(int i = 0; i <= n.intValue(); i++) {
            a = b;
            b = c;
            c = a.add(b);
            sum = sum.add(a);
        }

        return sum.multiply(BigInteger.valueOf(4));
    }

    //方法二,这个方法效率执行的比较慢,建议使用第一种
    public static BigInteger perimeter(BigInteger n) {
        String[] str={String.valueOf(1),String.valueOf(1),"2"};
        while (n.compareTo(BigInteger.valueOf(1))>0){
            n=n.subtract(BigInteger.valueOf(1));
            getData(str);
        }
        String res=multiply(str[2],"4");
        return new BigInteger(res);
    }

    public static String[] getData(String[] str){
       String sum="0";
        String s=str[2];
        for(int i=0;i<2;i++){
            sum=bigNumberPlus(sum,str[i]);
        }
        str[0]=str[1];
        str[1]=sum;
        s=bigNumberPlus(s,sum);
        str[2]=s;
        return str;
    }

    /**
     * 两个大数相乘
     * @param num1
     * @param num2
     * @return
     */
    public static String multiply(String num1, String num2) {
        //把字符串转成char数组
        char chars1[] = num1.toCharArray();
        char chars2[] = num2.toCharArray();
        //声明存放结果和两个乘积的容器
        int result[] = new int[chars1.length + chars2.length];
        int n1[] = new int[chars1.length];
        int n2[] = new int[chars2.length];
        //把char转换成int数组。
        for (int i = 0; i < chars1.length; i++) {
            n1[i] = chars1[i] - '0';
        }
        for (int j = 0; j < chars2.length; j++) {
            n2[j] = chars2[j] - '0';
        }
        //逐个相乘
        for (int i = 0; i < chars1.length; i++) {
            for (int j = 0; j < chars2.length; j++) {
                result[i + j] += n1[i] * n2[j];
            }
        }
        //从后往前满十进位
        for (int i = result.length - 1; i > 0; i--) {
            result[i - 1] += result[i] / 10;
            result[i] = result[i] % 10;
        }
        //转成string并返回
        String resultStr = "";
        for (int i = 0; i < result.length - 1; i++) {
            resultStr += "" + result[i];
        }
        return resultStr;
    }

    /**
     * 用字符串模拟两个大数相加
     * <a href="http://home.cnblogs.com/u/309701/" target="_blank">@param</a> n1 加数1
     * <a href="http://home.cnblogs.com/u/309701/" target="_blank">@param</a> n2 加数2
     * <a href="http://home.cnblogs.com/u/69429/" target="_blank">@return</a>   相加结果
     */
    public static String bigNumberPlus(String a, String b) {
        int lenA = a.length();
        int lenB = b.length();
        if(lenA > lenB) {
            b = StringUtils.leftPad(b, lenA, "0");
        } else {
            a = StringUtils.leftPad(a, lenB, "0");
        }

        int[] arrC = new int[a.length() + 1];

        for(int i = a.length()-1; i>=0; i--) {
            int ai = Integer.parseInt(a.charAt(i) + "" );
            int bi = Integer.parseInt(b.charAt(i) + "" );
            int ci = arrC[i+1];
            int t = ai + bi + ci;
            arrC[i+1] = t%10;
            arrC[i] = t/10;
        }

        StringBuffer res = new StringBuffer();
        for(int i = 0; i<arrC.length; i++) {
            if(i==0 && arrC[i]==0) continue;
            res.append(arrC[i]);
        }
        return res.toString();
    }

    public static void main(String[] args) {
        System.out.println(perimeter1(new BigInteger("522277")));
//        System.out.println(perimeter(BigInteger.valueOf(5)));
    }
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值