腾讯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;
}
}
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
- 腾讯2017实习生编程题之有趣的数字
- 腾讯2017暑期实习生编程题之有趣的数字
- 有趣的数字----腾讯2017暑期实习生编程题
- 腾讯2017暑假实习生编程题有趣的数字
- 腾讯2017 暑假实习生编程题---【3】 有趣的数字
- 腾讯2017暑期实习生编程题-有趣的数字
- 腾讯2017暑期实习生编程题-C-有趣的数字
- 腾讯2017暑期实习生编程题--有趣的数字
- 腾讯2017暑期实习生编程题:有趣的数字 [python]
- 2016腾讯实习生笔试编程题:有趣的数字
- 有趣的数字(腾讯2017实习生)
- 穷举法-有趣的数字(腾讯2017实习生编程题)
- 腾讯2017暑期实习生编程题(三)----有趣的数字
- 算法与数据结构——算法题 67:有趣的数字(腾讯2017暑期实习生编程题) ? 待解决
- 有趣的数字--腾讯2017暑假实习生招聘笔试
- 腾讯17年暑期实习生笔试题 - 有趣的数字
- 2017百度春季实习生编程题之有趣的排序
- 笔试题88.腾讯2017暑期实习生笔试题(有趣数字)
- C语言 volatile 修饰 变量作用 总结
- Android自定义键盘之汉字键盘
- hdu 2896 病毒侵袭(AC自动机)
- spring boot 工程配置文件
- mac 安装Navicat Premium 注册机
- 腾讯2017实习生编程题之有趣的数字
- 推荐系统之协同过滤(CF)
- spring boot log日志
- Mybaits深入了解(二)—-入门实例
- L1-025. 正整数A+B
- spring boot关联velocity
- 分类算法需要准备的
- org.apache.taglibs.standard.tlv.JstlCoreTLV报错运行
- HashMap按value的顺序输出