# Flyingis

Talking and thinking freely !
Flying in the world of GIS !

## 理解数组和链表的最基本特性

/**

* 根据命令行输入的N值，计算出所有小于N的素数

* 是素数将数组元素值设为true，否则设为false

*/

class ArrayApp {

public static void main(String[] args) {

int N = Integer.parseInt(args[0]);

boolean[] a = new boolean[N];

for (int i = 2; i < N; i++)

a[i] = true;

for (int i = 2; i < N; i++)

if (a[i] != false)

for (int j = i; j*i < N; j++)

a[i*j] = false;

for (int i = 2; i < N; j++)

if (a[i])

System.out.println(“” + i);

}

}

/**

* N个有编号的小球围成一圈，每个M-1个就拿去一个小球，计算最后剩下的球的位置

*/

static class Node {

int value;

Node next;

Node (int v) { v = value; }

}

public static void main(String[] args) {

int N = Integer.parseInt(args[0]);

int M = Integer.parseInt(args[1]);

Node first = new Node(1);

Node x = first;

for (int i = 2; i <= N; i++)

x = (x.next = new Node(i));

x.next = first;

while (x != x.next) {

for (int i = 1; i < M; i++)

x = x.next;

x.next = x.next.next;

}

System.out.println(“最后剩下的小球：” + x.value);

}

}

posted on 2006-01-24 23:42 Flyingis 阅读(2125) 评论(0)  编辑  收藏 所属分类: Algorithm

 只有注册用户登录后才能发表评论。 网站导航: 相关文章: