腾讯2017实习生编程题之有趣的数字

来源:互联网 发布:C语言死循环有什么后果 编辑:程序博客网 时间:2024/04/28 23:38

思路:
         1.采用map来处理,TreeMap中key存放输入数字的值(达到排序的效果),vlaue存放数字出现的次数
         2.考虑输入所有数字中不同数字的个数为1,2和大于2三种情况
         3.绝对值最大的对数是最大和最小数之间的组合,取TreeMap中第一个和最后一个的value,对数=firstCount*lastCount
         4.绝对值最小的对数考虑两种情况:
                                                                a.输入数字中有重复数字,即TreeMap中存在vlaue大于1的情况,绝对值最小一定为0
                                                                   所有这样的value按照value*(value-1)/2计算之后相加即为绝对值最小的对数
                                                                b.所有数字都只出现一次,这个时候需要计算所有数字之间的差值,并统计,还是使用
                                                                  一个TreeMap来处理,最后获取TreeMap第一个value即为绝对值最小的对数

代码:
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextLine()){
        String line1=sc.nextLine();
        int n=Integer.parseInt(line1);
        String line2=sc.nextLine();
        String[] strArray=line2.split(" ");
        int[] array=new int[strArray.length];
        for(int i=0;i<strArray.length;i++){
            array[i]=Integer.parseInt(strArray[i]);
        }
            calculate(n,array);
        }
       
       
    }
   
    private static void calculate(int n,int[] array){
      
        Map<Integer,Integer> map=sort(n,array);
       
        Iterator<Integer> iter=map.keySet().iterator();
        int firstCount,lastCount,currentCount,min,max;
        if(map.size()==1){
         firstCount=lastCount=map.get(iter.next());
         max=min=firstCount*(firstCount-1)/2;
       
        }else if(map.size()==2){
         firstCount=map.get(iter.next());
         lastCount=map.get(iter.next());
         max=min=firstCount*lastCount;
         
        }else{
         firstCount=map.get(iter.next());
         min=firstCount*(firstCount-1)/2;
         for(int j=1;j<map.size()-1;j++){
          currentCount=map.get(iter.next());
          min+=currentCount*(currentCount-1)/2;
         }
         lastCount=map.get(iter.next());
         min+=lastCount*(lastCount-1)/2;
         
         if(min==0){
                Iterator<Integer> it=map.keySet().iterator();
                int[] minArray=new int[map.size()-1];
                int current=it.next();
                int i=0,next;
                while(it.hasNext()){
                    next=it.next();
                    minArray[i]=next-current;
                    i++;
                    current=next;
                }
               
                Map<Integer,Integer> minMap=sort(minArray.length,minArray);
                Iterator<Integer> minIter=minMap.keySet().iterator();
                min=minMap.get(minIter.next());
            }
         max=firstCount*lastCount;
        }
       
       
        System.out.println(min+" "+max);
    }
   
    private static Map<Integer,Integer> sort(int n,int[] array){
        Map<Integer,Integer> map=new TreeMap<Integer,Integer>();
        map.put(array[0], 1);
        for(int i=1;i<n;++i){
         if(map.get(array[i])==null){
          map.put(array[i], 1);
         }else{
          int count=map.get(array[i]);
          count++;
          map.put(array[i], count);
         }
        }
        return map;
    }
}

0 0
原创粉丝点击