Work Application 笔试题 2017
- work application
- 笔试
- 真题
- 2017年
-[Exam1] Magic Cube
Description
Magic cube is a cube(M * M * M) which contains a integer number(0 <= number < P) in each cell(So you know there are M ^ 3 numbers).
Alice have one big cube and N smaller cubes(each small cube is unique).
The cube is magic since when you put a small cube in bigger one, the number in the same location will add up and mod P.
Your job is to determine the location of each small cube, so that we can make every number in big cube equal 0 after putting all the small cubes.
Notice
-We guarantee that there is only one solution.
-You cannot rotate cube.
-We give the numbers in cubes in order of position [0,0,0], [0,0,1], …,[0,1,0], …, [1,0,0], …
Limits
-Memory limit per test: 256 megabytes
-Time limit per test: The faster the better
Compile & Environment
C++
g++ Main.cc -o Main -fno-asm -Wall -lm –static -std=C++0x -DONLINE_JUDGE
Java J2SE 8
Maximum stack size is 50m
Input
The first line is three integers M (2 <= M <= 7), N (1 <= N <= 12), P(3 <= P <= 5)
Second line is M * M * M integers, showing the numbers in big cube.
Then is N lines, in which first is a integer L[i](1 <= L[i] < M) shows the length of the small cube, then follows L[i] ^ 3 numbers(0 <= number < P).
Output
Output N lines of triad of integers, shows the location of this small cube.
Sample Test
Input
2 1 3
1 0 0 0 0 0 0 0
1 2
output
0 0 0
(C) 2016 Works Applications Co., Ltd., All Rights Reserved.
TEST1 Magic-Cube
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private int M;
private int P;
private int N;
private int[][][] numbers;
public ArrayList<SmallCube> scList;
public Boolean CheckPosition(int index) {
int i;
if(index>=N){
return false;
}else{
SmallCube sc=scList.get(index);
for(i=0;i<sc.maxmotions;i++){
//System.out.println("[sc position]id:"+sc.id+"|| "+ sc.x + " " + sc.y + " " + sc.z + "\n");
if(i==0){
sc.x=0;sc.y=0;sc.z=0;
}
if(CheckPosition(index+1))
return true;
if(IsZero())
return true;
sc=SetNextPosition(sc);
};
}
return false;
}
public SmallCube SetNextPosition(SmallCube sc) {
int bound=M-sc.LEN;
int x=sc.x,y=sc.y,z=sc.z;
sc.z=sc.z+1;
if(sc.z>bound){
sc.z=0;
sc.y=sc.y+1;
if(sc.y>bound){
sc.y=0;
sc.x=sc.x+1;
if(sc.x>bound){
sc.x=x;sc.y=y;sc.z=z;
}
}
}
//System.out.println("[sc position]id:"+sc.id+" "+ sc.x + " " + sc.y + " " + sc.z + "\n");
return sc;
}
public Boolean IsZero(){
int x,y,z;
Boolean flag=true;
ok:
for(x=0;x<M;x++){
for(y=0;y<M;y++){
for(z=0;z<M;z++){
if(SumFromPositonXYZ(x, y,z)%P!=0){
flag=false;
break ok;
}
}
}
}
if(flag){//System.out.println("bingo !");
}
return flag;
}
public int SumFromPositonXYZ(int x, int y,int z){
int i;
int sum=0;
int len;
for(i=0;i<N;i++){
SmallCube sc=scList.get(i);
len=sc.LEN-1;
if((sc.x<=x&&x<=sc.x+len)&&(sc.y<=y&&y<=sc.y+len)&&(sc.z<=z&&z<=sc.z+len)){
sum=sum+numbers[x][y][z]+sc.numbers[x-sc.x][y-sc.y][z-sc.z];
}else{
sum =sum +numbers[x][y][z];
}
}
return sum;
}
public int[][][] InputCube(Scanner scan, int n) {
int x, y, z;
int[][][] cube = new int[n][n][n];
for (x = 0; x < n; x++) {
for (y = 0; y < n; y++) {
for (z = 0; z < n; z++) {
cube[x][y][z] = scan.nextInt();
}
}
}
return cube;
}
public void Print() {
for(int i=0;i<N;i++){
SmallCube sc=scList.get(i);
System.out.println(sc.x + " " + sc.y + " " + sc.z + "\n");
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int m, n, p;
int i;
m = scan.nextInt();
n = scan.nextInt();
p = scan.nextInt();
Main bigcube = new Main();
bigcube.M = m;
bigcube.N = n;
bigcube.P = p;
bigcube.numbers = bigcube.InputCube(scan, m);
bigcube.scList = new ArrayList<SmallCube>();
for (i = 0; i < n; i++) {
SmallCube sc = new SmallCube();
sc.id = i;
sc.LEN = scan.nextInt();
sc.numbers = bigcube.InputCube(scan, sc.LEN);
sc.maxmotions=(bigcube.M-sc.LEN+1)*(bigcube.M-sc.LEN+1)*(bigcube.M-sc.LEN+1);
bigcube.scList.add(sc);
}
bigcube.CheckPosition(0);
bigcube.Print();
}
}
class SmallCube {
public int id;
public int LEN;
public int x = 0;
public int y = 0;
public int z = 0;
public int maxmotions;
public int[][][] numbers;
}
[Exam2] Wireless Routers
Description
Alice just bought a very big house with N rooms and N-1 doors, which each door connects two rooms. Besides, each room have at least one door and at most 3 doors(of course Alice can Go to every room in this house). However, a modern person cannot live without Wifi, so Alice wants to put M wireless routers in several rooms.
As the routers are cheap, only adjacent rooms (which have a door connect to this room) can receive it, and each router works independently. Since M routers may cannot cover every room, Alice tells you the satisfaction point S[i] she could have if room i have Wifi.
Please help Alice to calculate the maximum satisfaction point she can get in total.
Input
The first line is two integers N(2 <= N <= 1000), M(1 <= M <= N, M <= 100) Then are N lines, each shows the satisfaction S[i](1 <= S[i] <= 10) point of room i. Then are N - 1 lines, each contains two
integers x, y, which represents a door is between room x and y.
Output
Just output the maximum point of satisfaction.
Limits
•Memory limit per test: 256 megabytes
•Time limit per test: The faster the better
Compile & Environment
C++
g++ Main.cc -o Main -fno-asm -Wall -lm –static -std=c++0x -DONLINE_JUDGE
Java
J2SE 8
Maximum stack size is 50m
Sample Test
Input
5 1
1 2 3 4 5
2 1
3 2
4 2
5 3
output
10
(C) 2016 Works Applications Co., Ltd., All Rights Reserved.
TEST1 Wireless-Router
import java.util.Scanner;
public class Main {
private int Three=3;
private int Two=2;
private int N;
private int M;
private int[][] doors;
private int[][] rooms;
private int[] weight;
public void InitRoomAdjacent(){
int i,j;
if(doors!=null){
for(i=0;i<N-1;i++){
for(j=0;j<Three;j++){
if(rooms[doors[i][0]-1][j]==0){
rooms[doors[i][0]-1][j]=doors[i][1];
break;
}
}
for(j=0;j<Three;j++){
if(rooms[doors[i][1]-1][j]==0){
rooms[doors[i][1]-1][j]=doors[i][0];
break;
}
}
}
}
}
public int FindSatisfactionOfRemain(){
int temp=0,location=0,max=0;
int i,j;
for(i=0;i<N;i++){
temp=weight[i];
for(j=0;j<Three;j++){
if(rooms[i][j]!=0)
temp=temp+weight[rooms[i][j]-1];
}
if(temp>max){
max=temp;
location=i;
}
}
RemoveWeight(location);
return max;
}
public void RemoveWeight(int location){
int i;
weight[location]=0;
for(i=0;i<Three;i++)
if(rooms[location][i]!=0){
weight[rooms[location][i]-1]=0;
}
}
public int MaxSatisfaction(){
int max=0;
int i;
for(i=0;i<M;i++){
max=max+FindSatisfactionOfRemain();
}
return max;
}
public static void main(String[] args){
Main main=new Main();
Scanner sc = new Scanner(System.in);
int i,j;
main.N=sc.nextInt();
main.M=sc.nextInt();
main.weight=new int[main.N];
for(i=0;i<main.N;i++){
main.weight[i]=sc.nextInt();
}
main.doors=new int[main.N-1][main.Two];
for(j=0;j<main.N-1;j++){
main.doors[j][0]=sc.nextInt();
main.doors[j][1]=sc.nextInt();
}
main.rooms=new int[main.N][main.Three];
main.InitRoomAdjacent();
System.out.println(main.MaxSatisfaction());
sc.close();
}
}