国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java實現(xiàn)的連續(xù)奇數(shù)(n+2*x)是合數(shù)的算法題暴力算法

Java實現(xiàn)的連續(xù)奇數(shù)(n+2*x)是合數(shù)的算法題暴力算法

2019-11-28 14:18junjie JAVA教程

這篇文章主要介紹了Java實現(xiàn)的連續(xù)奇數(shù)(n+2*x)是合數(shù)的算法題暴力算法,本文包含運算結(jié)果和實現(xiàn)代碼,需要的朋友可以參考下

連續(xù)6個奇數(shù)a,a+2,a+4,a+6,a+8,a+10都是合數(shù),求最小的a的暴力解法。

先上結(jié)果,后面貼上代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
1次連續(xù)n=9,連續(xù)值個數(shù): 1;耗時: 0ms,總計: 0ms
2次連續(xù)n=25,連續(xù)值個數(shù): 1;耗時: 0ms,總計: 0ms
3次連續(xù)n=91,連續(xù)值個數(shù): 1;耗時: 0ms,總計: 0ms
4次連續(xù)n=115,連續(xù)值個數(shù): 3;耗時: 0ms,總計: 1ms
5次連續(xù)n=115,連續(xù)值個數(shù): 3;耗時: 0ms,總計: 1ms
6次連續(xù)n=115,連續(xù)值個數(shù): 3;耗時: 0ms,總計: 1ms
7次連續(xù)n=525,連續(xù)值個數(shù): 2;耗時: 0ms,總計: 1ms
8次連續(xù)n=525,連續(xù)值個數(shù): 2;耗時: 0ms,總計: 1ms
9次連續(xù)n=889,連續(xù)值個數(shù): 1;耗時: 0ms,總計: 1ms
10次連續(xù)n=1131,連續(xù)值個數(shù): 1;耗時: 0ms,總計: 1ms
11次連續(xù)n=1329,連續(xù)值個數(shù): 6;耗時: 0ms,總計: 1ms
12次連續(xù)n=1329,連續(xù)值個數(shù): 6;耗時: 0ms,總計: 1ms
13次連續(xù)n=1329,連續(xù)值個數(shù): 6;耗時: 0ms,總計: 1ms
14次連續(xù)n=1329,連續(xù)值個數(shù): 6;耗時: 0ms,總計: 1ms
15次連續(xù)n=1329,連續(xù)值個數(shù): 6;耗時: 0ms,總計: 1ms
16次連續(xù)n=1329,連續(xù)值個數(shù): 6;耗時: 0ms,總計: 1ms
17次連續(xù)n=9553,連續(xù)值個數(shù): 1;耗時: 2ms,總計: 3ms
18次連續(xù)n=15685,連續(xù)值個數(shù): 4;耗時: 1ms,總計: 5ms
19次連續(xù)n=15685,連續(xù)值個數(shù): 4;耗時: 1ms,總計: 5ms
20次連續(xù)n=15685,連續(xù)值個數(shù): 4;耗時: 1ms,總計: 5ms
21次連續(xù)n=15685,連續(xù)值個數(shù): 4;耗時: 1ms,總計: 5ms
22次連續(xù)n=19611,連續(xù)值個數(shù): 4;耗時: 2ms,總計: 8ms
23次連續(xù)n=19611,連續(xù)值個數(shù): 4;耗時: 2ms,總計: 8ms
24次連續(xù)n=19611,連續(xù)值個數(shù): 4;耗時: 2ms,總計: 8ms
25次連續(xù)n=19611,連續(xù)值個數(shù): 4;耗時: 2ms,總計: 8ms
26次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
27次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
28次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
29次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
30次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
31次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
32次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
33次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
34次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
35次連續(xù)n=31399,連續(xù)值個數(shù): 10;耗時: 5ms,總計: 13ms
36次連續(xù)n=155923,連續(xù)值個數(shù): 7;耗時: 92ms,總計: 105ms
37次連續(xù)n=155923,連續(xù)值個數(shù): 7;耗時: 92ms,總計: 105ms
38次連續(xù)n=155923,連續(xù)值個數(shù): 7;耗時: 92ms,總計: 105ms
39次連續(xù)n=155923,連續(xù)值個數(shù): 7;耗時: 92ms,總計: 105ms
40次連續(xù)n=155923,連續(xù)值個數(shù): 7;耗時: 92ms,總計: 105ms
41次連續(xù)n=155923,連續(xù)值個數(shù): 7;耗時: 92ms,總計: 105ms
42次連續(xù)n=155923,連續(xù)值個數(shù): 7;耗時: 93ms,總計: 106ms
43次連續(xù)n=360655,連續(xù)值個數(shù): 5;耗時: 243ms,總計: 349ms
44次連續(xù)n=360655,連續(xù)值個數(shù): 5;耗時: 243ms,總計: 349ms
45次連續(xù)n=360655,連續(xù)值個數(shù): 5;耗時: 243ms,總計: 349ms
46次連續(xù)n=360655,連續(xù)值個數(shù): 5;耗時: 243ms,總計: 349ms
47次連續(xù)n=360655,連續(xù)值個數(shù): 5;耗時: 243ms,總計: 349ms
48次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
49次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
50次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
51次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
52次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
53次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
54次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
55次連續(xù)n=370263,連續(xù)值個數(shù): 8;耗時: 14ms,總計: 363ms
56次連續(xù)n=492115,連續(xù)值個數(shù): 1;耗時: 185ms,總計: 548ms
57次連續(xù)n=1349535,連續(xù)值個數(shù): 2;耗時: 1854ms,總計: 2402ms
58次連續(xù)n=1349535,連續(xù)值個數(shù): 2;耗時: 1854ms,總計: 2402ms
59次連續(xù)n=1357203,連續(xù)值個數(shù): 7;耗時: 22ms,總計: 2424ms
60次連續(xù)n=1357203,連續(xù)值個數(shù): 7;耗時: 22ms,總計: 2424ms
61次連續(xù)n=1357203,連續(xù)值個數(shù): 7;耗時: 22ms,總計: 2424ms
62次連續(xù)n=1357203,連續(xù)值個數(shù): 7;耗時: 22ms,總計: 2424ms
63次連續(xù)n=1357203,連續(xù)值個數(shù): 7;耗時: 22ms,總計: 2424ms
64次連續(xù)n=1357203,連續(xù)值個數(shù): 7;耗時: 22ms,總計: 2424ms
65次連續(xù)n=1357203,連續(xù)值個數(shù): 7;耗時: 22ms,總計: 2424ms
66次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1889ms,總計: 4313ms
67次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1889ms,總計: 4313ms
68次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1889ms,總計: 4313ms
69次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1889ms,總計: 4313ms
70次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1889ms,總計: 4313ms
71次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1889ms,總計: 4313ms
72次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1889ms,總計: 4313ms
73次連續(xù)n=2010735,連續(xù)值個數(shù): 8;耗時: 1890ms,總計: 4314ms
74次連續(xù)n=4652355,連續(xù)值個數(shù): 3;耗時: 10583ms,總計: 14897ms
75次連續(xù)n=4652355,連續(xù)值個數(shù): 3;耗時: 10583ms,總計: 14897ms
76次連續(xù)n=4652355,連續(xù)值個數(shù): 3;耗時: 10583ms,總計: 14897ms
77次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
78次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
79次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
80次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
81次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
82次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
83次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
84次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86082ms,總計: 100979ms
85次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86083ms,總計: 100980ms
86次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86083ms,總計: 100980ms
87次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86083ms,總計: 100980ms
88次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86083ms,總計: 100980ms
89次連續(xù)n=17051709,連續(xù)值個數(shù): 13;耗時: 86083ms,總計: 100980ms
90次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
91次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
92次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
93次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
94次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
95次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
96次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
97次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
98次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34772ms,總計: 135752ms
99次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34773ms,總計: 135753ms
100次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34773ms,總計: 135753ms
101次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34773ms,總計: 135753ms
102次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34773ms,總計: 135753ms
103次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34773ms,總計: 135753ms
104次連續(xù)n=20831325,連續(xù)值個數(shù): 15;耗時: 34773ms,總計: 135753ms
105次連續(xù)n=47326695,連續(xù)值個數(shù): 5;耗時: 319130ms,總計: 452155ms
106次連續(xù)n=47326695,連續(xù)值個數(shù): 5;耗時: 319131ms,總計: 452156ms
107次連續(xù)n=47326695,連續(xù)值個數(shù): 5;耗時: 319131ms,總計: 452156ms
108次連續(xù)n=47326695,連續(xù)值個數(shù): 5;耗時: 319131ms,總計: 452156ms
109次連續(xù)n=47326695,連續(xù)值個數(shù): 5;耗時: 319131ms,總計: 452156ms
110次連續(xù)n=122164749,連續(xù)值個數(shù): 1;耗時: 1395200ms,總計: 1847356ms
111次連續(xù)n=189695661,連續(xù)值個數(shù): 6;耗時: 1705936ms,總計: 3553292ms
112次連續(xù)n=189695661,連續(xù)值個數(shù): 6;耗時: 1705936ms,總計: 3553292ms
113次連續(xù)n=189695661,連續(xù)值個數(shù): 6;耗時: 1705936ms,總計: 3553292ms
114次連續(xù)n=189695661,連續(xù)值個數(shù): 6;耗時: 1705936ms,總計: 3553292ms
115次連續(xù)n=189695661,連續(xù)值個數(shù): 6;耗時: 1705936ms,總計: 3553292ms
116次連續(xù)n=189695661,連續(xù)值個數(shù): 6;耗時: 1705936ms,總計: 3553292ms
117次連續(xù)n=191912785,連續(xù)值個數(shù): 7;耗時: 61964ms,總計: 3615256ms
118次連續(xù)n=191912785,連續(xù)值個數(shù): 7;耗時: 61964ms,總計: 3615256ms
119次連續(xù)n=191912785,連續(xù)值個數(shù): 7;耗時: 61964ms,總計: 3615256ms
120次連續(xù)n=191912785,連續(xù)值個數(shù): 7;耗時: 61964ms,總計: 3615256ms
121次連續(xù)n=191912785,連續(xù)值個數(shù): 7;耗時: 61964ms,總計: 3615256ms
122次連續(xù)n=191912785,連續(xù)值個數(shù): 7;耗時: 61964ms,總計: 3615256ms
123次連續(xù)n=191912785,連續(xù)值個數(shù): 7;耗時: 61964ms,總計: 3615256ms
124次連續(xù)n=387096135,連續(xù)值個數(shù): 1;耗時: 6650201ms,總計: 10265457ms
-----
本次已經(jīng)跑完了,下一個值超出了1000次;無用耗時: 0ms,總計: xxxxxx135395ms

。。。。。。 后面的結(jié)果還沒算出來

代碼如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.test.test.zhihe;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/**
 * 連續(xù)6個奇數(shù)a,a+2,a+4,a+6,a+8,a+10都是合數(shù),求最小的a
 */
public class ZhishuTest {
 /**
 * 判斷某個數(shù)是否是合數(shù). 相較于質(zhì)數(shù)
 * @param num
 * @return
 */
 public static boolean He(int num){
 // 平方根
 int sq = ((Double)Math.sqrt(num)).intValue();
 // 2 ...... sq
 for (int i = 2; i <= sq; i++) {
  int mo = num % i;
  if(0 == mo){
  return true;
  }
 }
 //
 return false;
 }
 
 /**
 * 主函數(shù)
 * @param args
 */
 public static void main(String[] args) {
 test();
 }
 public static void test() {
 // 開始時間
 long startMillis = System.currentTimeMillis();
 // 上次完成時間
 long preMillis = System.currentTimeMillis();
 // 本次完成時間
 long curMillis = System.currentTimeMillis();
 //
 int lianxu = 1000;
 int start = 1;
 int times = 1;
 for (int x = 1; x <= lianxu; x++) {
  if(times > x){
  continue;// 跳過,進(jìn)入下一次循環(huán)
  } else {
  times = x;
  }
  List<Map<Integer, Integer>> resList = testTimesHe(x, start, false);
  //
  // 如果有數(shù)字,則進(jìn)行處理
  if(null == resList || resList.isEmpty()){
  // 找不到,就不會再有下一個了...
  // 深層嵌套太惡心了。。。
  break;
  }
  int size = resList.size();
  // 遍歷
  Iterator<Map<Integer, Integer>> iteratorR = resList.iterator();
  while (iteratorR.hasNext()) {
  Map<Integer, Integer> map = (Map<Integer, Integer>) iteratorR.next();
  //
  if(null != map && !map.isEmpty()){
   // Map遍歷太惡心了.爛Java
   Set<Integer> keys= map.keySet();
   Iterator<Integer> iteratorK = keys.iterator();
   if(iteratorK.hasNext()){
   Integer key = iteratorK.next(); // 次數(shù)
   Integer value = map.get(key); // 最小n
   //
   // 本次完成時間
   curMillis = System.currentTimeMillis();
   //
   long allTimeout = curMillis - startMillis;
   long curTimeout = curMillis - preMillis;
   System.out.println(""+key+"次連續(xù)n="+value +",連續(xù)值個數(shù): "+size +
    ";耗時: " + curTimeout + "ms,總計: "+allTimeout+"ms");
   // 處理數(shù)據(jù),貪婪處理過的就不處理了
   if(key > 0 && value > 0){
    times = key+1;
    start = value;
   }
   }
  }
  }
  // 計入上次完成時間
  preMillis = System.currentTimeMillis();
 }
 //
 // 本次完成時間
 curMillis = System.currentTimeMillis();
 //
 long allTimeout = curMillis - startMillis;
 long curTimeout = curMillis - preMillis;
 System.out.println("本次已經(jīng)跑完了,下一個值超出了100次 " +
  ";無用耗時: " + curTimeout + "ms,總計: "+allTimeout+"ms");
 }
 
 
 /**
 *
 * 測試 times 次的+2都是合數(shù)的最小n
 * @param times 計算次數(shù)
 * @param start 起始數(shù)字
 * @param onlyStart 只計算單個start值.用于遞歸.外部調(diào)用應(yīng)該傳入
 * @return
 */
 public static List<Map<Integer, Integer>> testTimesHe(int times,int start, boolean onlyStart) {
 //
 List<Map<Integer, Integer>> resList= new ArrayList<Map<Integer, Integer>>();
 //
 // 防御式編程
 if(start < 1){
  return resList;
 }
 if(0 == start % 2){ // 不處理偶數(shù)
  return resList;
 }
 if(times < 1){
  times = 1;
 }
 //
 int result = -1;
 //
 for (int i = start; i < Integer.MAX_VALUE; i+=2) {
  //
  // 避免一直計算不返回
  if(onlyStart && i > start){ // start 不滿足,就直接
  return resList;
  }
  for (int j = 0; j < times; j++) {
  int n = i + 2*j;
  //
  if(!He(n)){
   break;// 內(nèi)層退出
  }
  //
  if(j+1 == times){
   // 跑到結(jié)果了. times 次都滿足
   result = i;
   break;// 這里退不退無所謂,跑到for的最后了
  }
  }
  //
  if(result > 0){
  //
  //System.out.println("result = "+result);
  //
  Map<Integer, Integer> resMap = new HashMap<Integer, Integer>();
  resMap.put(times, result);
  resList.add(resMap);
  // 嘗試下一個次數(shù),遞歸; 其實這個遞歸還可以繼續(xù)優(yōu)化一點; 貪婪算法,直接加下一次。。。
  // startTimes, 直接加這個參數(shù)。。。貪婪遞歸?
  // 多1次,從result這個數(shù)開始
  int t = times +1;
  int s = result;
  List<Map<Integer, Integer>> nextList = testTimesHe(t, s, true);
  // 如果有下一層的數(shù)字,則加入到當(dāng)前結(jié)果
  if(null != nextList && false==nextList.isEmpty()){
   resList.addAll(nextList);
  }
  
  //
  break;// 外層退出
  }
 }
 //
 return resList;
 }
}

說明: 還有改進(jìn)空間,歡迎下次修正

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品99久久免费观看 | 国产一区二区视频在线 | 精品国产99| 91精品国产色综合久久不卡蜜臀 | 成人aⅴ视频 | 成人国产电影 | 一久久久 | 亚洲精品视频观看 | 五月婷婷在线视频 | a级在线观看 | 黄在线免费观看 | 婷婷久久综合九色综合绿巨人 | 国产精品爱久久久久久久 | 免费观看在线午夜影视 | 在线免费观看黄色 | av色伊人久久综合一区二区 | 国产成年人网站 | 二区在线视频 | 午夜看片 | 婷婷天堂 | 国产在线拍揄自揄拍视频 | 一本大道伊人久久综合 | 久久久91精品国产一区二区三区 | 都市激情综合 | 日韩中文一区二区三区 | 91精品国产综合久久久久久 | 国产日韩欧美综合 | 日韩免费| 欧美性久久 | 欧州一级片 | 精品久久av | 欧美日韩免费 | 亚洲高清电影 | 综合网视频 | 一区二区精品在线 | 亚洲精品一区二三区不卡 | 国产一区日韩在线 | 亚洲一级在线 | 免费亚洲网站 | 久久国产区 | 在线观看亚洲 |