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++)+" ");
        }


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