dp+java:uva11375

來源:互聯網
上載者:User

http://acm.hust.edu.cn/vjudge/contest/127206#problem/L
題目大意:
用火柴拼數字,告訴你每一個數字所需要的火柴數,問你用N根可以拼出多少個數字,包括一位及多位,並且不可以有前置字元為零

由於題目結果會很大用java大數

代碼是在已拼好的的數字之後添加新的數字,所以第一個數字不可以為零即i和j不可以同時為零
然後在輸出結果的時候,若果N大於等於6加上單專屬零的情況

import java.util.Scanner;  import java.math.*;  public class Main  {      public static void main(String[] args)      {        Scanner sc=new Scanner(System.in);        BigInteger []dp=new BigInteger[2001];          BigInteger []ans=new BigInteger[2001];          int []c=new int[11];          c[0]=6;        c[1]=2;        c[2]=5;        c[3]=5;        c[4]=4;        c[5]=5;        c[6]=6;        c[7]=3;        c[8]=7;        c[9]=6;        for(int i=1;i<2001;i++)            dp[i]=BigInteger.ZERO;        dp[0]=BigInteger.ONE;        for(int i=0;i<2001;i++)        {            for(int j=0;j<=9;j++)            {                if((i!=0||j!=0)&&(i+c[j])<=2000)                {                    dp[i+c[j]]=dp[i+c[j]].add(dp[i]);                }            }        }        ans[0]=BigInteger.ZERO;        for(int i=1;i<=2000;i++)            ans[i]=ans[i-1].add(dp[i]);        while(sc.hasNext())        {            int n=sc.nextInt();            if(n<6)                System.out.println(ans[n]);            else                System.out.println(ans[n].add(BigInteger.ONE));        }         sc.close();    }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.