拼多多笔试

数组中最大的三个数乘积

public class Main {


    public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int[] arrH = new int[n1];
        for (int j = 0; j < n1; j++) {
            arrH[j] = sc.nextInt();
        }


        int num = multiply(arrH);
        System.out.println(num);
   
    }
    public static int multiply(int [] a) {
    if(a.length<3){
    return 0;
    }
    if(a.length==3){
    return a[0]*a[1]*a[2];
    }
    int len = a.length;
    Arrays.sort(a);
    int b =a[0]*a[1]*a[len-1];
    int c =a[len-1]*a[len-1]*a[len-3];
    return b>c? b:c;
   
    }
       
}

用字符串表示大数字相乘

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * Created by Administrator on 2017-8-1.
 */
public class Main {


    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br= new BufferedReader(isr);
        String s = br.readLine();
        String[] inputs = s.split(" ");
        String num1 = inputs[0];
        String num2 = inputs[1];
        multiply(num1,num2);
    }
    public static String multiply(String num1, String num2) {
        if(num1.equals("0") || num2.equals("0")) return "0";
        int len1=num1.length();
        int len2=num2.length();
        int product,carry,i,j;
        int[] num= new int[len1+len2];
        for(i=len1-1;i>=0;i--){
            carry=0;
            for(j=len2-1;j>=0;j--){
                product=carry+ (int)(num1.charAt(i)-'0')*(int)(num2.charAt(j)-'0')+num[i+j+1];
                num[i+j+1]=product%10;
                carry=product/10;
            }
            num[i+j+1]=carry;
        }
        i=0;
        while(i             i++;
        }
        StringBuilder sb=new StringBuilder();
        while(i             sb.append(num[i]);
            i++;
        }


        System.out.println(sb);
        return sb.toString();
    }
}


import java.util.Scanner;
/**
 * Created by Administrator on 2017/8/1.
 */
public class  Main{
  public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int[] arrH = new int[n1];
        for (int j = 0; j < n1; j++) {
            arrH[j] = sc.nextInt();
        }
        int n2 = sc.nextInt();
        int[] arrW = new int[n2];
        for (int j = 0; j < n2; j++) {
            arrW[j] = sc.nextInt();
        }
        int num = countNum(arrH, arrW);
        System.out.println(num);
    }




    public static int countNum(int[] arrH, int[] arrW) {
        if (arrH == null || arrW == null || arrH.length == 0 || arrW.length == 0) {
            return -1;
        }
        sort(arrH);
        sort(arrW);
        int count = 0;
        for (int i = 0, j = 0; i < arrW.length && j < arrH.length; ) {
            if (arrW[i] >= arrH[j]) {
                j++;
                i++;
                count++;
            } else {
                j++;
            }
        }
        return count;
    }


    private static void sort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] > a[i]) {
                    int t = a[j];
                    a[j] = a[i];
                    a[i] = t;
                }
            }
        }
    }
}

你可能感兴趣的:(数据结构,JAVA)