2018級《算法分析與設計》5(java)

問題 A: 選房子

題目描述

棟棟和李劍已經大四了,想要出去找房子住。他們一共看中了n套房子。其中第i套房子已經住了ai個人了,它最多能住bi個人。棟棟和李劍想要住在一起,那麼請問他們有幾套可以選擇的房子?

輸入

輸入的第一行爲一個正整數T (T<=1000),代表一共有T組測試數據。

每組測試數據的第一行有一個正整數n (1<=n<=100),代表一共有n套房子。接下來n行,每行有兩個正整數ai,bi (1<=ai<=bi<=100),分別代表現在已經住了ai個人和最多能住bi個人。

輸出

對於每組測試數據,輸出一行包含一個整數,代表他們可以選擇房子的數量。

樣例輸入 Copy

2
2
1 2
1 3
3
1 10
2 10
3 10

樣例輸出 Copy

1
3

import java.util.Scanner;
 
public class Main
{
 
     
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
        int T=cin.nextInt();
        while(T>0) {
            int count=0;
            int n=cin.nextInt();
            int a[][]=new int[n][2];
            for(int i=0;i<n;i++) {
                for(int j=0;j<2;j++) {
                    a[i][j]=cin.nextInt();
                }
            }
            for(int i=0;i<n;i++) {
                    if(a[i][1]-a[i][0]>=2) {
                        count++;
                    }
                     
                }
            System.out.println(count);
            T--;
        }
         
    }
}
 

問題 B: 神祕的數字

題目描述

又一個神祕的數字,它是一個4位數,該4位數的千位上的數字和百位上的數字都被擦掉了,知道十位上的數字是1、個位上的數字是2,
又知道這個數字減去7就能被7整除,減去8就能被8整除,減去9就能被9整除。

輸入

輸出

輸出這個數

import java.util.Scanner;
 
public class Main
{
 
     
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
         
        for(int a = 1; a <= 9; a++)  
            for(int b =0; b <= 9; b++){
                int num = a * 1000 + b * 100 + 10 + 2; 
                if((num - 7) % 7 == 0 && (num - 8) % 8 == 0 && (num - 9) % 9 == 0){
                    System.out.println(num);
                    break; 
                } 
                     
     
         
    }
}}

問題 C: 隨機數

題目描述

有一個rand(n)的函數,它的作用是產生一個在[0,n)的隨機整數。現在有另外一個函數,它的代碼如下:

int random(int n, int m)

{

     return rand(n)+m;

}

顯而易見的是函數random(n,m)可以產生任意範圍的隨機數。現在問題來了,如果我想要產生範圍在[a,b)內的一個隨機數,那麼對應的n,m分別爲多少?

輸入

輸入的第一行爲一個正整數T (T<=1000),表示一共有T組測試數據。

對於每組測試數據包含兩個整數a,b (a<=b)。

輸出

對於每組測試數據,輸出一行包含兩個整數n和m,兩個整數中間有一個空格分隔。

樣例輸入 Copy

2
0 5
1 4

樣例輸出 Copy

5 0
3 1

import java.util.Scanner;
 
public class Main
{
 
     
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
         
        int n;
        n=cin.nextInt();
        while(n>0){
            while(n>0){
                int a,b;
                 a=cin.nextInt();
                b=cin.nextInt();
                int x,y;
                x=b-a;
                y=a;
               System.out.println(x+" "+y);
               n--;
            }
             
        }
 
     
}}

問題 D: 快速排序

題目描述

編程實現快速排序算法,深入理解快速排序算法的基本思想。

輸入

多組輸入,每組第一個數字爲數組長度,然後輸入一個一維整型數組。

輸出

輸出快速排序之後的一維整型數組(升序)

樣例輸入 Copy

6 1 8 6 5 3 4
5 12 42 2 5 8

樣例輸出 Copy

1 3 4 5 6 8
2 5 8 12 42

import java.util.*;
public class Main {


    static void swap(int a[],int i,int j){
    int temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    //分區函數

    static  int  partion(int a[],int p,int q){
    int x=a[p];
    int i=p,j;
    for(j=p+1;j<=q;j++){
        if(a[j]<=x){
            i++;
            swap(a,i,j);
        }

    }
        swap(a,p,i);
    return i;
    }
    static void quckSort(int a[],int p,int q){
    if(p<q){
        int r=partion(a,p,q);
        quckSort(a,p,r-1);
        quckSort(a,r+1,q);

    }
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()){
            int n=cin.nextInt();
            int arr[]=new int[n];
            for(int i=0;i<arr.length;i++){
                arr[i]=cin.nextInt();
            }
            quckSort(arr,0,arr.length-1);
            for(int i=0;i<arr.length;i++){
                System.out.print(arr[i]+" ");
            }
            System.out.println();

        }

}
}

問題 E: 隨機化快速排序

題目描述

使用Java或C++等語言中內置的隨機函數實現隨機化快速排序,在數組中隨機選擇一個元素作爲分區的主元(Pivot)。

輸入

多組樣例輸入,每組由一個一維整型數組組成。

輸出

隨機化快速排序之後的一維整型數組(升序排列)。

樣例輸入 Copy

6 1 8 6 5 3 4
5 12 42 2 5 8

樣例輸出 Copy

1 3 4 5 6 8
2 5 8 12 42

import java.util.*;
public class Main {


    static void swap(int a[],int i,int j){
        int temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
    //分區函數

    static  int  partion(int a[],int p,int q){
        int x=a[p];
        int i=p,j;
        for(j=p+1;j<=q;j++){
            if(a[j]<=x){
                i++;
                swap(a,i,j);
            }

        }
        swap(a,p,i);
        return i;
    }
    static void quckSort(int a[],int p,int q){
        if(p<q){
            int k=(int) ((Math.random()% (q-p+1))+ q);
            //隨機產出一個[p,q]之間的隨機數
            swap(a,k,p);//與p進行交換
            int r=partion(a,p,q);
            quckSort(a,p,r-1);
            quckSort(a,r+1,q);

        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()){
            int n=cin.nextInt();
            int arr[]=new int[n];
            for(int i=0;i<arr.length;i++){
                arr[i]=cin.nextInt();
            }
            quckSort(arr,0,arr.length-1);
            for(int i=0;i<arr.length;i++){
                System.out.print(arr[i]+" ");
            }
            System.out.println();

        }

    }
}
 
 
 

問題 F: 數組合並

題目描述

編寫一個程序,將兩個有序數組合併成一個更大的有序數組,要求時間複雜度爲O(n)。

輸入

多組數據輸入,每組輸入包括兩行,每行第一個數字爲數組長度n,然後輸入n個有序整數。

輸出

輸出合併後的數組(升序),每組輸出用一個空行隔開。

樣例輸入 Copy

3 1 3 5
3 2 4 6
2 1 2
4 3 4 5 6

樣例輸出 Copy

1 2 3 4 5 6

1 2 3 4 5 6

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
 
public class Main
{
   
   
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
       while(cin.hasNext()) {
       //輸入
           int n1=cin.nextInt();
           int a1[]=new int[n1];
           for(int i=0;i<n1;i++) {
               a1[i]=cin.nextInt();
           }
           int n2=cin.nextInt();
           int a2[]=new int[n2];
           for(int i=0;i<n2;i++) {
               a2[i]=cin.nextInt();
           }
           //定義一個數組,用於存放排序後的結果
           int arr[]=new int[n1+n2];
           //對兩個數組進行排序
           Arrays.sort(a1);
           Arrays.sort(a2);
           int i = 0,j = 0;int q=0;
           //如果兩個數組均還有數
         while(i<n1&&j<n2) {
         
             if(a1[i]<a2[j]) {
                 arr[q]=a1[i];
                 i++;
                 q++;
             }else {
                 arr[q]=a2[j];
                 j++;
                 q++;
             }
         }
         //對於a1判斷完的情況
         while(j<n2) {
             arr[q]=a2[j];
             j++;
             q++;
         }
          //對於a2判斷完的情況
         while(i<n1) {
             arr[q]=a2[i];
             i++;
             q++;
         }
         for( i=0;i<arr.length;i++) {
             System.out.print(arr[i]+" ");
             if(i==arr.length-1) {System.out.println();}
         }
         System.out.println();
         
       }
    }
}
 

問題 G: 歸併排序

題目描述

編寫一個程序,使用分治策略實現二路歸併排序(升序)。

輸入

多組輸入,每組第一個數字爲數組長度,然後輸入一個一維整型數組。

輸出

輸出排序之後(升序)的一維整型數組,每組輸出佔一行。

樣例輸入 Copy

6 1 8 6 5 3 4
5 12 42 2 5 8

樣例輸出 Copy

1 3 4 5 6 8
2 5 8 12 42

import java.util.Scanner;

public class Main {
    static  void Merge (int SR[ ], int TR[ ], int s, int m, int t )
    {
       int i=s;int j=m+1;int k=s;
       while(i<=m&&j<=t){
           if  (SR[i]<=SR[j])
           {
               TR[k++]=SR[i++];
           }
           else
           {
               TR[k++]=SR[j++];
           }
       }
        while (i<=m)
        {
            TR[k++]=SR[i++];
        }
        while (j<=t)
        {
            TR[k++]=SR[j++];
        }

    }
    static  void Meragesort(int SR[],int TR[],int s,int t){
        if(s<t){
            int m=(s+t)/2;
            Meragesort(SR,TR,s,m);
            Meragesort(SR,TR,m+1,t);
            Merge(SR,TR,s,m,t);
        }
    }
   static void MergeSort(int SR[],int TR[], int s, int t ) {
        if (s < t) {
            int m = (s+t)/2;
            MergeSort(SR,TR, s, m);
            MergeSort(SR,TR, m+1, t);
            Merge(SR, TR, s, m, t);
            for(int i=s;i<=t;i++)
            {
                SR[i] = TR[i];
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext())
        {
            int n=cin.nextInt();
            int arr[]=new int[n];
            for(int i=0;i<n;i++)
            {
                arr[i]=cin.nextInt();
            }
            int[] r=new int[n];

           MergeSort(arr, r, 0, n-1);
            for(int j=0;j<n;j++)
            {
                System.out.print(r[j]+" ");
            }
            System.out.println();
        }
    }

}

問題 H: Strange fuction

題目描述

Now, here is a fuction:
F(x) = 6 * x7+8*x6+7x3+5*x2-yx (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.

輸入

The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)

輸出

Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

樣例輸入 Copy

2
100
200

樣例輸出 Copy

-74.4291
-178.8534

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
 
public class Main
{
  static double y;
    public static double calc(double x) {
        return 6 * Math.pow(x, 7) + 8 *Math. pow(x, 6) + 7 *Math. pow(x, 3) + 5 * x * x - y * x;
    }
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
        int T=cin.nextInt();
        while(T>0) {
             y=cin.nextDouble();
             double l = 0, r = 100;
             double midl = 0, midr;
             while (r - l > 1e-8) {
                 midl = (l + r) / 2;
                 midr = (midl + r) / 2;
                 double cmidl = calc(midl);
                 double cmidr = calc(midr);
                 if (cmidl < cmidr) {
                     r = midr;
                 }
                 else l = midl;
             }
            System.out.println(String.format("%.4f",calc(midl)));
 
            T--;
         }
       
        }
    }
 
 

問題 I: Who’s in the Middle

題目描述

FJ is surveying his herd to find the most average cow. He wants to know how much milk this ‘median’ cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1…1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.

輸入

  • Line 1: A single integer N

  • Lines 2…N+1: Each line contains a single integer that is the milk output of one cow.

輸出

  • Line 1: A single integer that is the median milk output.

樣例輸入 Copy

5
2
4
1
3
5

樣例輸出 Copy

3

提示

INPUT DETAILS:
Five cows with milk outputs of 1…5
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
 
public class Main
{
  static double y;
    public static double calc(double x) {
        return 6 * Math.pow(x, 7) + 8 *Math. pow(x, 6) + 7 *Math. pow(x, 3) + 5 * x * x - y * x;
    }
    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()) {
        int n=cin.nextInt();
        int arr[]=new int[n];
        if(n%2!=0) {
            for(int i=0;i<arr.length;i++) {
                arr[i]=cin.nextInt();
            }
            Arrays.sort(arr);
            System.out.println(arr[arr.length/2]);
        }
        }
        }
    }
 
 

問題 J: 第k大元素問題

題目描述

輸入n個整數和一個正整數k(1<=k<=n),輸出這些整數從大到小排序後的第k個。(要求時間複雜度爲O(n),需使用隨機化分區) 。

輸入

多組數據輸入,每組第一個數字爲數組的長度n, 然後接下輸入n個整數,最後輸入整數k(1<=k<=n)。

輸出

輸出數組降序排序後的第k個整數。

樣例輸入 Copy

5 1 5 2 4 3 3
6 1 2 3 4 5 6 1

樣例輸出 Copy

3
6

  import java.util.Random;
        import java.util.Scanner;

public class Main
{

//交換函數
    public static void change(int a[],int p,int q) {
        int t;
        t=a[p];
        a[p]=a[q];
        a[q]=t;
    }
//分區函數
    public static int partition(int a[],int p,int q) {
        int x=a[p];
        int i=p,j;
        for(j=p+1;j<=q;j++) {
            if(a[j]>x) {
                i++;
                change(a,i,j);
            }
        }
        change(a,p,i);
        return i;
    }
    //隨機生成[p,q]的隨機函數
    int randomswap(int a[],int p,int q)
    {
        int k=(int) ((Math.random()% (q-p+1))+ q);
        change(a,k,p);
        return partition(a,p,q);
    }


    public static int  quickSelect(int a[],int s,int t,int k){
        if(s==t)return  a[s];
        int i=partition(a,s,t);
        int j=i-s+1;//比a[i]大的個數
        if(k<=j)return quickSelect(a,s,i,k);
        else return  quickSelect(a,i+1,t,k);

    }

    public static void main(String[] args){
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()) {
            int n=cin.nextInt();
            int a[]=new int[n];
            for(int i=0;i<a.length;i++) {
                a[i]=cin.nextInt();
            }

            int k=cin.nextInt();
            System.out.println( quickSelect(a,0,a.length-1,k));
        }

    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章