码迷,mamicode.com
首页 > 其他好文 > 详细

第三次作业

时间:2016-03-31 01:53:15      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 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)}.

 

第三次作业

标签:

原文地址:http://www.cnblogs.com/c337134154/p/5339576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!