3
import java.util.Scanner; public class Main{ static boolean[] dir;//true: 表示方向向右 false: 向左 static int[] dis;//绝对距离 static boolean[] gm;//是否感冒 public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); dir = new boolean[n]; dis = new int[n]; gm = new boolean[n]; String[] sarr = scanner.nextLine().split("\\s+"); if (Integer.parseInt(sarr[0]) > 0) { dir[0] = true; dis[0] = Integer.parseInt(sarr[0]); gm[0] = true; } else { dir[0] = false; dis[0] = -Integer.parseInt(sarr[0]); gm[0] = true; } for (int i = 1; i < n; i++) { int tmp = Integer.parseInt(sarr[i]); if (tmp > 0) { dir[i] = true; dis[i] = tmp; } else { dir[i] = false; dis[i] = -tmp; } } cal(); int count=0; for(boolean b:gm) { if(b) { count++; } } System.out.print(count); } static void cal() { for(int i=1;i<dis.length-1;i++) { for(int j=i+1;j<dis.length;j++) { if(Math.abs(dis[i])>Math.abs(dis[j])) { int tmp=dis[i]; dis[i]=dis[j]; dis[j]=tmp; boolean b=dir[i]; dir[i]=dir[j]; dir[j]=b; } } } /*for(int i:dis) { System.out.print(i+" "); }*/ if (dir[0]) { int mark=0; for (int i = gm.length-1; i >0; i--) { if (dis[0] < dis[i]) { if (dir[i]) { } else { gm[i] = true; mark=1; } } else { if(dir[i]&&mark==1) { gm[i]=true; } } } } else { int mark=0; for (int i = 1; i <gm.length; i++) { if (dis[0] > dis[i]) { if (!dir[i]) { } else { gm[i] = true; mark=1; } } else { if(!dir[i]&&mark==1) { gm[i]=true; } } } } } }
原文地址:http://blog.csdn.net/hymanxq/article/details/26962923