java进阶干货IT面试分享数据结构

拼多多2018服务端笔试

2018-04-03  本文已影响200人  Taoyongpan

第一题

pdd01.png
import java.util.Scanner;

/**
 * @Author: Taoyongpan
 * @Date: Created in 15:10 2018/4/3
 */
public class Test05 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();
            int k = sc.nextInt();
            int[] arr = new int[105];
            int a = 0;
            int b = 0;
            for (int i = 0 ; i< n;i++){
                a = sc.nextInt();
                b = sc.nextInt();
                a+=50;
                b+=50;
                for (int j = a; j <= b;j++){
                    arr[j]++;
                }
            }
            int max = -1;
            int min = 105;
            for (int i = 0 ; i<101;i++){
                if (arr[i]>=k){
                    if (i>=max)
                        max = i;
                    if (i<=min)
                        min = i;
                }
            }
            max-=50;
            min-=50;
            if (min<max&&min>100&&max<0)
                System.out.println(min+" "+max);
            else
                System.out.println("error");
        }
    }
}

第二题

pdd02.png
解法一,不可取,有点暴力
import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * @Author: Taoyongpan
 * @Date: Created in 15:10 2018/4/3
 */
public class Test06 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            char[] c = str.toCharArray();
            double h = 0;
            int m = 0;
            if(c.length==4){
                m = (c[2]-'0')*10+(c[3]-'0');
                h = ((c[0]-'0')+((m*1.0)/60));

            }else if (c.length ==5){
                m = (c[3]-'0')*10+(c[4]-'0');
                h = (((c[0]-'0')*10+(c[1]-'0'))+((m*1.0)/60));
            }
            if (m<=59 && m>=0 && h<24 && h>=0){
                if ((Math.abs((h%12)*5-m)*6)%1 == 0){
                    if (Math.abs((h%12)*5-m)*6<=180)
                        System.out.println((int)(Math.abs((h%12)*5-m)*6));
                    else
                        System.out.println((int)(360-Math.abs((h%12)*5-m)*6));
                }else {
                    DecimalFormat df = new DecimalFormat("#.0");
                    if (Math.abs((h%12)*5-m)*6<=180)
                        System.out.println(df.format(Math.abs((h%12)*5-m)*6));
                    else
                        System.out.println(df.format(360-Math.abs((h%12)*5-m)*6));
                }
            }else {
                System.out.println("error");
            }
        }
    }
}

解法二

import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * @Author: Taoyongpan
 * @Date: Created in 20:35 2018/4/3
 */
public class Test09 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            String[] s = str.split(":");
            int h, m;
            h = Integer.valueOf(s[0]);
            m = Integer.valueOf(s[1]);
            double gap = (30 * h - 5.5 * m) % 360;
            if (gap>180){
                gap = 360-gap;
            }
            if (gap%1==0){
                System.out.println((int)gap);
            }else {
                DecimalFormat df = new DecimalFormat("#.0");
                System.out.println(df.format(gap));
            }
        }
    }
}

第三题

pdd03.png
#include<bits/stdc++.h>  
using namespace std;  
typedef long long ll;  
const double pi=acos(-1.0);  
const double eps=1e-8;
struct point{
    double x,y;
    point(double a=0,double b=0){
    x=a,y=b;
    }
}p[110];  

double xmult(point p1,point p2,point p0){
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

int dblcmp(double x){
    if(x<-eps) return -1;
    if(x>eps) return 1;
    return 0;
}

int dot_inline(point a,point b,point c){
    return dblcmp(xmult(a,b,c));
}

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>p[i].x>>p[i].y;
    }
    ll cnt=0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n;k++){
                if(dot_inline(p[i],p[j],p[k])) cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}

这题的代码是我室友写出来的,我的思路是,求斜率,当三个点在一条直线上的时候不能构成,其他都行,暴力遍历一遍就能 算出结果;

上一篇下一篇

猜你喜欢

热点阅读