本题要用高精度,表示不会,上网找的高精度模板。。。
本题dp【i】表示用i个火柴所能组成的数的个数,
核心代码:
for(i=1;i<=9;i++)//0不能当第一个数,所以没赋初值
dp[a[i]]=dp[a[i]]+one;//先赋初值,one是高精度的1.
for(i=1;i<=2000;i++)
for(j=0;j<=9;j++)
dp[i+a[j]]=dp[i+a[j]]+dp[i];//递推求
dp[6]=dp[6]+one;//单独将0这个数的情况 加上1.
for(i=1;i<=2000;i++)
dp[i]=dp[i]+dp[i-1];//由于火柴可以不全用,所以最后腰累加起来
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
using namespace std;
const int maxn = 1000;
struct bign //上网找的高精度的模板,目前水平还不会用c++编高精度。。
{
int len, s[maxn]; //s是逆序存储
bign(){ memset(s, 0 , sizeof(s)); len = 1 ; } //构造函数
bign(int num) { *this = num ; } //初始化
bign(const char* num) { *this = num ; }
string str() const
{
int i;
string res = "";
for( i = 0 ; i < len ; i++ )
res = (char)( s[i] + '0' ) + res ;
if (res == "") res = "0";
return res;
}
bign operator = (const char* num)
{
int i;
len = strlen(num);
for( i = 0 ; i < len ; i++ )
s[i] = num[len-i-1] - '0' ;
return *this;
}
bign operator = (int num)
{
char s[maxn];
sprintf(s , "%d" , num);
*this = s;
return *this;
}
bign operator + (const bign& b) const
{
bign c;
c.len = 0;
int i, g;
for( i = 0 , g = 0 ; g || i < ( len > b.len ? len : b.len ) ; i++ )
{
int x = g ;
if( i < len ) x += s[i] ;
if( i < b.len ) x += b.s[i] ;
c.s[c.len++] = x % 10 ;
g = x / 10 ;
}
return c;
}
//比较的前提是两个数都没有前导零
bool operator < (const bign& b) const
{
if( len != b.len ) return len < b.len ;
int i;
for( i = len-1 ; i >= 0 ; i-- )
if( s[i] != b.s[i] ) return s[i] < b.s[i];
return false;
}
bool operator > (const bign& b) const { return b < *this; }
bool operator <= (const bign& b) const { return !(b < *this); }
bool operator >= (const bign& b) const { return !(*this < b); }
bool operator != (const bign& b) const { return b < *this || *this < b; }
bool operator == (const bign& b) const { return !(b < *this) || !(*this < b); }
};
istream& operator >> (istream &in , bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream &out , const bign& x)
{
out << x.str();
return out;
}
int n;
bign dp[2020];
int a[12]={6,2,5,5,4,5,6,3,7,6};
int main()
{
int i,j;
bign one=1;
for(i=1;i<=9;i++)
dp[a[i]]=dp[a[i]]+one;
for(i=1;i<=2000;i++)
for(j=0;j<=9;j++)
dp[i+a[j]]=dp[i+a[j]]+dp[i];
dp[6]=dp[6]+one;
for(i=1;i<=2000;i++)
dp[i]=dp[i]+dp[i-1];
while(scanf("%d",&n)!=EOF)
cout<<dp[n]<<endl;
}