java筆試題

今天的兩道筆試題,附自己寫的代碼:

題目(共兩題,考覈基礎代碼能力而不是算法好壞):

Problem Statement # 1

Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2 numbers, respectively. Let S1 and S2 denote the sums of all the numbers in A1 and A2, respectively. You are supposed to make the partition so that |n1 - n2| is minimized first, and then |S1 - S2| is maximized.

Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2 <= N <= 10^5), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 2^31.

Output Specification:
For each case, print in a line two numbers: |n1 - n2| and |S1 - S2|, separated by exactly one space.

Sample Input 1:
10
23 8 10 99 46 2333 46 1 666 555
Sample Output 1:
0 3611

Sample Input 2:
13
110 79 218 69 3721 100 29 135 2 6 13 5188 85
Sample Output 2:
1 9359


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        int[] num = new int[n];

        int d=0,s=0,s1=0,s2=0;;

        //n個數  用數組接受
        for(int i=0;i<n;i++){
            num[i] = in.nextInt();
        }

        //對數組進行排序
        Arrays.sort(num);

        //當n爲偶數時,即前一半和後一半的差值最大
        //n爲奇數時,前(n-1)/2  與後一半的差值最大 

        //計算差值s時 
        for(int i=0;i<n/2;i++){
            s1+=num[i];
        }
        for(int i=n/2;i<n;i++){
            s2+=num[i];
        }
        // s=|s1 - s2|
        s = s2-s1;  

        //d = |n1 - n2|
        d = n%2==0 ? 0:1;

        //輸出結果
        System.out.print(d+" "+s);

    }
}

Problem Statement # 2

You are to write a program that takes a list of strings containing integers and words and returns a sorted version of the list.

The goal is to sort this list in such a way that all words are in alphabetical order and all integers are in numerical order. Furthermore, if the nth element in the list is an integer it must remain an integer, and if it is a word it must remain a word.

Input Specification:
The input will contain a single, possibly empty, line containing a space-separated list of strings to be sorted. Words will not contain spaces, will contain only the lower-case letters a-z. Integers will be in the range -999999 to 999999, inclusive. The line will be at most 1000 characters long.

Output Specification:
The program must output the list of strings, sorted per the requirements above. Strings must be separated by a single space, with no leading space at the beginning of the line or trailing space at the end of the line.

Sample Input 1:
1
Sample Output 1:
1

Sample Input 2:
car truck bus
Sample Output 2:
bus car truck

Sample Input 3:
8 4 6 1 -2 9 5
Sample Output 3:
-2 1 4 5 6 8 9

Sample Input 4:
car truck 8 4 bus 6 1
Sample Output 4:
bus car 1 4 truck 6 8

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        //用來接收輸入的數據
        String s = in.nextLine();
        //將字符串分割成數組  標誌位空格“ ”
        String[] sourceStrArray = s.split(" ");
        //用來存放數字
        ArrayList<Integer> num = new ArrayList<>();
        //用來存放字符
        ArrayList<String> str = new ArrayList<>();

        //設置標誌位  即爲0時候爲數子  爲1時候爲字符
        int[] n  =new int[sourceStrArray.length];

        for (int i = 0; i < sourceStrArray.length; i++) {
            //將數據進行分組  如果對一個字符串使用Integer.parseInt方法會拋出異常 即爲字符串
            try {

                num.add(Integer.parseInt(sourceStrArray[i]));
                n[i] = 0;   

            } catch (NumberFormatException e) { 
                str.add(sourceStrArray[i]);
                n[i] =1;
            }

        }
        //排序
        Collections.sort(num);
        Collections.sort(str);

        //輸出數據 j k分別是數字和字符中的第幾位
        int j=0,k=0;

        for(int i = 0; i < sourceStrArray.length; i++){
            if(n[i]==0)
                System.out.print(num.get(j++)+" ");
            else
                System.out.print(str.get(k++)+" ");
        }


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