//看到题目第一个想到的就是桶排序了---值的范围固定,然后出现重复数import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = 0; int[] count = new int[100]; while (sc.hasNext()){ count[sc.nextInt()]++; n++; } for (int i = 0; i < count.length; i++) { if (count[i] >= n / 2){ System.out.println(i); return; } } } }
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str[]=br.readLine().split(" "); Map<Integer,Integer> m=new HashMap<Integer,Integer>(); int sz[]=new int[str.length]; for(int i=0;i<str.length;i++){ sz[i]=-1; if(m.get(Integer.parseInt(str[i]))!=null){ m.put(Integer.parseInt(str[i]), m.get(Integer.parseInt(str[i]))+1); }else{ m.put(Integer.parseInt(str[i]), 1); sz[i]=Integer.parseInt(str[i]); } } int max=0; int maxs=0; for(int i=0;i<str.length;i++){ if(sz[i]!=-1){ if(maxs<m.get(sz[i])){ maxs=m.get(sz[i]); max=sz[i]; } } } System.out.println(max); } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; //保证AC public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Map<String, Integer> map = new HashMap<>(); String input = scanner.nextLine(); String[] arr= input.split(" "); for (String s : arr) { map.put(s, map.getOrDefault(s, 0) + 1); } for (String key : map.keySet()) { if (map.get(key) >= map.size()/2) { System.out.println(key); break; } } } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String[] dateStr=in.nextLine().split(" "); int[] date=new int[dateStr.length]; for(int i=0;i<dateStr.length;i++) { date[i]=Integer.valueOf(dateStr[i]); } Arrays.sort(date); System.out.println(date[date.length/2-1]); }}
//做的时候没想到中间值的方法,使用map的方法如下 import java.util.Scanner; import java.util.Map; import java.util.TreeMap; import java.util.Map.Entry; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ Map<Integer,Integer> map = new TreeMap<>(); String[] strs = sc.nextLine().split(" "); for(int i=0;i<strs.length;i++){ int s = Integer.valueOf(strs[i]); if(map.containsKey(s)){ map.put(s,map.get(s)+1); }else{ map.put(s,1); } } for(Map.Entry<Integer,Integer> entry : map.entrySet()){ if(entry.getValue() >= strs.length/2){ System.out.println(entry.getKey()); } } } } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { // 输入测试用例 Scanner in = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); while (in.hasNextInt()) { list.add(in.nextInt()); } // 利用工具类进行排序 Collections.sort(list); // 打印值 System.out.println(list.get(list.size() / 2 - 1)); } }
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String arr[]=str.split(" "); int arr1[]=new int [arr.length]; Arrays.fill(arr1, 1); for(int i=0;i<arr.length;i++){ for(int j=i+1;j<arr.length;j++){ if(arr[j].equals(arr[i])){ arr1[i]++; } } } for(int i=0;i<arr.length;i++){ if(arr1[i]>=arr1.length/2){ System.out.println(arr[i]); break; } } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); String[] input = in.nextLine().split(" "); int n = input.length; int temp = 0; String flag=null; for(String s:input){ if(temp==0){ flag = s; temp=1; }else if(flag.equals(s)){ temp++; }else{ temp--; } } if(temp>1){ System.out.println(flag); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); HashMap<Integer, Integer> map = new HashMap<>(); while (in.hasNext()) { int num = in.nextInt(); Integer count = map.get(num); map.put(num, count == null? 1: count + 1); } Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); if ((Integer)entry.getValue() >= map.size() / 2) System.out.println(entry.getKey()); } } } ---------------------------------------------------------------------- import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int pre = -1; int res = 1; while (in.hasNext()) { int num = in.nextInt(); if (pre == -1) { pre = num; continue; } if (pre != num) { if (res == 0) { res = 1; pre = num;} else res--; } else { res++; } } System.out.println(pre); } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String[] numbers = scanner.nextLine().split(" "); int n = numbers.length; int count; for (int i = 0; i < n; i++) { count = 1; for (int j = i + 1; j < n; j++) { if (numbers[j].equals(numbers[i]) && !numbers[j].equals("j")) { numbers[j] = "j"; count++; } } if (count >= n / 2) { System.out.println(numbers[i]); } } } } } 这道题也好简单,就是计数嘛,需要注意的就是已经记过数的要进行标记,要不然容易重复计数重复输出
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<>(); while(cin.hasNextInt()){ list.add(cin.nextInt()); } Collections.sort(list); System.out.println(list.get(list.size()/2-1)); } }
import java.util.HashMap; import java.util.Scanner; import java.util.Set; public class StringUtil { //n个数里出现次数大于长度的一般的数 public static void main(String[] args ){ Scanner in = new Scanner(System.in); int n = 0; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); while(in.hasNext()){ int num = in.nextInt(); n++; if(map.containsKey(num)){ int count = map.get(num); count++; map.remove(num); map.put(num, count); } else{ map.put(num, 1); } } in.close(); Set<Integer> keyset = map.keySet(); for(Integer num: keyset){ int count = map.get(num); if(count >= n/2){ System.out.println(num); return ; } } } }
/** * 本方法只适用于这个数超过n的一半。剑指offer原题。 * 测试用例不完全,当这个数刚好等于n/2时,如1,2,1,2,3,1,正确答案为1,本代码输出3仍然通过 * * @author DanKo * */ public class Main{ public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<Integer>(); while (input.hasNext()) { list.add(input.nextInt()); } int num = list.get(0); int count = 1; for (int i = 1; i < list.size(); i++) { if (count == 0) { num = list.get(i); count = 1; } else if (list.get(i) == num) count++; else count--; } System.out.println(num); } }