蓝桥杯真题(2题)

2024蓝桥杯真题:

  • 好数
#include<iostream>
using namespace std;

const int N = 1e7 + 10;
int n, a,sum;

int main()
{
    cin >> n;
    for(int a = 1 ; a <= n; a++)
    {
        int tmp = 0, flag = 1, ans = a;
        //注意不要直接更改a的值
        for (int i = 1; i <= 7 && ans != 0; i++)
        {
            tmp = ans % 10;
            ans = ans / 10;
            //cout << tmp <<" "<< a << endl;
            if (i % 2 == 0 && tmp % 2 == 0 || i % 2 != 0 && tmp % 2 != 0)
            {
                flag = 0;
            }
            else
            {
                flag = 1;
                break;
            }
        }
        if (!flag) sum++;
    }
    cout << sum << endl;

    return 0;
}
  • R格式
#include <iostream>
#include <string>
using namespace std;

const int N = 10000;
int n, a[N] ,cnt=0, lon;//lon记录小数点的位置
string d;

void cheng()
{
    //做2的乘法
    for (int i = 0; i < cnt; i++)
    {
        a[i] *= 2;
    }
    //考虑进位
    for (int i = 0; i < cnt-1; i++)
    {
        a[i + 1] += a[i] / 10;
        a[i] %= 10;
    }
    //处理最高位的进位
    if (a[cnt-1] >= 10)
    {
        a[cnt] = a[cnt-1] / 10;
        a[cnt-1] %= 10;
        cnt++;
    }
}

int main()
{
    cin >> n >> d;
    //存储到数组中
    for (int i = d.size()-1; i >=0 ; i--)
    {
        if (d[i] != '.')
            a[cnt++] = d[i]- '0';//3.14存入输出4 1 3
        else
        {
            lon = d.size() - i - 1;//小数点并没有存到数组中
        }
    }
    for (int i = 1; i <= n; i++)
        cheng();
    //四舍五入
    if (a[lon-1] >= 5)//如果小数点后第一位大于5,就进位
    {
        a[lon] += 1;
    }
    for (int i = lon; i < cnt-1; i++)
    {
        a[i + 1] += a[i] / 10;
        a[i] %= 10;
    }
    if (a[cnt - 1] >= 10)
    {
        a[cnt] = a[cnt - 1] / 10;
        a[cnt - 1] %= 10;
    }
    for (int i = cnt-1 ; i >= lon; i--)
        cout << a[i];
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值