ACM-ICPC基本算法之递推法

image-20200714095815945

#include <iostream>
using namespace std;
int main()
{
    int i,n,preNum = 1,curNum = 1,temp;
    cin >> n;
    for(i = 3;i <= n;i++)
    {
        temp = curNum;
        curNum = curNum + preNum;
        preNum = temp;
    }
    cout << curNum << endl;
    return 0;
}

image-20200714100118446

#include <iostream>
using namespace std;
int gcd(int a, int b)
{
    while(b>0)
    {
        int temp = a % b;
        a = b;
        b = temp;
    }
    return a;
}
int main()
{
    int m,n;
    cin >> m >> n;
    cout << gcd(m,n) << endl;
    return 0;
}

image-20200714100353426

#include <iostream>
using namespace std;
int main()
{
    int i,n;
    while(cin >> n && n){
        int sum = 1;
        for(i = 2;i <= n;i++)
            sum = (sum+1)*2;
        cout << sum << endl;
    }
    return 0;
}

image-20200714100735084

#include <iostream>
using namespace std;
int main()
{
    int i,j,n,a[35][35];
    cin >> n;
    for(i = 0;i < n;i++)
    {
        a[i][0] = 1;
        a[i][i] = 1;
        for(j = 1;j < i;j++)
            a[i][j] = a[i-1][j-1] + a[i-1][j];
    }
    for(i = 0;i < n;i++)
    {
        for(j = 0;j <= i;j++)
            cout << a[i][j];
        cout << endl;
    }
    return 0;
}

image-20200714101316475

#include <iostream>
using namespace std;
int main()
{
    int x,k=1;
    double oil,dis = 500;
    cin >> x;
    if(x <= 500)
        oil = x;
    else
    {
        do
        {
            ++k;
            dis += 500.0/(2*k-1);
            oil = 500*k;
        }
        while(dis<x);
        oil = 500*k + (x-dis)*(2*k-1);
    }
    printf("%.2f\n",oil);
    return 0;
}

image-20200714101722316

image-20200714101819934

#include <iostream>
using namespace std;
int main()
{
    int n,i;
    long long a[51];
    a[1] = 3;
    a[2] = 6;
    a[3] = 6;
    for(i=4;i<51;i++)
        a[i] = 2*a[i-1]+a[i-1];
    while(cin >> n && n)
        cout << a[n] << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不能say的秘密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值