第四周双周赛

目录

T1:重要的话说三遍

代码:

 T2:日期格式化

 思路:

代码:

T3:大笨钟

 思路:

代码:

T4:拯救外星人 

​编辑思路:

代码:

T5:个位数统计 

​编辑思路:

代码:

T6:正整数A+B

思路:

代码:

T7:打印沙漏

 思路:

          代码:

T8:机工士姆斯塔迪奥

思路:

代码:

T9:排座位

 思路:

代码

T10:名人堂与代金卷

 思路:

代码:

T11:包装机

 思路:

代码:

T12:愿天下有情人都是失散多年的兄妹

​思路:

代码:


T1:重要的话说三遍

 

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	cout << "I'm gonna WIN!" << endl;
	cout << "I'm gonna WIN!" << endl; 
	cout << "I'm gonna WIN!" << endl;
	return 0;
}

 T2:日期格式化

 思路:

利用getchar的简单应用

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	char x; string M, Y, D;
	int cnt = 0;
	while (true)
	{
		x = getchar();
		if (x == '\n')
		{
			break;
		}
		if (x == '-') 
		{
			cnt++;
			continue;
		}
		if (cnt == 0)
		{
			M += x;
		}
		else if(cnt==1)
		{
			D += x;
		}
		else
		{
			Y += x;
		}
	}
	cout << Y << "-" << M << "-" << D << endl;
	return 0;
	}
	

T3:大笨钟

 思路:

一道简单的模拟题,主要注意细节就可以了

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int cnt = 0;
	string h, m;
	while (true)
	{
		char x;
		x = getchar();
		if (x == '\n')
		{
			break;
		}
		if (x == ':')
		{
			cnt++;
			continue;
		}
		if (cnt == 0)
		{
			h += x;
		}
		else
		{
			m += x;
		}
	}
	int H = (h[0] - '0')*10 + (h[1] - '0');
	int M = (m[0] - '0')*10 + m[1] - '0';
	if (H < 12)
	{
		cout << "Only " << h << ":" << m << ".  Too early to Dang." << endl;
	}
	else if(H==12&&M==0)
	{
		cout << "Only " << h << ":" << m << ".  Too early to Dang." << endl;
	}
	else
	{
		for (int i = 1; i <= H - 12; i++)
		{
			cout << "Dang";
		}
		if(M)
		{
			cout << "Dang";
		}
	}
	return 0;
}

T4:拯救外星人 

思路:

简单的阶乘求解

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	int ans = a + b;
	long long res = 1;
	for (int i = 2; i <= ans; i++)
	{
		res *= i;
	}
	cout << res << endl;
	return 0;
}

T5:个位数统计 

思路:

利用string来存储输入,后挨个遍历判断即可

代码:

#include<bits/stdc++.h>
using namespace std;
map<int, int >mp;
int main()
{
	string s;
	cin >> s;
	for (int i = 0; i < s.size(); i++)
	{
		mp[s[i] - '0']++;
	}	
	for (int i = 0; i <= 9; i++)
	{
		if (mp[i] != 0)
		{
			cout << i << ":" << mp[i] << endl;
		}
	}
	return 0;
}

T6:正整数A+B

思路:

主要是注意B若为 9846 asd则后面应该输入为?,则前面输入A的时候可以用cin,但是后面输入B的时候应该用getline

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string A, B,C;
	bool flag_A = true, flag_B = true;
	cin >> A;
	char c;
	c = getchar();
	getline(cin, B);
	int a = 0, b = 0;
	if (A.size() <= 4)
	{
		for (int i = 0; i < A.size(); i++)
		{
			if (!flag_A)break;
			if (A[i] > '9' || A[i] < '0')
			{
				flag_A = false;
				break;
			}
			a += (A[i] - '0') * pow(10, A.size() - i - 1);
		}
		if (a > 1000||a==0)flag_A = false;
	}
	else
	{
		flag_A = false;

	}  
		for (int i = 0; i < B.size(); i++)
		{
			if (!flag_B)break;
			if (B[i] > '9' || B[i] < '0')

			{
				flag_B = false;
				break;
			}
			b += (B[i] - '0') * pow(10, B.size() - i - 1);
		}
		if (b > 1000||b==0)flag_B = false;
	if (flag_A)
	{
		cout << a;
	}
	else
	{
		cout << "?";
	}
	cout << " + ";
	if (flag_B)
	{
		cout << b;
	}
	else
	{
		cout << "?";
	}
	cout << " = ";
	if (flag_A && flag_B)
	{
		cout << a + b << endl;
	}
	else
	{
		cout << "?" << endl;
	}
	return 0;
}

T7:打印沙漏

 思路:

主要是根据等差数列求和可以求出最大沙漏的层数,后面可以分别打印上下两层

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	char c;
	cin >> n >> c;
	int h = sqrt((n + 1) / 2);    
	for (int i = 0; i < h; i++)    //打印上半层沙漏
	{
		for (int j = 0; j < i; j++)
			cout << " ";
		for (int j = 0; j < 2 * (h - i) - 1; j++)
			cout << c;
		cout << endl;
	}
	for (int i = 2; i <= h; i++)    //打印下半层沙漏
	{
		for (int j = 0; j < h - i; j++)
			cout << " ";
		for (int j = 0; j < 2 * i - 1; j++)
			cout << c;
		cout << endl;
	}
	cout << n - 2 * h * h + 1;
	return 0;
}

T8:机工士姆斯塔迪奥

思路:

主要是记录行和列有多少重合的单元,到时候剔除就好了,没有什么难度

代码:

#include<bits/stdc++.h>
using namespace std;
map<pair<int, int >, int >mp_x,mp_y;
int main()
{
	int n, m, Q;
	cin >> n >> m >> Q;
	long long all = n * m;
	int X = 0,Y=0;
	for (int i = 1; i <= Q; i++)
	{
		int x, y;
		cin >> x >> y;
		if (x == 0)//行
		{
			mp_x[{x, y}]++;
		}
		if (x == 1)//列
		{
			mp_y[{x, y}]++;
		}
	}
	long long ans = all - mp_x.size() * m - mp_y.size() * n + mp_x.size() * mp_y.size();
	cout << ans << endl;
	return 0;
}

T9:排座位

 思路:

考察并查集和map<pair<int ,int >,int >的利用,可以用并查集来判断俩者之间是否为朋友,由map来记录俩人是否为敌人。

代码

#include<bits/stdc++.h>
using namespace std;
int pre_F[1050];
map<pair<int, int >, int >mp;
int find_F(int x)
{
	return pre_F[x]== x ? x : find_F(pre_F[x]);
}
void unite_F(int x, int y)
{
	int fx = find_F(x); int fy = find_F(y);
	if (fx != fy)
	{
		pre_F[fx] = fy;	
	}
}
int main()
{
	int n, m, k;
	cin >> n >> m >> k;

	for (int i = 1; i <= n; i++)
	{
		pre_F[i] = i;
	}//初始化
	for (int i = 1; i <= m; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		if (a > b)
		{
			swap(a, b);
		}
		if (c == 1)
		{
			unite_F(a, b);
		}
		else
		{
			mp[{a, b}]++;
		}
	}
	while (k--)
	{
		int x, y; cin >> x >> y;
		if (x > y)swap(x, y);
		if (find_F(x) == find_F(y) && mp[{x, y}]==0)
		{
			cout << "No problem" << endl;
		}
		else if(find_F(x) != find_F(y)&& mp[{x, y}] == 0)
		{
			cout << "OK" << endl;
		}
		else if (find_F(x) == find_F(y) && mp[{x, y}] != 0)
		{
			cout << "OK but..." << endl;
		}
		else if(find_F(x) != find_F(y) && mp[{x, y}] != 0)
		{
			cout << "No way" << endl;
		}
	}
	return 0;
} 

T10:名人堂与代金卷

 思路:

主要是考察结构体和sort的使用

代码:

#include<bits/stdc++.h>
using namespace std;
struct student
{
	string s;
	int grade;
};
student a[10005];
int main()
{
	int n, G, k;
	cin >> n >> G >> k;
	long long ans = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].s >> a[i].grade;
		if (a[i].grade >= G)
		{
			ans += 50;
		}
		else if(a[i].grade<G&&a[i].grade>=60)
		{
			ans += 20;
		}
	}
	cout << ans << endl;
	sort(a + 1, a + 1 + n, [](student a, student b)
		{
			return a.grade > b.grade || a.grade == b.grade && a.s[0] < b.s[0]|| a.grade == b.grade && a.s[0] == b.s[0]&&a.s<b.s;
		});
	int cnt = 1, now = 1;
	while (cnt<=k)
	{
		int R = 0;
		while (true)
		{
			cout << cnt << " " << a[now].s << " " << a[now].grade << endl;
			if (a[now].grade != a[now +1].grade)
			{
				now++;
				break;
			}
			now++;
			R++;
		}
		cnt += R;
		cnt++;
	}
	return 0;
}

T11:包装机

 思路:

主要是考察queue和stack的运用,一道比较简单的模拟题,但是要注意队列是否为空等细节

代码:

#include<bits/stdc++.h>
using namespace std;
stack<char>S;
queue<char>Q[105];
int main()
{
	int n, m, s;
	cin >> n >> m >> s;
	int cnt = 1;
	for (int i = 1; i <= n; i++)
	{
		string c;
		cin >> c;
		for (int j = 0; j < m; j++)
		{
			Q[i].push(c[j]);
		}
	}
	while (true)
	{
		int x; cin >> x;
		if (x == -1)
		{
			break;
		}
		else if (x==0)
		{
			if (!S.empty())
			{
				cout << S.top();
				S.pop();
			}
			
		}
		else 
		{
			if (S.empty() || S.size() < s)
			{
					if (!Q[x].empty())
					{
						S.push(Q[x].front());
						Q[x].pop();
					}
			}
			else if(S.size()>=s)
			{
				if (!Q[x].empty())
				{
					cout << S.top();
					S.pop();
					S.push(Q[x].front());
					Q[x].pop();
				}
			}
		}

	}
	return 0;
}

T12:愿天下有情人都是失散多年的兄妹

思路:

考察dfs和vector存图,可以利用深搜查男女的祖上五代,用vis来判断是否有相同的人出现,如果有则是不可以的,同时在储存父母的时候也要记录性别,因为可能有再婚的情况存在。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<int >V[N];
bool vis[N];
char sex[N];
bool flag;
void dfs(int x, int num)
{
	if (num >= 4)
	{
		return;
	}
	for (int i = 0; i < V[x].size(); i++)
	{
		if (!vis[V[x][i]])
		{
			vis[V[x][i]] = true;
			dfs(V[x][i], num + 1);
		}
		else
		{
			flag = 1;
		}//表明有重复的说明是五代以内,是近亲
	}
}
int main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int name; cin >> name;
		char ch; cin >> ch;
		sex[name]=ch;
		int F, M;
		cin >> M >> F;
		if (F != -1)
		{
			V[name].push_back(F);
			sex[F] = 'F';
		}
		if (M != -1)
		{
			V[name].push_back(M);
			sex[M] = 'M';
		}
	}
	int k; cin >> k;
	while (k--)
	{
		int x, y;
		cin >> x >> y;
		if (sex[x] == sex[y])
		{
			cout << "Never Mind" << endl;
		}
		else
		{
			flag = 0;
			memset(vis, 0, sizeof(vis));
			vis[x] = vis[y] = 1;
			dfs(x, 0); dfs(y, 0);
			if (flag)cout << "No" << endl;
			else
			{
				cout << "Yes" << endl;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值