小Q在周末的时候和他的小伙伴来到大城市逛街,一条步行街上有很多高楼,共有n座高楼排成一行。
小Q从第一栋一直走到了最后一栋,小Q从来都没有见到这么多的楼,所以他想知道他在每栋楼的位置处能看到多少栋楼呢?(当前面的楼的高度大于等于后面的楼时,后面的楼将被挡住)
输入第一行将包含一个数字n,代表楼的栋数,接下来的一行将包含n个数字wi(1<=i<=n),代表每一栋楼的高度。1<=n<=100000;1<=wi<=100000;
输出一行,包含空格分割的n个数字vi,分别代表小Q在第i栋楼时能看到的楼的数量。
6 5 3 8 3 2 5
3 3 5 4 4 4
当小Q处于位置3时,他可以向前看到位置2,1处的楼,向后看到位置4,6处的楼,加上第3栋楼,共可看到5栋楼。当小Q处于位置4时,他可以向前看到位置3处的楼,向后看到位置5,6处的楼,加上第4栋楼,共可看到4栋楼。
import java.util.Scanner; import java.util.Stack; public class Main { public static Stack<Integer> stackLeft = new Stack<>(); public static Stack<Integer> stackRight = new Stack<>(); public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int len = Integer.valueOf(scanner.nextLine()); String[] strs = scanner.nextLine().split(" "); Integer[] ints = strsToInts(strs, len); String res = ""; for (int i = 0; i < len; i++){ if (i!=len-1){ res += look(ints, i)+" "; }else{ res += look(ints, i); } } System.out.println(res); } public static Integer look(Integer[] items, int index){ stackLeft.clear(); stackRight.clear(); for (int offset = 1; index-offset >=0||index+offset < items.length; offset++){ int left = index-offset; int right = index+offset; if (left >= 0){ int leftItem = items[left]; if (stackLeft.empty()){ stackLeft.push(leftItem); }else{ if (stackLeft.peek()<leftItem){ stackLeft.push(leftItem); } } } if (right <= items.length-1){ int rightItem = items[right]; if (stackRight.empty()){ stackRight.push(rightItem); }else{ if (stackRight.peek()<rightItem){ stackRight.push(rightItem); } } } } return stackLeft.size() + stackRight.size() + 1; } public static Integer[] strsToInts(String[] strs, int len){ Integer[] ints = new Integer[len]; for (int i = 0; i < len; i++){ ints[i] = Integer.valueOf(strs[i]); } return ints; } }身为一个小菜鸡,我尽力了【自闭】
import java.util.Scanner; public class Tencent2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int count = Integer.valueOf(scan.nextLine()); String s = scan.nextLine();//包含实际数组的字符串 outPut(s); } public static void outPut (String s){ String[] str = s.split(" "); int[] arr = new int[str.length]; //获取数组 for (int i = 0; i < str.length; i++) { arr[i] = Integer.valueOf(str[i]); } if (arr.length==1) { System.out.println("1");//排除长度为1的数组 }else if (arr.length==2) { System.out.println("2 2");//排除长度为2的数组 }else { for (int i = 0; i < arr.length; i++) {//剩余情况仅包含楼栋数>=3 if (i==0) { System.out.print(getCount(arr,i+1,true)+1+" ");//当处于第一栋楼的时候看到的数量 }else if ( i==arr.length-1) { System.out.print(getCount(arr,arr.length-i,false)+1);//当处于最后一栋楼看到的数量 }else { System.out.print(getCount(arr,i+1,true)+getCount(arr,arr.length-i,false)+1+" ");//当处于中间楼看到的数量 } } } } //获取倒序数组 public static int[] reverse(int[] a) { int[] b = new int[a.length]; for(int start=0,end=b.length-1;start<=end;start++,end--) { int temp=a[start]; b[start]=a[end]; b[end]=temp; } return b; } //获取当前朝向所看到的楼栋数(不包含本身所在楼) public static int getCount (int[] arrb,int j, boolean a) { int count = 1; if (!a) { arrb = reverse(arrb); } int max = arrb[j]; for (int i = j; i < arrb.length; i++) { if (arrb[i]>max) { max = arrb[i]; count++; } } return count; } }