Chapter IV - test report

Chapter IV test report

4-1 in storage procedures (90 points)
 

There n procedures {1,2, ..., n} to be stored in a length L of the tape. I programs stored on the tape length li, 1≤i≤n. The program storage seek to establish that n procedures stored in the program on the magnetic tape, the tape makes it possible to store as many programs. For a given n programs stored on the magnetic tape length, up to the number of programs can be stored on the tape is calculated.

Input formats:

The first line is a positive integer 2, respectively, and n denotes the length of the magnetic tape the number of documents L. 1 in the next row, with n a positive integer representing the program stored on the tape length.

Output formats:

The number of output can store up program.

Sample input:

Here we are given a set of inputs. E.g:

6 50 
2 3 13 8 80 20

Sample output:

Given here corresponding output. E.g:

5



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

public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int size=input.nextInt();
int[]nums=new int[n];
for(int i=0;i<nums.length;i++){
nums[i]=input.nextInt();
}
System.out.println(Solve(nums,size));
}

private static int Solve(int[] nums, int size) {
Arrays.sort(nums);
int count=0;
for(int i=0;i<nums.length;i++){
if(nums[i]<=size){
size=size-nums[i];
count++;
}
}
return count;
}
}

Complexity analysis:

  Space complexity o (n), because the article storage arrays use n positions

  The time complexity is o (nlogn), because the use sort, the sort complexity is nlogn and this time it takes for the highest power.

Experience

  Listen partner to explain the whole, benefit, need to pay more hands-on.

4-1 in storage procedures (90 points)
 

There n procedures {1,2, ..., n} to be stored in a length L of the tape. I programs stored on the tape length li, 1≤i≤n. The program storage seek to establish that n procedures stored in the program on the magnetic tape, the tape makes it possible to store as many programs. For a given n programs stored on the magnetic tape length, up to the number of programs can be stored on the tape is calculated.

Input formats:

The first line is a positive integer 2, respectively, and n denotes the length of the magnetic tape the number of documents L. 1 in the next row, with n a positive integer representing the program stored on the tape length.

Output formats:

The number of output can store up program.

Sample input:

Here we are given a set of inputs. E.g:

6 50 
2 3 13 8 80 20

Sample output:

Given here corresponding output. E.g:

5



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

public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int size=input.nextInt();
int[]nums=new int[n];
for(int i=0;i<nums.length;i++){
nums[i]=input.nextInt();
}
System.out.println(Solve(nums,size));
}

private static int Solve(int[] nums, int size) {
Arrays.sort(nums);
int count=0;
for(int i=0;i<nums.length;i++){
if(nums[i]<=size){
size=size-nums[i];
count++;
}
}
return count;
}
}

Complexity analysis:

  Space complexity o (n), because the article storage arrays use n positions

  The time complexity is o (nlogn), because the use sort, the sort complexity is nlogn and this time it takes for the highest power.

Experience

  Listen partner to explain the whole, benefit, need to pay more hands-on.

Guess you like

Origin www.cnblogs.com/ysmsxzs/p/11900715.html