第三次作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三次作业相关的知识,希望对你有一定的参考价值。

技术分享

 1 public class Prime {
 2  /******************************************************* 
 3      * Finds and prints n prime integers 
 4      * Jeff Offutt, Spring 2003 
 5      ******************************************************/ 
 6     public void printPrimes (int n) 
 7     { 
 8         int curPrime; // Value currently considered for primeness 
 9         int numPrimes; // Number of primes found so far. 
10         boolean isPrime; // Is curPrime prime? 
11         int [] primes = new int [100]; // The list of prime numbers. 
12         
13         // Initialize 2 into the list of primes. 
14         primes [0] = 2; 
15         numPrimes = 1; 
16         curPrime = 2; 
17         while (numPrimes < n) 
18         { 
19             curPrime++; // next number to consider ... 
20             isPrime = true; 
21             for (int i = 0; i <= numPrimes-1; i++) 
22             { // for each previous prime. 
23                 if (curPrime%primes[i] == 0) 
24                 { // Found a divisor, curPrime is not prime. 
25                     isPrime = false; 
26                     break; // out of loop through primes. 
27                 } 
28             } 
29             if (isPrime) 
30             { // save it! 
31                 primes[numPrimes] = curPrime; 
32                 numPrimes++; 
33             } 
34         } // End while 
35         
36         // Print all the primes out. 
37         for (int i = 0; i <= numPrimes-1; i++) 
38         { 
39             System.out.println ("Prime: " + primes[i]); 
40         } 
41     } // end printPrimes
42 }

 1 import static org.junit.Assert.*;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.PrintStream;
 5 
 6 import org.junit.*;
 7 
 8 public class PrimeTest {
 9     Prime p;
10     PrintStream console = null;          // 声明(为null):输出流 (字符设备) console
11     ByteArrayOutputStream bytes = null;  // 声明(为null):bytes 用于缓存console 重定向过来的字符流
12     @Before
13     public void setup()
14     {
15         p = new Prime();
16         bytes = new ByteArrayOutputStream();    // 分配空间
17         console = System.out;                   // 获取System.out 输出流的句柄
18         System.setOut(new PrintStream(bytes));  // 将原本输出到控制台Console的字符流 重定向 到 bytes
19     }
20     @After
21     public void tearDown() 
22     {
23         System.setOut(console);
24     }
25     @Test
26     public void test() {
27         p.printPrimes(3);
28         String s = new String("Prime: 2\r\nPrime: 3\r\nPrime: 5\r\n");    // 注意:控制台的换行,这里用 ‘\n‘ 表示
29         assertEquals("11111111",s, bytes.toString());          // bytes.toString() 作用是将 bytes内容 转换为字符流
30         
31     }
32 
33 }
当MAXPRIMES等于3或4时。t2=(n=5)会越界。但t1=(n=3)不越界
n=1
节点覆盖{1,2,3,4,5,6,7,8,9,10,11,12}
  {(1,2),(2,3),(3,4),(4,5),(5,6),(6,4),(4,5),(5,7),(7,8),(8,2),(2,9),(9,10),(10,     11),(11,9),(9,12)}
{(1,2),(2,3),(3,4),(4,5),(5,6),(6,4),(4,7),(7,8),(8,2),(2,9),(9,10),(10,     11),(11,9),(9,12)}
基本路径覆盖:={(1,2,10,11),(3,4,8,9,2,10,11,12),(3,4,8,2,10,11,12),(3,4,8,9,2,10,11),(3,4,8,2,10,11),(4,5,7,4),(4,5,6,8,9,2,3,4),(4,5,6,8,2,3,4),(4,8,9,2,3,4),(4,8,2,3,4),(5,7,4,5),(5,6,8,9,2,3,4,5),(1,2,3,4,8,9),(1,2,3,4,5,7),(1,2,3,4,5,6,8,9),(1,2,10,11,12),(2,3,4,8,9,2),(2,3,4,8,2),(2,3,4,5,7),(2,3,4,5,6,8,9,2),(2,3,4,5,6,8,2),(2,10,11,12),(2,10,11),(3,4,5,6,8,9,2,3),(3,4,5,6,8,2,3),(3,4,8,9,2,3),(3,4,8,2,3),(3,4,5,6,8,9,2,10,11,12),(3,4,5,6,8,2,10,11,12),(3,4,5,6,8,9,2,10,11),(3,4,5,6,8,2,10,11),(5,6,8,2,3,4,5),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,7),(6,8,2,3,4,5,7),(7,4,5,7),(7,4,8,9,2,3),(7,4,8,2,3),(7,4,5,6,8,9,2,3),(7,4,5,6,8,2,3),(7,4,5,6,8,9,2,10,11,12),(7,4,5,6,8,9,2,10,11),(7,4,5,6,8,2,10,11,12),(7,4,5,6,8,2,10,11),(7,4,8,9,2,10,11,12),(7,4,8,9,2,10,11),(7,4,8,2,10,11,12),(7,4,8,2,10,11),(8,2,3,4,8),(8,9,2,3,4,8),(8,2,3,4,5,6,8),(8,9,2,3,4,5,6,8),(9,2,3,4,8,9),(9,2,3,4,5,6,8,12)}.

 

以上是关于第三次作业的主要内容,如果未能解决你的问题,请参考以下文章

第三次寒假作业

第三次作业

第三次作业

第三次作业

第三次作业

第三次作业