SHOW:
|
|
- or go back to the newest paste.
1 | - | /* Chương trinhf giải */ |
1 | + | /* Chương trình giải */ |
2 | import java.util.LinkedList; | |
3 | /** | |
4 | * | |
5 | * @author KimDinhSon | |
6 | */ | |
7 | public class VMO04 { | |
8 | public LinkedList<Integer> arr=new LinkedList<Integer>(); | |
9 | public LinkedList<Integer> Arr=new LinkedList<Integer>(); | |
10 | private int N=2013; | |
11 | ||
12 | int CountA=0, CountB=0;// Dem so buoc thuc hien | |
13 | public void init(){ | |
14 | // Du lieu phan b) | |
15 | for(int i=0;i<999;i++){ | |
16 | arr.add(i, i+1); | |
17 | } | |
18 | // Du lieu phan a) | |
19 | Arr.addFirst(1); | |
20 | Arr.addLast(1000); | |
21 | } | |
22 | public int resulfA(){ | |
23 | int i=0,KQ=0; | |
24 | while(i<Arr.size()){ | |
25 | if(Arr.get(i)==N){ | |
26 | KQ++; | |
27 | i++; | |
28 | } | |
29 | else i++; | |
30 | } | |
31 | return KQ; | |
32 | } | |
33 | public int resulfB(){ | |
34 | int i=0,KQ=0; | |
35 | ||
36 | while(i<arr.size()){ | |
37 | if(arr.get(i)==N){ | |
38 | KQ++; | |
39 | i++; | |
40 | } | |
41 | else i++; | |
42 | } | |
43 | ||
44 | return KQ; | |
45 | } | |
46 | public void addNumber(){ | |
47 | // Part a | |
48 | while(CountA<N){ | |
49 | int i=1; | |
50 | while(i<Arr.size()){ | |
51 | // Buoc dien thu i+1 | |
52 | if(Arr.get(i)<N+1){ | |
53 | Arr.add(i, Arr.get(i-1)+Arr.get(i));// Dien so moi vao mang bang tong Arr[i-1]+Arr[i] | |
54 | i+=2; | |
55 | } | |
56 | else break; | |
57 | } | |
58 | CountA++; | |
59 | i=1; | |
60 | } | |
61 | // Part b | |
62 | while(CountB<N){ | |
63 | int i=1; | |
64 | while(i<arr.size()){ | |
65 | // Buoc dien thu i+1 | |
66 | if(arr.get(i)<N+1){ | |
67 | arr.add(i, arr.get(i-1)+arr.get(i));// Dien so moi vao mang bang tong Arr[i-1]+Arr[i] | |
68 | i+=2; | |
69 | } | |
70 | else break; | |
71 | } | |
72 | CountB++; | |
73 | i=1; | |
74 | } | |
75 | } | |
76 | public void printArrA(){ | |
77 | for(int i=0;i<Arr.size();i++){ | |
78 | if(Arr.get(i)==N)// Chi hien thi ptu bang 2013 | |
79 | System.out.print("A["+i+"]="+Arr.get(i)+" ;"); | |
80 | } | |
81 | System.out.println(); | |
82 | } | |
83 | public void printArrB(){ | |
84 | for(int i=0;i<arr.size();i++){ | |
85 | if(arr.get(i)==N){// Chi hien thi ptu bang 2013 | |
86 | System.out.print("B["+i+"]="+arr.get(i)+" ;"); | |
87 | if(i%10==0)System.out.println(); | |
88 | } | |
89 | } | |
90 | System.out.println(); | |
91 | } | |
92 | public static void main(String[] args) { | |
93 | VMO04 pro4 = new VMO04(); | |
94 | pro4.init(); | |
95 | pro4.addNumber(); | |
96 | System.out.print("Kết quả câu a là: "+pro4.resulfA()+" lần số "+pro4.N+" xuất hiện.\n"); | |
97 | pro4.printArrA(); | |
98 | System.out.println(); | |
99 | System.out.print("Kết quả câu b là: "+pro4.resulfB()+" lần số "+pro4.N+" xuất hiện.\n"); | |
100 | pro4.printArrB(); | |
101 | ||
102 | } | |
103 | } | |
104 | ////////////////////////////////////////////////////////////////////////////////////////// | |
105 | - | // Output: |
105 | + | // Output: |
106 | Kết quả câu a là: 2 lần số 2013 xuất hiện. | |
107 | A[2]=2013 ;A[2016]=2013 ; | |
108 | ||
109 | Kết quả câu b là: 40 lần số 2013 xuất hiện. | |
110 | B[2]=2013 ;B[2016]=2013 ;B[4701]=2013 ;B[5708]=2013 ;B[7779]=2013 ;B[8498]=2013 ;B[10257]=2013 ;B[12842]=2013 ;B[14151]=2013 ;B[14711]=2013 ;B[19149]=2013 ;B[20337]=2013 ;B[25519]=2013 ;B[29699]=2013 ;B[37168]=2013 ;B[38545]=2013 ;B[42517]=2013 ;B[50703]=2013 ;B[57069]=2013 ;B[60499]=2013 ;B[61717]=2013 ;B[64309]=2013 ;B[120671]=2013 ;B[169579]=2013 ;B[170797]=2013 ;B[172609]=2013 ;B[179955]=2013 ;B[183493]=2013 ;B[190765]=2013 ;B[192058]=2013 ;B[198611]=2013 ;B[206657]=2013 ;B[207685]=2013 ;B[211515]=2013 ;B[213149]=2013 ;B[215347]=2013 ;B[216962]=2013 ;B[217545]=2013 ;B[219386]=2013 ;B[220195]=2013 ; | |
111 | BUILD SUCCESSFUL (total time: 5 minutes 21 seconds) |