首页 > 其他分享 >2023/03/05刷题

2023/03/05刷题

时间:2023-03-05 19:23:31浏览次数:49  
标签:03 cout 奇数 int 05 long define include 刷题

链接

A. Domino

这个题还是比较有意思的.我们可以统计左边奇数的数量和右边奇数的数量,然后还需要统计一下左边和右边奇偶性不同的个数.(因为这样的一对数才能翻转.)最后综合考虑一下

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 104;

void solve() {
	int n;
	cin >> n;

	int odd_1 = 0;//统计左边奇数的数量
	int odd_2 = 0;//统计右边奇数的数量
	int cnt = 0;//统计奇偶不同的一对数的数量
	int  a, b;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b;
		if (a % 2 == 1) {
			odd_1++;
		}
		if (b % 2 == 1) {
			odd_2++;
		}
		if (a % 2 != b % 2) {
			cnt++;
		}
	}
	if ((odd_1 + odd_2) % 2 == 1) {//如果一共有奇数个奇数不可能把两行都变成偶数
		cout << -1 << '\n';
		return;
	}
	if (odd_1 % 2 == 0 && odd_2 % 2 == 0) {//如果有左边有偶数个奇数并且右边有偶数个奇数//不需要翻转
		cout << 0 << '\n';
		return;
	}
	if (odd_1 % 2 == 1 && odd_2 % 2 == 1 && cnt >= 1) {
		//如果左边有奇数个奇数右边有奇数个奇数,并且cnt>=1,只需要翻转一次
		cout << 1 << '\n';
	} else {//如果cnt=0的话不可能进行翻转打印-1
		cout << -1 << '\n';

	}





}
signed main () {

	solve();



	return 0;
}

A. Great Sequence

链接

A. Great Sequence

这个题先把每个数有多少个存下来.然后用一个循环让每个数除以k,看看有没有除以k的数字如果有把这两个数字进行配对,之后对剩下的数字每个都配对一个新的数字就可以了,最后打印出来结果

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;
const int N = 2 * 100000 + 5;
int a[N] = {0};

void solve() {
	map <int, int> st;//用map来存每个数的个数,用数组会超时
	int n, k;
	scanf("%lld %lld", &n, &k);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
		st[a[i]]++;//统计每个数字的数量
	}
	sort(a + 1, a + n + 1);//排序保证大的在后面
	for (int i = 1; i <= n; i++) {
		//如果a[i]是k的倍数并且st里面有a[i]/k这个key值,并且该值大于0的话
		if (a[i] % k == 0 && st.find(a[i] / k) != st.end() && st[a[i] / k] >= 1) {
			st[a[i]]--;//让a[i]值减一
			st[a[i] / k]--;//让a[i]/k的值减一
		}
	}
	int ans = 0;
	//对于剩下的数字有几个就给它配对几个最后打印出来
	for (auto it = st.begin(); it != st.end(); it++) {
		ans = ans + it->second;
	}
	printf("%lld\n", ans);
	/*
	2
	5 3
	5 2 3 5 15
	*/



}
signed main () {
	int t;
	scanf("%lld", &t);
	while (t--) {


		solve();
	}


	return 0;
}
/*
2
20 2
13 2 8 12 15 11 2 4 19 6 13 2 14 8 9 12 10 7 16 5
*/

A. Prefix and Suffix Array

链接

A. Prefix and Suffix Array

这个题比较简单,我们对于n为偶数的字符串我们直接将两个长度为n/2的字符拼接起来然后判断其是不是回文就可以,对于n为奇数的字符串中间那个字符是多少并不重要,我们还是只需要把长度为n/2(下取整)的字符串拼接起来判断一下就可以

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;


void solve() {
	int n;
	cin >> n;
	int len = n / 2;
	vector<string> s;//储存两个n/2的字符串
	string x;
	for (int i = 1; i <= 2 * n - 2; i++) {
		cin >> x;
		if (x.size() == len) {
			s.push_back(x);
		}
	}
	string res = s[0] + s[1];//拼接两个字符串
	int flag = 1;//1为是,0为不是
	for (int i = 0; i < len; i++) {//判断两个字符串是不是回文
		if (res[i] != res[res.size() - i - 1]) {
			flag = 0;
			break;

		}

	}//打印
	if (flag == 1) {
		yes;
	} else {
		no;

	}


}
signed main () {
	int t;
	scanf("%lld", &t);
	while (t--) {


		solve();
	}


	return 0;
}
/*
2
20 2
13 2 8 12 15 11 2 4 19 6 13 2 14 8 9 12 10 7 16 5
*/

4867.整除数

链接

4867.整除数

看一下代码里面的注释

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no 	cout<<"NO"<<'\n'

using namespace std;

signed main () {
	int n, k;
	cin >> n >> k; //读入n和k
//先让n+1除以k向上取整,然后在乘以k就可以得到严格大于n的一个最近的k的倍数
	int res = ((n + 1 + k - 1) / k) * k;
	cout << res << '\n';




	return 0;
}
/*
2
20 2
13 2 8 12 15 11 2 4 19 6 13 2 14 8 9 12 10 7 16 5
*/

标签:03,cout,奇数,int,05,long,define,include,刷题
From: https://www.cnblogs.com/harper886/p/17181340.html

相关文章

  • Day03.mysql高级_函数和索引
    day03-mysql高级学习网站:索引高级:https://www.bilibili.com/video/BV1MS4y1T7uJ?from=search&seid=5032261320934971179&spm_id_from=333.337.0.0hashmap:https://www.......
  • Day03.JDBC
    第一章JDBC1、JDBC的概念目标能够掌握JDBC的概念能够理解JDBC的作用讲解客户端操作MySQL数据库的方式使用第三方客户端来访问MySQL:SQLyog、Navicat使用MySQ......
  • 已拦截跨源请求:同源策略禁止读取位于 http:// 的远程资源。(原因:CORS 头缺少 'Access-C
    这个报错提示是因为在同源策略下,JavaScript代码试图跨域访问另一个源(例如不同的域名、协议或端口)的资源,而服务器未设置CORS头部信息。同源策略是一个重要的安全特性,用于保......
  • git clone的时候出现出现 fatal: unable to access 'https://github.com/...':OpenSSL
    一般发生这种事故因为代理在git种配置的,既然它是https代理(而不是http)gitconfighttp.proxy和gitconfig--globalhttp.proxy也无济于事。解决方案一 1、先看看......
  • #yyds干货盘点#【愚公系列】2023年03月 ASP.NET Core下Worker Service构建系统服务实
    前言当你想到ASP.NETCore时,可能会想到Web应用程序后端代码,包括MVC和WebAPI。MVC视图和Razor页面还允许使用后端代码生成带有HTML元素的前端UI。全新的Blazor更进一步,允许......
  • LG-P3603 雪辉 题解
    LG-P3603雪辉Solution目录LG-P3603雪辉Solution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面给定$n$个点的树,存在点权,多次询问每次......
  • 杂题小记(2023.03.01)
    杂题小记(2023.03.01)目录杂题小记(2023.03.01)更好的阅读体验戳此进入[ARC084D]SmallMultiple题面SolutionCodeLG-P2371[国家集训队]墨墨的等式题面SolutionCodeLG-P2158......
  • 联想(Lenovo)小新15IIL2020款i5-1035G电脑 Hackintosh 黑苹果efi引导文件
    原文来源于黑果魏叔官网,转载需注明出处。硬件型号驱动情况主板联想(Lenovo)小新15IIL2020款i5-1035G处理器英特尔酷睿i5-1035G1已驱动内存16GBLPDDR4X3200MHz已驱动硬盘金......
  • 2023/03/04刷题
    C.AndrewandStones链接C.AndrewandStones这个题还是比较有意思的,每天再补A.Array链接A.Array这个题比较好做可以发现要想条件成立的话,必须存在一个0.所以......
  • 18.JSR303数据校验
    以新增品牌接口为例接口代码展示   添加校验注解前端送的json对应BrandEntity,比如我们需要品牌的名称不能为空:  NotBlank注解表示不允许为null为空为纯空......