sicily 1029.Rabbit

题意:高精度加法和统计。归纳出公式:   1、当月份少于rabbit长成adult时,num[i] = num[i - 1] + 1;表示每个月都增加一对rabbit

2、当月份达到rabbit长成adult时,num[i] = num[i - 1] + num[i - m];表示第i个月的rabbit对数是上个月的rabbit加上本月刚成年的rabbit(刚成年等同于刚生下新兔子...)

#include 
#include 
#include 
#include 
using namespace std;




struct info
{
    string num;
};
info result[150];


void clear(string &s);
void reset(string &s1,string &s2)//补位,使两个字符串长度相等,并在前面补0
{
    string str1,str2;
    if(s1.length() > s2.length())
    {
        int size = s1.length() - s2.length();
        for(int i = 0;i <= size;i++)
        {
            str2.append("0");
        }
        str2.append(s2);
        str1.append("0");
        str1.append(s1);


        s1 = str1;
        s2 = str2;
    }
    else if(s1.length() < s2.length())
    {
        int size = s2.length() - s1.length();
        for(int i = 0;i <= size;i++)
        {
            str1.append("0");
        }
        str1.append(s1);
        str2.append("0");
        str2.append(s2);


        s2 = str2;
        s1 = str1;
    }
    else
    {
        str1.append("0");
        str2.append("0");
        str1.append(s1);
        str2.append(s2);


        s1 = str1;
        s2 = str2; 
    }
}


string addition(string result,string s)//加法
{
    reset(result,s);
    string temp = result;
    for(int i = 0;i < temp.length();i++)
        temp[i] = '0';
    for(int i = result.length() - 1;i >= 0;i--)
    {
        int a = result[i] - '0';
        int b = s[i] - '0';
        int c = temp[i] - '0';
        int m = a + b + c;
        if(m > 9)
        {
            int t1 = m - 10;
            temp[i] = t1 + '0';
            
            int t2 = temp[i - 1] - '0';
            t2++;
            temp[i - 1] = t2 +'0';
        }
        else
        {
            temp[i] = m + '0';
        }
    }
    //result = temp;
    clear(temp);
    return temp;
}


void clear(string &s)//清除字符串前面多余的0
{
    string temp = "";
    int count = 0;
    for(int i = 0;i < s.length();i++)
    {
        if(s[i] == '0')
            count++;
        else
            break;
    }
    temp = s.substr(count,s.length() - count);
    s = temp;
}




                            




int main()
{
    int m,d;//m表示每m个月rabbit长成adult,d表示输入的期限
    while(cin >> m >> d && m != 0 && d != 0)
    {
        for(int i = 0;i < 150;i++)
            result[i].num = "";


        result[0].num = "1";
        for(int i = 1;i <= d;i++)
        {
            if(i < m)//还没有rabbit长成adult
            {
                result[i].num = addition(result[i - 1].num,"1");
            }
            else//已经有rabbit长成adult
            {
                result[i].num = addition(result[i - 1].num,result[i - m].num);
            }
        }


        cout << result[d].num << endl;
    }
    return 0;
}



你可能感兴趣的:(ACM)