2024蓝桥杯C++A组:因数计数(集合存储二元组)

题目描述

解题思路

循环遍历数组,判断因数找出所有的有序二元组对,并用集合进行存储,遍历集合,以i,j,k,l互不相等为条件,生成有序四元组,计算四元组个数

附代码

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
using namespace std;
set<pair<int, int>> s;
int main() {
	int N;
	cin >> N;
	vector<int> nums;
	while (N--) {
		int temp;
		cin >> temp;
		nums.push_back(temp);
	}
	for (int i = 0; i < nums.size(); i++) {
		for (int j = i + 1; j < nums.size(); j++) {
			if (nums[i] % nums[j] == 0)
				s.insert({ j + 1,i + 1 });
			if(nums[j] % nums[i] == 0)
				s.insert({ i + 1,j + 1 });			//判断ai和aj是否为对方的因数
		}
	}
	int sum = 0;
	for (auto t1 : s) {
		for (auto t2 : s) {
			if (t1.first != t2.first && t1.first != t2.second && t1.second != t2.first && t1.second != t2.second)
				sum++;
		}
	}
	cout << sum;
	return 0;
}

  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值