蓝桥杯 总结经典基础题型

 十进制转R进制

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

string tentoR(int x, int r) {
    if (x == 0) return "0"; // 如果x为0,直接返回"0"
    string s;
    while (x) {
        int num = x % r;
        if (num >= 10) s += 'A' + (num - 10); // 处理10及以上的数字转为字母
        else s += num + '0'; // 处理0-9的数字
        x /= r;
    }
    reverse(s.begin(), s.end()); // 反转字符串得到正确的顺序
    return s;
}

int main() {
    int n, r;
    cin >> n >> r;
    cout << tentoR(n, r);
    return 0;
}

计算一个数转为二进制后有几个1  

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int calebinone(int x)
{
	int cnt = 0;
	while (x)
	{
		cnt++;
		x &= x - 1;//每次将最后一个1变为0 
	}
	return cnt;
}

int main()
{
	int n;
	cin >> n;
	cout << calebinone(n);

}

异或(^)相同为0,不同为1

最大公约数和最小公倍数(递归版本)

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int gcd(int a, int b) // 最大公约数
{
    return b == 0 ? a : gcd(b, a % b);
}

int lcm(int a, int b) // 最小公倍数
{
    return a / gcd(a, b) * b;
}

int main()
{
    int a, b;
    cin >> a >> b;
    cout << "最大公约数是:" << gcd(a, b) << endl;
    cout << "最小公倍数是:" << lcm(a, b) << endl;
    return 0;
}

日期(闰年)

显示某一年的日历

#include<iostream>
using namespace std;

int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

// 检查是否为闰年
bool check(int year) {
    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        return true;
    return false;
}

int main() {
    int y;
    cin >> y;
    // 根据年份调整二月天数
    if(check(y))
        days[2] = 29;
    else
        days[2] = 28; // 确保处理多个年份时对于非闰年,二月的天数正确设置为28天

    for(int i = 1; i <= 12; i++) { // 十二个月
        for(int j = 1; j <= days[i]; j++) 
            cout << y << "年" << i << "月" <<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

了一li

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

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

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

打赏作者

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

抵扣说明:

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

余额充值