java面试题,质数求和
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
/**
* 求质数和,
* 如:
* sum(1)=2=2
* sum(2)=2+3=5
* sum(3)=2+3+5=10
* sum(4)=2+3+5+7=17
* ...
* sum(7)=2+3+5+7+11+13+17=58
*/
System.out.println(Test.sum(7));
}
//产生质数n的后一个质数,如n=2,return 3
public static int nextNum(int n){
n++;
for(int i=2;i<n;i++){
if(n%i==0){
//不是质数
n++;
continue;
}
}
return n;
}
//求和方法
public static int sum(int count){
if(count <0) return 0;
int sum = 2;
int zn=2;
for(int i=0;i<count-1;i++){
zn=Test.nextNum(zn);
System.out.println(zn);
sum += zn;
}
return sum;
}
}
相关文档:
//=============================输出奇数
public class OddTest {
public static boolean isOdd(int i){
return i % 2 != 0; //比较 i % 2 == 0;注: -1%2 = -1
}
public static void main(String[] args) {
for(int i = -10; i <= 10; i++) {
System.out.println(isOdd(i));
}
}
}
// ......
1、创建了一个对象后:
(1)没有在适当的地方释放掉
(2)在应该释放的地方没有做释放操作
例如:下面一段程序:
m_progressDlg = ProgressDialog.show(this, getString(R.string.market),getString(R.string.is_visiting), true);
new Thread() {
public v ......
//从数组a中删除数组b中存在的元素
String stra[] = {"g","b","c","h","k"};//原始数组
String strb[] = {"g","k"}; //移除的元素
ArrayList list = new ArrayList();
//方法一
for(int i=0;i<stra.length;i++){
int n=0;
......
public static void CentreWnd(Shell shell){
int width = shell.getMonitor().getClientArea().width;
int height = shell.getMonitor().getClientArea().height;
int x = shell.getSize().x;
int y = shell.getSize().y;
if (x > width) {
shell.getSize().x = width;
}
if (y > height) ......
URI 是资源标识符。就是相当于一个人的家庭住址。
URL和URI类似。是资源定位的。 和URI不同的就是URL提供了获取东西的方法。
java.io.InputStream l_urlStream;
// 也可以换成uri,然后调用uri.toURL
  ......