CCFCSP试题编号:202006-2试题名称:稀疏向量

不断匹配相乘累加就好了

cpp 复制代码
#include<iostream>
#include<vector>
#include <utility>
using namespace std;

int main() {
	int n;
	int a, b;
	long long result=0; // 使用 long long  
	cin >> n >> a >> b;

	vector<pair<int, int> > u, v;
	int index, value;

	for (int i = 0; i < a; i++) {
		cin >> index >> value;
		u.push_back({ index, value });
	}

	for (int i = 0; i < b; i++) {
		cin >> index >> value;
		v.push_back({ index, value });
	}

	int i = 0, j = 0;
	while (i < a && j < b) {
		if (u[i].first == v[j].first) {
			result += u[i].second * v[j].second;
			i++; j++;
		}
		else if (u[i].first > v[j].first) {
			j++;
		}
		else {
			i++;
		}
	}

	cout << result << endl;

	return 0;
}

这里面用了pair,如果不了解的,可以看一下妾身这一篇C++中的pair

谢谢大家!

相关推荐
txinyu的博客2 分钟前
解析muduo源码之 SocketsOps.h & SocketsOps.cc
c++
ctyshr25 分钟前
C++编译期数学计算
开发语言·c++·算法
zh_xuan34 分钟前
最小跳跃次数
数据结构·算法
努力写代码的熊大43 分钟前
c++异常和智能指针
java·开发语言·c++
yumgpkpm44 分钟前
2026软件:白嫖,开源,外包,招标,晚进场(2025年下半年),数科,AI...中国的企业软件产业出路
大数据·人工智能·hadoop·算法·kafka·开源·cloudera
John_ToDebug1 小时前
WebContent 与 WebView:深入解析浏览器渲染架构的双层设计
c++·chrome·ui
千秋乐。1 小时前
C++-string
开发语言·c++
孞㐑¥1 小时前
算法—队列+宽搜(bfs)+堆
开发语言·c++·经验分享·笔记·算法
yufuu981 小时前
并行算法在STL中的应用
开发语言·c++·算法
zh_xuan1 小时前
单青蛙跳台阶
数据结构·算法