• 第三次作业


     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
    Prime: 3
    Prime: 5
    ");    // 注意:控制台的换行,这里用 '
    ' 表示
    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)}.
  • 相关阅读:
    Go 语言机制之逃逸分析
    类型转换和类型断言
    浅析rune数据类型
    Go 文件操作(创建、打开、读、写)
    字符编码笔记:ASCII,Unicode 和 UTF-8
    cmd.exe启动参数详解
    linux下.so、.ko、.a的区别
    Python 和C#的交互
    Innodb表压缩过程中遇到的坑(innodb_file_format)
    更改mysql的加密方式和密码策略
  • 原文地址:https://www.cnblogs.com/c337134154/p/5339576.html
Copyright © 2020-2023  润新知