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


思路:
         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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值