题目1126:打印极值点下标

来源:互联网 发布:阿里妈妈淘宝客违规 编辑:程序博客网 时间:2024/04/29 23:59
import java.io.IOException;import java.io.FileReader;import java.io.InputStreamReader;import java.io.BufferedReader;import java.util.Scanner;import java.util.Vector;class Main{public static final boolean DEBUG = false;public static void main(String[] args) throws IOException{Scanner cin;int n, t;if (DEBUG) {cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));} else {cin = new Scanner(new InputStreamReader(System.in));}t = cin.nextInt();while (t-- > 0) {n = cin.nextInt();int[] a = new int[n];for (int i = 0; i < n; i++) {a[i] = cin.nextInt();}Vector<Integer> v = new Vector<Integer>();if (a[0] > a[1] || a[0] < a[1]) {v.add(new Integer(0));}for (int i = 1; i < n - 1; i++) {if ((a[i] > a[i - 1] && a[i] > a[i + 1]) || (a[i] < a[i - 1] && a[i] < a[i + 1])) v.add(new Integer(i));}if (a[n - 1] > a[n - 2] || a[n - 1] < a[n - 2]) v.add(new Integer(n - 1));if (v.size() == 1) {System.out.println(v.get(0));} else {System.out.print(v.get(0));for (int i = 1; i < v.size(); i++) {System.out.print(" " + v.get(i));}System.out.println();}}}}

0 0
原创粉丝点击