首页 > 其他分享 >2023/02/24刷题

2023/02/24刷题

时间:2023-02-24 22:55:37浏览次数:58  
标签:02 24 fs int res k2 刷题 include zs

B. Maximum Product

链接

B. Maximum Product

这个题因为只取5个数字,所以我们直接枚举5个数字的全部情况取出最大值就可以了

#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;
const int ll_MAX = -9223372036854775807; //定义最小值

bool cmp(int a, int b) {
	return a > b;//从小到大排序

}
signed main () {
	ios::sync_with_stdio(false);
	int zs[N] = {0}, fs[N] = {0};
	//分成5种情况取最大值
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;

		int k1 = 0, k2 = 0;

		for (int i = 0; i < n; i++) {
			int a;
			cin >> a;
			if (a >= 0) {
				zs[k1++] = a;


			} else {
				fs[k2++] = a;
			}
			//分别统计正数包括0的个数和负数的个数


		}
		int res = -9223372036854775807;
		sort(fs, fs + k2);//负数从小到大排序
		sort(zs, zs + k1, cmp);//正数从大到小排序
		if (k1 >= 5) { //全是正数
			res = max(res, zs[0] * zs[1] * zs[2] * zs[3] * zs[4]);//取5个正数的最大值


		}
		if (k1 >= 4 && k2 >= 1) {//结果为负数,乘以负数的最大值,才能更大
			res = max(res, zs[0] * zs[1] * zs[2] * zs[3] * fs[k2 - 1]);

		}
		if (k1 >= 3 && k2 >= 2) {//结果为正数乘以负数的最小值
			res = max(res, zs[0] * zs[1] * zs[2] * fs[0] * fs[1]);
		}
		if (k1 >= 2 && k2 >= 3) {//结果为负数,乘以负数的最大值
			res = max(res, zs[0] * zs[1] * fs[k2 - 1] * fs[k2 - 2] * fs[k2 - 3]);
		}

		if (k1 >= 1 && k2 >= 4) {//结果为正,乘以负数最下值
			res = max(res, zs[0] * fs[0] * fs[1] * fs[2] * fs[3]);
		}


		if (k2 >= 5) {//,结果为负数,乘以负数最大值
			res = max(res, fs[k2 - 5] * fs[k2 - 4] * fs[k2 - 1] * fs[k2 - 2] * fs[k2 - 3]);
		}
		cout << res << '\n';//打印



	}



	return 0;
}

C. Double-ended Strings

链接

C. Double-ended Strings

枚举较短的那个全部的子串,然后直接长串里面寻找.找到一个更新一下最大值

#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 * 100005;

signed main () {
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		string a, b;
		cin >> a >> b;
		if (a.size() > b.size()) {
			swap(a, b);//让a始终较小
		}
//		cout<<a<<'\n';
		int res = 0;
//		if(strstr(b.c_str(),a.c_str())!=NULL){
//			res=a.size();
//		}
		for (int i = 0; i < a.size(); i++) {
			for (int j = i + 1; j <= a.size(); j++) {
				string temp = a.substr(i, j - i);//枚举每一个字串
//			cout<<temp<<'\n';
				if (strstr(b.c_str(), temp.c_str()) != NULL) {
					res = max(res, j - i);//看一下能不能更新最大值

				}

			}
		}
//打印结果

		cout << (b.size() - res) + (a.size() - res) << '\n';



	}





	return 0;
}

B. Captain Flint and a Long Voyage

链接

B. Captain Flint and a Long Voyage

这个题还是比较有意思的可以发现9是1001而8是1000然后我们去掉最后n位数,可以发现我们让其全部等于就可以出现最大值,但是因为最后几位是要去除的但是我们要保证位数不变所以我们最后是8和9都可以,说以我们让n/4(4为1000和1001的长度)上取整,就可以得出有多少个8可以替换9

直接打印

#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 * 100005;

signed main () {
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int x = (n + 4 - 1) / 4;//上取整

		int cnt;
		cnt = n - x;//cnt储存有几个9
		while (cnt--) {
			cout << 9;//打印9
		}
		cnt = x;//cnt现在储存有几个`8

		while (cnt--) {
			cout << 8;//打印


		}

		cout << '\n';








	}





	return 0;
}

C. Building Permutation

链接

C. Building Permutation

排序之后直接贪心一下,累加到res里面然后打印结果

#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 = 3 * 100005;
int a[N];
signed main () {
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int res = 0;
	sort(a + 1, a + 1 + n);//排序一下
	for (int i = 1; i <= n; i++) {
		//和当前的i进行比较
		if (a[i] > i) {//如果a[i]大于i加上差值
			res = res + a[i] - i;
		} else {
			res = res + i - a[i];//小于1的时候也加上差值
		}

	}
	cout << res;//打印结果







	return 0;
}

A. Jeff and Digits

链接

A. Jeff and Digits

这个题我们需要知道一个规律

image-20230224224133452

然后如果1-n个5加起来是9的倍数的话,将剩下的0放到最后就是最大

如果1-n个5加起来都不是9的倍数,打印-1,

如果没有5的倍数,但是有0打印0

#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 = 3 * 100005;
signed main () {
	ios::sync_with_stdio(false);
	int n;
	int a = 0, b = 0;
	int x;
	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> x;
		if (x == 5) {
			a++;
		} else {
			b++;
		}
	}
	//统计0和5的个数
	if (b == 0) {//0一个都没有打印-1
		cout << -1 << '\n';
		return 0;
	}
	int cnt = 0;//初始化为0
	for (int i = 1; i <= a; i++) {
		if ((i * 5) % 9 == 0) {//查找全部任意的5加起来有没有9的倍数
			cnt = max(cnt, i);//更新cnt
		}
	}
	if (cnt == 0) {//cnt没被更新打印0
		cout << 0 << '\n';

	} else {
		//否则打印结果

		while (cnt--) {
			cout << 5;

		}
		while (b--) {
			cout << 0;
		}

	}




	return 0;
}

标签:02,24,fs,int,res,k2,刷题,include,zs
From: https://www.cnblogs.com/harper886/p/17153455.html

相关文章

  • 2月24日总结
    所花时间:10节课,学习概率论,计算机网络代码量:0行今日的课非常多,原本是12节课,整整一天都有课,庆幸的是下午前两节老师有事情取消了上课,让我今天有了一丝丝喘息的机会,写了写作......
  • 今日总结2023/02/23
    今日学习了按钮控件的制作1.新建module2.找到activity_main.xml(首先项目内要有TextView)<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://......
  • 2023.2.24 日寄
    2022.2.23-24日寄一言沅有芷兮澧有兰,思公子兮未敢言。——屈原《九歌·湘夫人》模拟赛波鱼与N≡N争霸模拟赛「超!原!」模拟赛分治题目「CF1100F」IvanandBurgers......
  • 2023/2/24每日随笔
    今天,上了一天的课,很累,也收获了不少,英语口语课上的很有意思,老师听不懂我,我听不懂老师,自己写的句子里有着很多熟悉但写不出来的单词,比如population,reduce,common,relieve,receiv......
  • 路飞day_02
    目录今日内容详细一、企业项目类型二、软件开发流程三、路飞项目需求四、pip永久换源五、虚拟环境六、路飞项目前后端创建七、包导入八、后端项目调整目录今日内容详细一......
  • 2.24博客
    学生信息增删改查学生信息增删改查几天改改写写差不多了,报错很多,改来改去也是很麻烦,这一个错哪一个错。添加操作还是找人问的,自己看了半天改不对。navicat:  eclips......
  • 0224模拟赛(超!原!)
    \(~~~~\)经历了前两天答辩的洗礼现在如沐春风啊。大梦的曲调/「POI2011」LIZ-Lollipop题意\(~~~~\)长度为\(n\)的只含\(1,2\)的序列,\(q\)次询问求是否存在一......
  • 2023.2.24
    2023.2.24数学考试(考试时长:3小时,4道题,得分170)T1BSGS板子题,写出正解且用对拍验证,但是使用map实现,被卡掉3个点,之后应注意题目数据范围和时限,选用恰当的算法。T2KMP字符......
  • 从0到1一步一步玩转openEuler--24 openEuler管理进程-调度启动进程
    操作系统管理多个用户的请求和多个任务。大多数系统都只有一个CPU和一个主要存储,但一个系统可能有多个二级存储磁盘和多个输入/输出设备。操作系统管理这些资源并在多个用户......
  • 2023年,测试人尽量还是别裸辞了吧···
    -你知道什么叫度日如年吗?就是在家待业的每一天。-你知道什么叫心焦如焚吗?就是投出100份简历却等不来一个回应。当前就业环境,裁员、失业消息满天飞,好像能有一份工作就......