概述
第一次提交(60分)代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
long sum = 0L;
int[][] aa = new int[a][2];
int[] u = new int[n];
int[][] bb = new int[b][2];
int[] v = new int[n];
for (int i = 0; i < n; i++) {
v[i] = 0;
u[i] = 0;
}
for (int i = 0; i < a; i++) {
aa[i][0] = scanner.nextInt();
aa[i][1] = scanner.nextInt();
u[aa[i][0]-1] = aa[i][1];
}
for (int i = 0; i < b; i++) {
bb[i][0] = scanner.nextInt();
bb[i][1] = scanner.nextInt();
v[bb[i][0]-1] = bb[i][1];
}
for (int i = 0; i < n; i++) {
if (u[i]!=0&&v[i]!=0)
sum += u[i]*v[i];
}
System.out.println(sum);
}
}
方法二
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = 0;
int d = 0;
long sum = 0L;
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < a; i++) {
map.put(scanner.nextInt(),scanner.nextInt());
}
for (int i = 0; i < b; i++) {
c = scanner.nextInt();
d = scanner.nextInt();
if(map.containsKey(c))
sum += map.get(c)*d;
}
System.out.println(sum);
}
}
最后
以上就是迷你微笑为你收集整理的CCF(202006-2)—— 稀疏向量的全部内容,希望文章能够帮你解决CCF(202006-2)—— 稀疏向量所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复