ccf2015 Questions (Java)
Just finished the exam CCF ... Or the basic master of the not solid, five questions 4 hours, only to do three problems, and did not consider the complex situation. The exam time was hurried, so no comment was added.
/* Problem 1 Problem description rotation is the basic operation of image processing, in this problem, you need to rotate an image counterclockwise 90 degrees.
The image representation in the computer can be represented by a matrix, in order to rotate an image, just rotate the corresponding matrix.
The first line entered in the input format contains two integers n, m, representing the number of rows and columns of the image matrix, respectively.
The next n rows contain an m integer for each row, representing the input image.
Output format output m row, each row contains n integers, representing the original matrix counterclockwise rotation 90 degrees after the matrix. Sample Input 2 3 1 5 3 3 2 4 Sample Output 3 4 5 2 1 3 evaluation case scale and convention 1≤n, m≤1,000, the number in the matrix is not more than 1000 nonnegative integers.
* *//My implementation of import Java.util.Scanner;
public class Main {public static void main (string[] args) {main m = new Main ();
M.exam_1 ();
public void Exam_1 () {int n=0,m=0;
Scanner s = new Scanner (system.in);
Get N and M while (true) {n = s.nextint ();
m = S.nextint ();
if (n>=1 && n<=1000 && m>=1 && m<=1000) break;
int array[][] = new Int[n][m];
int array_result[][] =new int[m][n]; Get content for (int row=0;row<n;row++) {for (int clo=0;clo<m;clo++) {Array[row]
[CLO] = S.nextint (); } for (int row_a=0;row_a<n;row_a++) {int temp[] = new Int[m];
for (int clo_a=0;clo_a<m;clo_a++) {temp[clo_a] = array[row_a][clo_a];
for (int clo_b=row_a;clo_b<n;clo_b++) {for (int row_b=m-1;row_b>=0;row_b--) {
Array_result[row_b][clo_b] = Temp[m-1-row_b];
to (int row=0;row<m;row++) {for (int clo=0;clo<n;clo++) {
if (Clo = = n-1) System.out.print (Array_result[row][clo));
else System.out.print (array_result[row][clo]+ "");
} System.out.println (); }
}
}
/* The second problem describes the given n integers, please count the number of occurrences of each integer, in the order of the number of occurrences from more to less output.
The first line entered in the input format contains an integer n that represents the number of the given number.
The second row contains n integers, separated by a space between the adjacent integers, representing the given integer. Output formats output multiple rows, each containing two integers, representing a given integer and the number of times it appears. Output in descending order of occurrences.
If two integers appear as many times, the output value is smaller and the output value is larger.
Sample Input 12 5 2 3 3 1 3 4 2 5 2 3 5 Sample Output 3 4 2 3 5 3 1 1 4 1 Evaluate use case scale and convention 1≤n≤1000, the number given is not more than 1000 nonnegative integers.
* * Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map.Entry;
Import Java.util.Scanner;
public class Main {public static void main (string[] args) {main m = new Main ();
M.exam_2 ();
public void Exam_2 () {int num = 0;
hashmap<string,integer> result = new hashmap<string,integer> ();
Scanner s = new Scanner (system.in);
while (true) {num = S.nextint ();
if (num>=1 && num<=1000) break;
int array[] = new Int[num]; for (int i=0;i<array.length;i++) Array[i] = S.NExtint ();
for (int i:array) {Integer time = Result.get (integer.tostring (i));
if (time = = null) {Result.put (integer.tostring (i), 1);
}else{Result.put (integer.tostring (i), ++time);
} iterator<entry<string,integer>> Itentry = Result.entryset (). iterator ();
arraylist<string> Strarr = new arraylist<string> ();
while (Itentry.hasnext ()) {Strarr.add (Itentry.next (). Getkey ());
int v_arr[] = new int[strarr.size ()];
int temp_ = 0;
for (String S2:strarr) {V_arr[temp_] = result.get (s2);
temp_++; for (int k=1;k<v_arr.length;k++) for (int i=0;i<v_arr.length-k;i++) {if (v_arr[i]<v_a
Rr[i+1]) {int temp = V_arr[i];
V_arr[i] = v_arr[i+1];
V_ARR[I+1] = temp;
} int index = 0;
while (!result.isempty ()) {for (String S2:strarr) {if (Result.get (s2)!= null)
if (result.get (s2) = = V_arr[index]) {System.out.println (s2+ "" +v_arr[index]);
Result.remove (S2);
index++; }
}
}
}
}
The third question is about the time conversion, which is to specify a date is the week, and then let you calculate the number of a month x JoJo a few of the number of a similar problem ... See a headache, just jump over to see the fourth question.
The fourth question is a question about the tree. Probably the idea is to ask the tree farthest two leaf node between the distance (and then consider the topic of some settings), no record of the problem, directly on the code. The code doesn't consider a critical problem. The writing is not perfect
March 30, 2015 10:35:49 add
Lack of consideration of a situation, namely
1
2
3
4 5
In a situation like this, a Layer two layer three layer has only one node, the result of the algorithm is 2. But the correct result should be 3, and you should compare Max with level maximum in the Main method ...
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map.Entry;
Import Java.util.Scanner;
public class Main {public static void main (string[] args) {main m = new Main ();
arraylist<treenode> arr = M.createtree ();
int res = M.getsingle (arr);
int[] arr_in = M.getlastlevel (arr);
int max = arr_in[0]+arr_in[1]-2-2* (res-1);
SYSTEM.OUT.PRINTLN (max);
Public arraylist<treenode> Createtree () {int comput_num=0,router_num = 0;
Scanner s = new Scanner (system.in);
Router_num = S.nextint ();
Comput_num = S.nextint ();
arraylist<treenode> Treearr = new arraylist<treenode> ();
TreeNode root = new TreeNode (1);
Treearr.add (root);
int index = 0;
for (int i=1;i<=comput_num+router_num;i++) {Treearr.add (new TreeNode (i));
} TreeNode root = Treearr.get (0); for (TreeNode t: Treearr) {if (t.getnum () = 1) continue;
int temp = S.nextint ();
TreeNode t_temp = Getnodebyindex (Temp,treearr);
T_temp.addchild (t);
return Treearr; public int Getsingle (arraylist<treenode> treearr) {hashmap<string,integer> ha = new hashmap<
String,integer> ();
for (TreeNode T:treearr) {Integer key = T.getlevel ();
Integer time = Ha.get (integer.tostring (key));
if (time = = null) {Ha.put (integer.tostring (key), 1);
}else{Ha.put (integer.tostring (key), ++time);
int res = 0;
iterator<entry<string,integer>> itentry = Ha.entryset (). iterator ();
while (Itentry.hasnext ()) {if (Itentry.next (). GetValue () = = 1) res++;
int Level_max = treearr.get (0). Getlevel ();TreeNode temp = treearr.get (0);
for (TreeNode T:treearr) {if (Level_max<t.getlevel ()) {Level_max = T.getlevel ();
temp = t;
} if (Ha.get (integer.tostring (level_max)) = = 1) res--;
return res;
Public int[] Getlastlevel (arraylist<treenode> treearr) {int level_max = treearr.get (0). Getlevel ();
TreeNode temp = treearr.get (0);
for (TreeNode T:treearr) {if (Level_max<t.getlevel ()) {Level_max = T.getlevel ();
temp = t;
} treearr.remove (temp);
int level_sec = treearr.get (0). Getlevel ();
TreeNode temp_sec = treearr.get (0);
for (TreeNode T:treearr) {if (Level_sec<t.getlevel ()) {level_sec = T.getlevel ();
Temp_sec = t;
} Return to new Int[]{level_max,level_sec}; Private TreeNode GEtnodebyindex (int index,arraylist<treenode> treearr) {for (TreeNode T:treearr) {if (t.getnum () =
index) return t;
return null;
Class treenode{private arraylist<treenode> Childnode;
private int num;
private int level;
public TreeNode (int num) {this.num = num;
This.level = 1;
Childnode = new arraylist<treenode> (0);
Public TreeNode () {this.num = 0;
Childnode = new arraylist<treenode> (0);
Public arraylist<treenode> Getchildarr () {return this.childnode;
public int Getnum () {return this.num;}
public void AddChild (TreeNode child) {this.childNode.add (child);
Child.setlevel (this.level+1);
public void setnum (int num) {this.num = num; public int Getlevel () {return this. Level;}
public void Setlevel (int level) {this.level = level;}
}
}
The fifth problem is the shortest path of a direction-free graph (which requires additional consideration of the setting in the topic, so it is harder than the shortest path of a simple diagram).