CSP认证考试20206-2稀疏向量

CSP试题稀疏向量-60分

CSP认证考试20206-2稀疏向量_第1张图片
CSP认证考试20206-2稀疏向量_第2张图片

long无法接受第7-10测试点,10亿数据炸内存了。标准输入无法接收


import java.util.Scanner;

public class p20206_2 {
     
	public static void main(String[] args) {
     

		Scanner scanner1 = new Scanner(System.in);
		int Num1 = scanner1.nextInt();// 数组长度

		int Fnuma = scanner1.nextInt();// A的次数

		int Fnumb = scanner1.nextInt();// B的次数

		long XsumA[] = new long[Num1];
		long XsumB[] = new long[Num1];

		for (int i = 0; i < Fnuma; i++) {
     
			int Num2 = scanner1.nextInt();// 输入的两个参数
			int Num3 = scanner1.nextInt();
			add1(XsumA, Num2, Num3);
		}

		for (int i = 0; i < Fnumb; i++) {
     
			int Num2 = scanner1.nextInt();// 输入的两个参数
			int Num3 = scanner1.nextInt();
			add1(XsumB, Num2, Num3);
		}

		
		System.out.println(sum(XsumA, XsumB));

	}

	static void add1(long X[], int Num1, int Num2) {
     
		X[Num1 - 1] = Num2;

	}

	static long  sum(long a[], long b[]) {
     
		long  sum = 0;
		for (int i = 0; i < a.length; i++) {
     
			if (a[i] != 0 && b[i] != 0) {
     
				sum += a[i] * b[i];
			}
		}
		return sum;

	}
	}
	

在这里插入图片描述

你可能感兴趣的:(Java学习笔记)