2018 蓝桥杯A组C++/C程序设计

这篇文章包含了几段关于编程问题的代码示例,涉及计算最大公约数(GCD)的不同方法,处理日期(如计算一年中星期一的次数)以及解决航班时间间隔的计算。此外,还有一个关于识别和计数二维网格中未被水包围的岛屿的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分数

#include<iostream>
#include<algorithm>
using namespace std;
int gcd(int a, int b)
{
	for(int i = b; ; i++)
	{
		if(!(i%a) && !(i%b)) return i;
	}
		
}
int gcd1(int a, int b)
{
	while(a%b)
	{
		int r = a%b;
		a = b;
		b = r;
		cout << a << " " << b << " " << r << endl;
	}
	return b;
}
int main()
{
	int up = 1, down = 1;
	for(int i = 2; i <= 20; ++i)
	{
		int u = 1, d = pow(2, i-1);	//第二项的分子和分母
		int g = gcd(down, d);	//最大公倍数
		up = up * (g/down) + u * (g/d);
		down = g;
	}
	cout << gcd1(3, 7) << endl;
	int c = gcd1(up, down);
	cout << up/c << " " << down / c;
	return 0;
}

星期一

#include<iostream>
using namespace std;
int main()
{
	int sum = 0;
	int t = 6;
	for(int i = 2000; i >= 1901; --i)
	{
		//366
		if((i%400==0) || (i%4==0 && i%100!=0))
		{
			
			int year = 366;
			year = year -  t;
		//	cout << year << endl;
			while(year - 7 >= 0)
			{
				sum++;
				year = year - 7;
				//cout << year << endl;
			}
			t = 7 - year;
		//	cout << "t" << " " << t << endl;
			sum++;
		}
	
		//365
		else
		{
			int year = 365;
			year -= t;
			//cout << year << endl;
			while(year - 7 >= 0)
			{
				sum++;
				year = year - 7;
			//	cout << year << endl;
			}
			t = 7 - year;
		//	cout << "t" << " " << t << endl;
			sum++;
		}
		
	}
	if(t==0) sum--; //判断1901.1.1是否为星期一,题目不包括这个范围 
	cout << sum - 1 << endl;
	return 0;
}

航班时间

#include<iostream>
using namespace std;
int t;
int n;
int get_time()
{
	int h1, m1, s1, h2, m2, s2, d = 0;
	scanf("%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1,  &h2, &m2, &s2, &d);
	int time1 = d * 24 * 3600 + (h2 - h1)*3600 + (m2 - m1) * 60 + s2 - s1;
	return time1;
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; ++i)
	{
		int time1 = get_time();
		int time2 = get_time();
		t = (time1 + time2)/2;
		printf("%02d:%02d:%02d\n",t/3600, t/60%60, t%60);
	}
	
	return 0;
}

全球变暖

#include<iostream>
#include<queue>
using namespace std;
const int N = 1005;
char a[N][N];
bool stu[N][N];
int n;
int sum, ans;//统计为沉默岛数量
queue<pair<int, int>> q;
void bfs(int x, int y)
{
	q.push({x, y});
	stu[x][y] = true;
	bool trag = true;
	while(!q.empty())
	{
		int x = q.front().first;
		int y = q.front().second;
		//cout << x << " " << y << endl;
		q.pop();
		bool flag = true; //为沉默岛屿标记 
		//上 下 左 右 
		int dx[4] = {-1, 1, 0, 0};
		int dy[4] = {0, 0, -1, 1};
		for(int i = 0; i < 4; ++i)
		{
			if((x + dx[i]) >= 0 && (x + dx[i]) < n && (y + dy[i]) >= 0 && (y + dy[i]) < n)
			{
				//寻找邻近岛屿
				if(a[x+dx[i]][y+dy[i]]=='#' && !stu[x+dx[i]][y+dy[i]])
				{
					//进队列 
					q.push({x+dx[i], y+dy[i]});
					stu[x+dx[i]][y+dy[i]] = true;
				}
				//判断岛屿是否有海 
				if(a[x+dx[i]][y+dy[i]]=='.') flag = false;
				//cout << flag << endl;
			}	
		}
		//判断岛是否被完全淹没 
		if(flag) trag = false;
		//cout << trag << endl; 
	}
	ans++;
	if(!trag)
	{
		sum += 1;
		//cout << x << " " << y << endl;
	}
}
int main()
{
	cin >> n;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j)
			cin >> a[i][j];
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j)
		{
			if(!stu[i][j] && a[i][j] == '#')
			{
				//cout << i << " " << j << endl;  
				bfs(i, j);
			}
				
		}
	cout << ans - sum << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

亚磊敲代码

随缘

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

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

打赏作者

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

抵扣说明:

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

余额充值