西工大noj(21,22)

前言:西工大noj刷题记录

           代码在CodeBlocks16.01环境下编译通过

21题-粒子裂变: 

/*
 *题目:有a和b两种粒子,每秒钟内一个a粒子可以裂变为3个b粒子,而一个b粒子可以裂变为1个a粒子和2个b粒子,若在t=0时刻中只有一个a粒子,求在t秒时产生的a粒子和b粒子数,输入t
 */

#include <iostream>

using namespace std;

int noj_021()
{
    int time;
    int aPart = 1, bPart = 0;
    int aPartNextSec = 1, bPartNextSec = 0;
    cout << "请输入时间t:";
    cin >> time;
    for (int i = 0; i < time; ++i)
    {
        aPart = aPartNextSec;
        bPart = bPartNextSec;
        aPartNextSec = bPart;
        bPartNextSec = aPart * 3 + bPart * 2;
    }

    cout << "a粒子的个数为:" << aPartNextSec << endl;
    cout << "b粒子的个数为:" << bPartNextSec;

    return 0;
}

22题-危险的组合(个人感觉难)参考了(https://blog.csdn.net/oldyang95/article/details/12978357):

/*
 *题目:有足够多的装有铅和铀的盒子,把n(n<=30)个盒子放成一行,至少有3个铀放在一起,有多少种方法?
 *分析:这一题我用的是递推的方法,
 *列出前几项a[]的结果:        0,0,1,3,8,20,47,107,238,……
 *      2的各次幂b[]:        0,0,1,2,4,08,16,032,064,……
 *    a[] - b[] = c[]:     0,0,0,1,4,12,31,075,174,……
 * c[]中C(n)-2*C(n-1)=d[]:  0,0,0,1,2,04,07,013,024,……
 * 规律:从d[]的第7项开始,d[n]=d[n-1]+d[n-2]+d[n-3];
                           b[n]=2^(n-3);
                           c[n]=d[n]+2*c[n-1];
                           a[n]=b[n]+c[n];
 */

#include <iostream>
#include <math.h>

using namespace std;

int noj_022()
{
    int a[32] = {0}, b[32] = {0}, c[32] = {0}, d[32] = {0}, numofTerm;
    cout << "请输入盒子个数:";
    cin >> numofTerm;
    d[4] = 1;
    d[5] = 2;
    d[6] = 4;
    b[3] = 1;
    b[4] = 2;
    b[5] = 4;
    b[6] = 8;
    c[4] = 1;
    c[5] = 4;
    c[6] = 12;

    switch(numofTerm)
    {
    case 0:
        cout << "0";
        return 0;
    case 1:
        cout << "0";
        return 0;
    case 2:
        cout << "0";
        return 0;
    case 3:
        cout << "1";
        return 0;
    case 4:
        cout << "3";
        return 0;
    case 5:
        cout << "8";
        return 0;
    case 6:
        cout << "20";
        return 0;
    }

    for (int i = 7; i < numofTerm + 1; ++i)
    {
        b[i] = pow(2, i - 3);
        d[i] = d[i-1] + d[i-2] + d[i-3];
        c[i] = d[i] + 2 * c[i-1];
        a[i] = b[i] + c[i];
    }

    cout << a[numofTerm];

    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值