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

2023/03/09刷题

时间:2023-03-12 19:47:35浏览次数:49  
标签:03 cout int 09 long define ans include 刷题

链接

B - Equal Rectangles

这个题还是比较有意思的因为有4n个,我们可以发现我们如果把序列排序的话必然有两个数字肯定是一模一样的,因为是长方形的两个边,我们还可以发现排序之后第一个元素和倒数第一个元素相乘一定等于第二个元素和倒数第二个元素相乘,如果不相等之际打印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 = 500;

void solve() {
	int a[N] = {0};
	int n;
	cin >> n;
	for (int i = 1; i <= 4 * n; i++) {
		cin >> a[i];//读入数据
	}
	int flag = 1;//标志变量
	sort(a + 1, a + 4 * n + 1);//先排序
	vector <int> res;

	vector <int> b;

	for (int i = 2; i <= 4 * n; i = i + 2) {
		if (a[i] != a[i - 1]) {//判断排序之后相邻的两个数是不是一样的如果不一样打印no
			flag = 0;
			no;
			return;
		}


	}
	for (int i = 1; i <= 4 * n; i = i + 2) {
		b.push_back(a[i]);//因为有两个数是重复的所以我们只让一个数进入b数组
	}
	int j = b.size() - 1;
	for (int i = 0; i < j; i++, j--) {//i和j分别指向第一个和最后一个,向前遍历
		if (res.empty() == true) {//如果res为空直接放入
			res.push_back(b[i]*b[j]);

		} else {
			if (res.back() != b[i]*b[j]) {//后面每次判断等不等前面的那个
				flag = 0;//不等于flag=0
				break;
			} else {
				res.push_back(b[i]*b[j]);//否则将元素加入数组
			}


		}


	}


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

	}






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


	return 0;
}

链接

Vanya and Books

这个题题目很好懂直接找到规律直接计算,暴力肯定会超时,所以我们需要一些规律,求出最后的结果

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

signed main () {
	int n;
	cin >> n;
	string s = to_string(n);//转换成字符串便于计算位数
	double x = pow(10, (int)s.size() - 1);//构造和当前数字位数相同的数并且最小的数
	int ans = (n - x + 1) * s.size();//计算在当前位数的一种的位数的值加入到ans里面
	if (s.size() - 1 == 0) {//如果s只有一位的话,那么直接退出就可以了
		cout << ans << '\n';
		return 0;
	}
	int m;
	for (int i = 1;; i++) {//否则进入这个循环进行计算以后的位数
		n = pow(10, (int)s.size() - i);//计算s.size()-i位
		m = pow(10, (int)s.size() - i - 1);//计算s.size()-i-1位
		ans = ans + (n - m) * (s.size() - i);//n-m是有多少个这种数,s.size()-i是当前的位数
		if (m == 1) {//如果m的值唯一就该退出循环了
			break;
		}

	}



	cout << ans << '\n';//打印结果
	return 0;
}

A. TL

链接

A. TL

这个题还是比较有意思的和算法题的评测机制放在了一起.先对数组排序找到满足条件1,2,4的结果,然后判断一下能不能满足条件3,当满足了条件1,2,3,4的话,找到最小的那个值打印出来

#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 = 500;
int a[N], b[N];
signed main () {
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= m; i++) {
		cin >> b[i];
	}
	sort(a + 1, a + n + 1);
	sort(b + 1, b + m + 1);//先对两个数组排序
	if (a[n] >= b[1]) {//如果a数组的最大值大于等于了b数组的最小值
		cout << -1 << '\n';//直接打印-1
		return 0;
	}
	int ans = b[1] - 1;//先定义结果为b[1]-1,保证满足条件1,2,4
	//下面来看条件3
	if (a[1] * 2 > ans) {//如果a里面最短的时间都不能满足条件3
		cout << -1 << '\n';//打印-1
	} else {
		//如果存在满足1,2,3,4的情况找到最下的那一个
		if (a[n] >= a[1] * 2) {
			//因为要满足所有的a数组通过我们看一下a[n]的大小
			//如果a[n] >= a[1] * 2)的话,
			ans = a[n];//ans设置为a[n]这样就是最小的
		} else {
			ans = a[1] * 2;//否则设置为a[1]*2这样是最小的
		}
		cout << ans << '\n';

	}




	return 0;
}

标签:03,cout,int,09,long,define,ans,include,刷题
From: https://www.cnblogs.com/harper886/p/17208861.html

相关文章

  • 2023/03/11刷题
    A.MinimizingtheString链接A.MinimizingtheString这个题的意思就是删除一个字母让字符串的字典序变得最小,如果字符串的顺序是abcda的话很明显我们要删除d所以我......
  • Mybatis-lesson05-结果映像-第一课:简单的映射-03-12
    第一步:pojo的属性和数据库的列名不一样packagecom.feijian.pojo;publicclassUser{privateintid;privateStringname;privateStringpassword;......
  • 09:矩阵乘法
    描述计算两个矩阵的乘法。n*m阶的矩阵A乘以m*k阶的矩阵B得到的矩阵C是n*k阶的,且C[i][j]=A[i][0]*B[0][j]+A[i][1]*B[1][j]+……+A[i][m-1]*B[m-1][j](C[i][j]表示......
  • C++银行卡管理系统[2023-03-12]
    C++银行卡管理系统[2023-03-12]第一次编程作业:(1)将代码阅读、调试通过;(2)添加功能:包括增加资金转账明细、按时间统计账号转账信息等。头文件:bankcard.h源文件:mai......
  • 基于QT实现的文献管理系统[2023-03-12]
    基于QT实现的文献管理系统[2023-03-12]基于QT文献管理系统的设计与实现用户管理:管理员可以文献管理系统编辑用户信息,用户可以自行注册账号,并且可以修改个人信息和密码。......
  • [20230308]12c以上版本模糊查询问题.txt
    [20230308]12c以上版本模糊查询问题.txt--//前几天看了链接http://www.itpub.net/thread-2148700-1-1.html,对方提到模糊查询慢的问题,实际上这个问题使用常规模式基本--//无......
  • C/C++书籍借阅系统[2023-03-12]
    C/C++书籍借阅系统[2023-03-12]1.程序名称:书籍借阅系统2.课题来源:课程组自拟3.课题类型:综合型4.目的和意义:1)综合运用所学知识,解决实际问题2)全面提高学生的程序设计......
  • 09 阻塞与非阻塞
    1.阻塞与非阻塞的实现和使用1.1openopen文件的时候选择0_NONBLOCK使用非阻塞方式打开文件;默认为阻塞1.2fcntl通过fcntl直接修改文件的flag为阻塞或非阻塞注意:对......
  • 【PAT乙】1003 我要通过! (20分) 字符串条件判定
    problem“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送——只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案......
  • MybatisPlus提示 Could not set property 'id' of '***' with value
    场景使用MybatisPlus执行插入操作时提示: Couldnotsetproperty'id'of'classcom.badao.beans.Employee'withvalue实现找到实体类添加主键策略以及制定表名MP支持以......