蓝桥杯刷题记录:第七届C++省赛真题(待更新)

T1四平方和:(二分)

//二分写法 
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 5000010
using namespace std;
struct sum{
	int s,c,d;
	bool operator< (const sum &t) const{
		if(s!=t.s)
			return s<t.s;
		if(c!=t.c)
			return c<t.c;
		return d<t.d;
	}
}S[N];
int n,m;

int main()
{
	cin>>n;
	for(int c=0;c*c<=n;c++)
	{
		for(int d=c;d*d+c*c<=n;d++)
		{
			S[m++]={c*c+d*d,c,d};//暂存可行的cd方案 
		}
	}
	sort(S,S+m);
	for(int a=0;a*a<=n;a++)
	{
		for(int b=a;b*b+a*a<=n;b++)
		{
			int t=n-a*a-b*b;
			int l=0,r=m-1;
			while(l<r)//模板1 
			{//找最大的,所以我们要在[l,mid]取到mid 
				int mid=l+r>>1;
				if(S[mid].s>=t)
					r=mid;
				else
					l=mid+1;
			}
			if(S[l].s==t)
			{
				cout<<a<<" "<<b<<" "<<S[l].c<<" "<<S[l].d<<endl;
				return 0;
			} 
		}
	 } 
	return 0;
} 

哈希写法:

#include<stdio.h>
#include<iostream>
#include<cstring> 
#include<algorithm>
#include<tr1/unordered_map>
#define x first
#define y second
#define N 2500010
using namespace std;
using namespace std::tr1;
typedef pair<int,int> PII;
int n,m;
unordered_map<int,PII > s;//哈希表存,可以不存重复的 


int main()
{
	cin>>n;
	for(int c=0;c*c<=n;c++)
	{
		for(int d=c;d*d+c*c<=n;d++)
		{
			int t=c*c+d*d;
			if(s.count(t)==0)//没存过的话 
				s[t]={c,d};//凑成这个和t的两个数就是c,d字典序最小 
		}
	}
	for(int a=0;a*a<=n;a++)
	{
		for(int b=a;b*b+a*a<=n;b++)
		{
			int t=n-a*a-b*b;
			if(s.count(t))
			{
				cout<<a<<" "<<b<<" "<<s[t].x<<" "<<s[t].y<<endl;
				return 0;			
			}
		}
	 } 
	return 0;
 } 

### 蓝桥杯第十三届C++ B组真题解析 以下是关于蓝桥杯第十三届C++ B组的部分真题及其背景分析: #### 1. **小明计划** 问描述如下: 小明决定从下周一开始努力准备蓝桥杯。他计划周一至周五每天做 `a` 道目,周六和周日每天做 `b` 道目。请你帮小明计算,按照计划他将在第几天实现做数大于等于 `n` ? 此属于简单的模拟类问,主要考察循环结构的应用以及条件判断的能力[^3]。 ```cpp #include <iostream> using namespace std; int main() { int a, b, n; cin >> a >> b >> n; // 输入参数 int total = 0; // 当前总数 int day = 0; // 天数计数器 while (total < n) { // 循环直到满足条件 day++; if (day % 7 >= 1 && day % 7 <= 5) { // 周一到周五 total += a; } else { // 周六和周日 total += b; } } cout << day << endl; // 输出天数 return 0; } ``` --- #### 2. **X 进制减法** 目要求处理 X 进制下的大整数减法运算。具体来说,给定两个字符串形式的大整数 A 和 B,在 X 进制下完成 A-B 的操作并返回结果。该涉及高精度算法与进制转换的知识点[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; string subtract(string num1, string num2, int base); int main(){ int x; string s1,s2; cin>>s1>>s2>>x; if(s1.length()<s2.length()){ swap(s1,s2); } bool flag=false; if(s1.length()==s2.length()){ for(int i=0;i<s1.length();i++){ if((int)(s1[i]-'0')>(int)(s2[i]-'0')){ break; } else if((int)(s1[i]-'0')<(int)(s2[i]-'0')){ flag=true; break; } } } if(flag){ swap(s1,s2); cout<<'-'; } cout<<subtract(s1,s2,x)<<endl; } // 减法函数定义部分... ``` 上述代码片段展示了如何通过逐位比较来解决不同长度或相同长度情况下的数值大小关系,并调用辅助方法完成最终的减法逻辑。 --- #### 3. **其他典型目概述** 除了以上提到的具体试外,还有诸如动态规划、贪心策略等方面的考也频繁出现于近年比中。例如背包问变种、最短路径求解等问均体现了比向更复杂算法倾斜的趋势[^1]。 --- ### 总结 通过对历届尤其是近几届蓝桥杯事的研究可以发现,其命方向正逐步由基础编程能力测试转向更高层次的数据结构与算法综合运用层面转变。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值