topic
[Problem description]
In the sequence a[1], a[2], …, a[n], if the subscripts i, j, k satisfy 0<i<j<k<n+1 and a[i] <a[j]<a[k], then a[i], a[j], a[k] is a set of increasing triples, and a[j] is the center of the increasing triples.
Given a sequence of numbers, how many elements in the sequence may be the center of the increasing triple.
[Input format]
The first line of input contains an integer n.
The second line contains n integers a[1], a[2], …, a[n]. Adjacent integers are separated by spaces to indicate a given sequence of numbers.
[Output format] The
output line contains an integer to indicate the answer.
[Sample input]
5
1 2 5 3 5
[Sample output]
2
[Sample description]
a[2] and a[4] may be the center of the triple.
[Evaluation use case scale and convention]
For 50% of the evaluation use cases, 2 <= n <= 100, 0 <= number in the sequence <= 1000.
For all evaluation cases, 2 <= n <= 1000, 0 <= number in the series <= 10000.
Make mistakes
The center of the triple is calculated twice
Solution
Re-assign the qualified a[j], and the assigned value cannot be greater than 0 to prevent it from being calculated twice
Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//输入整数n
int arr[] = new int[n];
int count=0;
for(int i=0;i<n;i++){
//输入n个数
arr[i]=scanner.nextInt();
}
for(int k=0;k<n;k++){
for(int j=0;j<k;j++){
for(int i=0;i<j;i++){
if(arr[i]<arr[j]&&arr[j]<arr[k]){
//条件判断,将符合条件的a[j]赋值为-1(题目有要求数列中的数大于0,所以将符合条件的a[j]赋值为-1,-2,-3都行),防止二次计算
arr[j]=-1;
count++;
}
}
}
}
System.out.println(count);
}
}