@@ -49,7 +49,7 @@ int main() {
4949 total += i;
5050 }
5151 printf ("%d\n", total);
52- return 0;
52+ return 0;
5353}
5454```
5555
@@ -100,52 +100,40 @@ import java.util.List;
100100import java.util.ArrayList ;
101101import java.util.Collections ;
102102import java.util.Scanner ;
103+ import java.util.concurrent.ThreadLocalRandom ;
103104
104105class Example03 {
105-
106- /**
107- * 产生[min, max)范围的随机整数
108- */
109- public static int randomInt (int min , int max ) {
110- return (int ) (Math . random() * (max - min) + min);
106+ private static final List<Integer > RED_BALLS = new ArrayList<> ();
107+ static {
108+ for (int i = 1 ; i <= 33 ; ++ i) RED_BALLS . add(i);
111109 }
112110
113- /**
114- * 输出一组双色球号码
115- */
116111 public static void display (List<Integer > balls ) {
117- for (int i = 0 ; i < balls. size(); ++ i) {
118- System . out. printf(" %02d " , balls. get(i));
119- if (i == balls. size() - 2 ) {
120- System . out. print(" | " );
121- }
112+ StringBuilder sb = new StringBuilder ();
113+ for (int i = 0 ; i < balls. size(); i++ ) {
114+ sb. append(String . format(" %02d " , balls. get(i)));
115+ if (i == 5 ) sb. append(" | " );
122116 }
123- System . out. println();
117+ System . out. println(sb . toString() );
124118 }
125119
126- /**
127- * 生成一组随机号码
128- */
129120 public static List<Integer > generate () {
130- List<Integer > redBalls = new ArrayList<> ();
131- for (int i = 1 ; i <= 33 ; ++ i) {
132- redBalls. add(i);
133- }
134- List<Integer > selectedBalls = new ArrayList<> ();
135- for (int i = 0 ; i < 6 ; ++ i) {
136- selectedBalls. add(redBalls. remove(randomInt(0 , redBalls. size())));
137- }
121+ List<Integer > pool = new ArrayList<> (RED_BALLS );
122+ Collections . shuffle(pool);
123+ List<Integer > selectedBalls = pool. subList(0 , 6 );
138124 Collections . sort(selectedBalls);
139- selectedBalls. add(randomInt (1 , 17 ));
125+ selectedBalls. add(ThreadLocalRandom . current() . nextInt (1 , 17 ));
140126 return selectedBalls;
141127 }
142128
143129 public static void main (String [] args ) {
144130 try (Scanner sc = new Scanner (System . in)) {
145131 System . out. print(" 机选几注: " );
146- int num = sc. nextInt();
147- for (int i = 0 ; i < num; ++ i) {
148- display(generate());
132+ if (sc. hasNextInt()) {
133+ int num = sc. nextInt();
134+ for (int i = 0 ; i < num; ++ i) {
135+ display(generate());
136+ }
149137 }
150138 }
151139 }
@@ -155,25 +143,23 @@ class Example03 {
155143Python的版本:
156144
157145``` Python
158- from random import randint, sample
146+ import random
147+
148+ RED_RANGE = range (1 , 34 )
149+ BLU_RANGE = range (1 , 17 )
159150
160151
161152def generate ():
162- """ 生成一组随机号码"""
163- red_balls = [x for x in range (1 , 34 )]
164- selected_balls = sample(red_balls, 6 )
165- selected_balls.sort()
166- selected_balls.append(randint(1 , 16 ))
167- return selected_balls
153+ red_balls = random.sample(RED_RANGE , 6 )
154+ red_balls.sort()
155+ blue_ball = random.choice(BLU_RANGE )
156+ return red_balls, blue_ball
168157
169158
170- def display (balls ):
171- """ 输出一组双色球号码"""
172- for index, ball in enumerate (balls):
173- print (f ' { ball:0>2d } ' , end = ' ' )
174- if index == len (balls) - 2 :
175- print (' |' , end = ' ' )
176- print ()
159+ def display (sample ):
160+ reds, blue = sample
161+ reds_str = ' ' .join(f ' { ball:02d } ' for ball in reds)
162+ print (f ' { reds_str} | { blue:02d } ' )
177163
178164
179165num = int (input (' 机选几注: ' ))
@@ -632,7 +618,7 @@ cProfile.run('list(PrimeIter(10000))')
632618 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
633619```
634620
635- ####line_profiler
621+ #### line_profiler
636622
637623给需要剖析时间性能的函数加上一个` profile ` 装饰器,这个函数每行代码的执行次数和时间都会被剖析。
638624
@@ -693,7 +679,7 @@ Function: is_prime at line 1
693679 6 1000 386.0 0.4 0.4 return True
694680` ` `
695681
696- # ###memory_profiler
682+ # ### memory_profiler
697683
698684给需要剖析内存性能的函数加上一个` profile` 装饰器,这个函数每行代码的内存使用情况都会被剖析。
699685
0 commit comments