4 solutions
-
0
import java.util.Scanner; public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); //定义输入数字的个数 int n = sc.nextInt(); //定义存储随机数的数组 int [] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } //把数组从小到大排列 for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } //定义boolean【】 usedArr数组,用来标记被用过的k,因为题目是说】求多少个k满足条件 boolean[] usedArr = new boolean[n]; //用count计算有几组题目要求的数 int count = 0; for (int i = 0; i <= n - 3; i++) { for (int j = i + 1; j <= n - 2; j++) { for (int k = j + 1; k <= n - 1; k++) { if (usedArr[k] == false && (arr [i] + arr[j] ==arr[k])){ count++; usedArr[k] = true; } } } } System.out.println(count); }}
Information
- ID
- 6189
- Time
- 1000ms
- Memory
- 125MiB
- Difficulty
- 3
- Tags
- # Submissions
- 677
- Accepted
- 250
- Uploaded By