蓝桥杯JAVA版答案——历年真题——数组操作

更多JAVA版答案移步我的博客:蓝桥杯JAVA版答案汇总

本题考查

未知

思路

构建三个函数模拟对应操作

20分代码

import java.util.Scanner;
public class Main {
	static long[] arr;
	static void fun1(int l, int r, int d) {
		for(int i = l-1; i < r; i++)	arr[i]+=d;
	}
	static void fun2(int l1, int r1, int l2, int r2) {
		if(l1 > l2) {
			long[] temp = new long[r2-l2+1];
			for(int i = 0; i <= r2-l2; i++)	temp[i] = arr[l2-1+i];
			for(int i = 0; i <= r2-l2; i++) arr[l1-1+i] = temp[i];	
		}
		else if(l1 < l2)	for(int i = 0; i <= r2-l2; i++)	arr[l1-1+i] = arr[l2-1+i];
		else;
	}
	static long fun3(int l, int r) {
		long sum = 0;
		for(int i = l-1; i < r; i++)	sum+=arr[i];
		return sum;
	}

	public static void main(String[] args) {
		Scanner scaner = new Scanner(System.in);scaner.nextInt();
		int n = scaner.nextInt();
		int m = scaner.nextInt();
		arr = new long[n];
		for (int i = 0; i < n; i++)	arr[i] = scaner.nextInt();
		for (int i = 0; i < m; i++) {
			int funIndex = scaner.nextInt();
			if(funIndex == 1) 		fun1(scaner.nextInt(), scaner.nextInt(), scaner.nextInt());
			else if(funIndex == 2) 	fun2(scaner.nextInt(), scaner.nextInt(), scaner.nextInt(), scaner.nextInt());
			else 					System.out.println(fun3(scaner.nextInt(), scaner.nextInt()));
		}
		scaner.close();
	}
}

你可能感兴趣的:(蓝桥杯,JAVA)