码迷,mamicode.com
首页 > 其他好文 > 详细

计算1+11+111+1111+........

时间:2017-08-16 20:23:01      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:generated   func   eth   else   ret   ==   oid   string   print   

f(n)=f(n-1)+10^n;

 public static void main(String[] args) {
        int n=2017;
        long temp=1;
        for(int i=1;i<n;i++){
            temp+=func(i);
        }
        System.out.println(temp);
    }

    private static long func(int i) {
        // TODO Auto-generated method stub
        long sum;
        if(i==0){
            sum=1;
        }
        else{
            sum=(long) (Math.pow(10, i)+func(i-1));    
        }
        return sum;
    }

计算1+11+111+1111+........

标签:generated   func   eth   else   ret   ==   oid   string   print   

原文地址:http://www.cnblogs.com/blythe/p/7375277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!