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

2023/03/04刷题

时间:2023-03-04 23:12:01浏览次数:41  
标签:03 正数 cout 04 int long define include 刷题

C. Andrew and Stones

链接

C. Andrew and Stones

这个题还是比较有意思的,每天再补

A. Array

链接

A. Array

这个题比较好做可以发现要想条件成立的话,必须存在一个0.所以要么最大的数是0,要么最大的数是负数,而且数组中肯定有负数。因为任何数乘以0都是0,所以我们分情况来讨论。

如果最大值是正数的话,第1行取排序后第一个值,这样一定是负数,第二行取最后一个值.这样一定是正数.剩下的个第三行,第三行一定是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 = 100 ;

/*
2
10 8
1 1 1 5 2 4 4 8 6 7
*/
void solve() {
	int n;
	cin >> n;
	int a[N] = {0};
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);
	if (a[1] < 0) {
		cout << 1 << ' ' << a[1] << '\n'; //输出第一个负数
	}

	if (a[n] >= 1) { //如果最大的是正数
		cout << 1 << ' ' << a[n] << '\n'; //打印最大的正数
		cout << n - 2 << ' '; //剩下的都放在最后一行
		for (int i = 2; i <= n - 1; i++) {
			cout << a[i] << ' ';
		}
		cout << '\n';


	} else {
		cout << 2 << ' ' << a[n - 1] << ' ' << a[n - 2] << '\n'; //打印最后倒数第二和第三个负数
		cout << n - 3 << ' ' << a[n] << ' '; //打印剩下的n-3个数

		for (int i = 2; i <= n - 3; i++) {
			cout << a[i] << ' ';
		}
		cout << '\n';


	}





}
signed main () {
	std::ios::sync_with_stdio(false);

	solve();


	return 0;
}

B. Sifid and Strange Subsequences

链接

B. Sifid and Strange Subsequences

这个题看了题解才会,先对数组排序然后可以发现两个数最小的绝对值的差值肯定是排序后两个数之间,并且我们可以发现数组中最多只能有正整数(如果剩下的值的最小的差值比这个正数还大否则就不能包含正整数)

#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 = 100004 ;

/*
2
10 8
1 1 1 5 2 4 4 8 6 7
*/
void solve() {
	int n;
	cin >> n;
	int a[N] = {0};
	int zs = 0;
	int fs = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (a[i] <= 0) {
			fs++;
		}

		else if (a[i] > 0) {
			zs++;

		}

	}//统计正数和负数(包括0)

	int mmin = 9999999999;
	int res = 0;
	sort(a + 1, a + n + 1); //排序
	for (int i = 1; i <= n; i++) {
		if (a[i] > 0) {
			res = a[i]; //碰到正数保存下来然后退出
			break;
		} else {
			if (i != 1) { //不是正数和第一个,每次取两个元素的差值最小值
				mmin = min(abs(a[i] - a[i - 1]), mmin);
			}

		}
	}


	if (a[n] <= 0) { //最大的为0,直接输出n
		cout << n << '\n';
		return;
	}
	if (mmin >= res) { //最小差值大于第一个正数
		cout << fs + 1 << '\n'; //打印负数加1
	} else {
		//否则打印负数数量
		cout << fs << '\n';

	}









}
signed main () {
	std::ios::sync_with_stdio(false);

	int t;
	cin >> t;
	while (t--) {
		solve();

	}


	return 0;
}

B. Neighbor Grid

链接

B. Neighbor Grid

构造题,四个角构造为2,剩下的边缘构造为3,剩下的构造为4..如果初始时大于上面的数量打印no

#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 = 310;

void solve() {
	int a[N][N] = {0};
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];//输入数据

		}
	}
	//直接构造,大于对应数量打印no

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if ((i == 1 && j == 1) || (i == 1 && j == m) || (i == n && j == 1) || (i == n && j == m)) {
				if (a[i][j] > 2) {
					no;
					return;
				} else {
					a[i][j] = 2;
				}

			} else if (i == 1 || i == n || j == 1 || j == m) {
				if (a[i][j] > 3) {
					no;
					return;
				} else {
					a[i][j] = 3;

				}



			} else {
				if (a[i][j] > 4) {
					no;
					return;
				} else {
					a[i][j] = 4;


				}

			}
		}
	}
	yes;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cout << a[i][j] << ' ' ;
		}
		cout << '\n';//输出结果

	}






}
signed main () {
	int t;
	cin >> t;
	while (t) {
		solve();
		t--;
	}


	return 0;
}

A. Alyona and Numbers

链接

A. Alyona and Numbers

这个题用上午刚学的取余很好用,可以找到最多的可以组成的数量.最后打印结果

#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 st1[10] = {0};

	int st2[10] = {0};
	int n, m;
	cin >> n >> m;
	int a;
	for (int i = 1; i <= n; i++) {
		a = i % 5;
		st1[a]++;//统计第一个数组,余数为0,1,2,3,4的数量
	}
	for (int i = 1; i <= m; i++) {
		a = i % 5;
		st2[a]++;;//统计第二个数组,余数为0,1,2,3,4的数量
	}
	int ans = 0;
	ans = ans + st1[0] * st2[0]; //对第一个和第二个数组余数为0的元素进行只有组合
	ans = ans + st1[1] * st2[4]; //余数1,余数4的元素进行组合
	ans = ans + st1[2] * st2[3]; //余数2,3进行组合
	ans = ans + st1[3] * st2[2]; //3,2进行组合
	ans = ans + st1[4] * st2[1]; //4.1进行组合
	cout << ans << '\n';
//打印结果





}
signed main () {


	solve();



	return 0;
}

标签:03,正数,cout,04,int,long,define,include,刷题
From: https://www.cnblogs.com/harper886/p/17179451.html

相关文章

  • 18.JSR303数据校验
    以新增品牌接口为例接口代码展示   添加校验注解前端送的json对应BrandEntity,比如我们需要品牌的名称不能为空:  NotBlank注解表示不允许为null为空为纯空......
  • 每日算法 230304
    每日算法230304题目982.按位与为零的三元组给你一个整数数组nums,返回其中按位与三元组的数目。按位与三元组是由下标(i,j,k)组成的三元组,并满足下述全部条......
  • csp201803-2
    主要思路就是:如果存在位置相同的两球,那么其前进方向*-1;或者球在端点,*-1,按时间累加即可。#include<bits/stdc++.h>usingnamespacestd;inta[105];intflag[105];int......
  • ubuntu20.04部署Nessus Essentials 10.5
    下载nessus#curl--requestGET--url'https://www.tenable.com/downloads/api/v2/pages/nessus/files/Nessus-10.5.0-ubuntu1404_amd64.deb'--output'Nessus-10.5.0-......
  • day04 打卡24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 142.环形链表II
    day04打卡24.两两交换链表中的节点19.删除链表的倒数第N个节点142.环形链表II24.两两交换链表中的节点24题目链接1.第一次想的思路:使用count记录当前是第几个节点,......
  • ubuntu 22.04 配置 samba 服务
     通过在ubuntu系统中安装samba,使得windows10 能访问安装在vmware中的ubuntu文件。sudoaptinstallsamba#编辑samba配置文件:sudovim/etc/samba/smb......
  • 洛谷P1036
    P1036[NOIP2002普及组]选数典型的dfs,建议书写递归代码时层次应与形参列表中自己所标志的层次相对应,否则很容易混乱importjava.util.Scanner;publicclassP1036......
  • Unity &lsquo;xxx Class&rsquo; is missing the class attribute 'ExtensionOfNativeC
    Unity‘xxxClass’ismissingtheclassattribute'ExtensionOfNativeClass翻译:‘xxxClass’缺少类属性`ExtensionOfNativeClass`这个就是当脚本继承了MonoBehaiour,......
  • 洛谷P1030
    P1030[NOIP2001普及组]求先序排列思路:由后序遍历序列求出根由中序遍历序列求出左右子树递归上述12直到中序/后续遍历序列为空publicclassP1030{//已......
  • 2023年03月编程语言流行度排名
    点击查看最新编程语言流行度排名(每月更新)2023年03月编程语言流行度排名编程语言流行度排名是通过分析在谷歌上搜索语言教程的频率而创建的一门语言教程被搜索的次数越多......