您的位置:首页 > 其它

第三次作业

2016-03-31 00:15 393 查看


public class Prime {
/*******************************************************
* Finds and prints n prime integers
* Jeff Offutt, Spring 2003
******************************************************/
public void printPrimes (int n)
{
int curPrime; // Value currently considered for primeness
int numPrimes; // Number of primes found so far.
boolean isPrime; // Is curPrime prime?
int [] primes = new int [100]; // The list of prime numbers.

// Initialize 2 into the list of primes.
primes [0] = 2;
numPrimes = 1;
curPrime = 2;
while (numPrimes < n)
{
curPrime++; // next number to consider ...
isPrime = true;
for (int i = 0; i <= numPrimes-1; i++)
{ // for each previous prime.
if (curPrime%primes[i] == 0)
{ // Found a divisor, curPrime is not prime.
isPrime = false;
break; // out of loop through primes.
}
}
if (isPrime)
{ // save it!
primes[numPrimes] = curPrime;
numPrimes++;
}
} // End while

// Print all the primes out.
for (int i = 0; i <= numPrimes-1; i++)
{
System.out.println ("Prime: " + primes[i]);
}
} // end printPrimes
}


import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.*;

public class PrimeTest {
Prime p;
PrintStream console = null;          // 声明(为null):输出流 (字符设备) console
ByteArrayOutputStream bytes = null;  // 声明(为null):bytes 用于缓存console 重定向过来的字符流
@Before
public void setup()
{
p = new Prime();
bytes = new ByteArrayOutputStream();    // 分配空间
console = System.out;                   // 获取System.out 输出流的句柄
System.setOut(new PrintStream(bytes));  // 将原本输出到控制台Console的字符流 重定向 到 bytes
}
@After
public void tearDown()
{
System.setOut(console);
}
@Test
public void test() {
p.printPrimes(3);
String s = new String("Prime: 2\r\nPrime: 3\r\nPrime: 5\r\n");    // 注意:控制台的换行,这里用 '\n' 表示
assertEquals("11111111",s, bytes.toString());          // bytes.toString() 作用是将 bytes内容 转换为字符流

}

}


当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)}.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: