java算法：数组

Java代码
1. publicclassPrimes{
2. publicstaticvoidmain(String[]args){
3. intN=Integer.parseInt(args[0]);
4. boolean[]a=newboolean[N];
5. for(inti=2;i
6. a[i]=true;
7. }
8. for(inti=2;i
9. if(a[i]!=false){
10. for(intj=i;j*i
11. a[i*j]=false;
12. }
13. }
14. }
15. for(inti=2;i
16. if(i>N-100){
17. if(a[i]){
18. System.out.println(""+i);
19. }
20. }
21. }
22. }
23. }
``````public class Primes {
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 ; i++){
if(i > N - 100){
if(a [i]){
System.out.println(" " + i);
}
}
}
}

}``````

Java代码
1. boolean[]a;
2. try{
3. a=newboolean[N];
4. }catch(OutOfMemoryExceptione){
5. System.out.println("Outofmemory.");
6. }
``````boolean [] a;
try{
a = new boolean[N];
}catch(OutOfMemoryException e){
System.out.println("Out of memory.");
}``````

Java代码
1. publicclassCoinFlip{
3. returnMath.random()<0.5;
4. }
5. publicstaticvoidmain(String[]args){
6. inti,j,cnt=0;
7. intN=Integer.parseInt(args[0]);
8. intM=Integer.parseInt(args[1]);
9. int[]f=newint[N+1];
10. for(j=0;j<=N;j++){
11. f[j]=0;
12. }
13. for(i=0;i
14. for(cnt=0,j=0;j<=N;j++){
16. cnt++;
17. }
18. }
19. }
20. for(j=0;j<=N;j++){
21. if(f[j]==0){
22. System.out.print(".");
23. }
24. for(i=0;i10){
25. System.out.print("*");
26. }
27. System.out.println("");
28. }
29. }
30. }
``````public class CoinFlip {
return Math.random() < 0.5;
}
public static void main(String[] args) {
int i,j,cnt = 0;
int N = Integer.parseInt(args[0]);
int M = Integer.parseInt(args[1]);
int  [] f = new int[N + 1];
for (j = 0; j <= N; j++) {
f[j]=0;
}
for (i = 0; i < M; i++, f[cnt]++) {
for(cnt = 0, j = 0; j <= N; j++){
cnt++ ;
}
}
}
for(j = 0; j <= N; j++){
if(f[j] == 0){
System.out.print(".");
}
for(i = 0; i < f[j]; i+= 10){
System.out.print("*");
}
System.out.println("");
}
}
}``````

Java代码
1. publicclassClosePoints{
2. publicstaticvoidmain(String[]args){
3. intcnt=0;
4. intN=Integer.parseInt(args[0]);
5. doubled=Double.parseDouble(args[1]);
6. Point[]a=newPoint[N];
7. for(inti=0;i
8. a[i]=newPoint();
9. }
10. for(inti=0;i
11. for(intj=i+1;j
12. if(a[i].distance(a[j])
13. cnt++;
14. }
15. }
16. }
17. System.out.println(cnt+"pairscloserthan"+d);
18. }
19. }
``````public class ClosePoints {

public static void main(String[] args) {
int cnt = 0;
int N = Integer.parseInt(args[0]);
double d = Double.parseDouble(args[1]);
Point [] a = new Point[N];
for(int i = 0; i < N; i++){
a[i] = new Point();
}
for(int i = 0; i < N; i++){
for(int j = i + 1; j < N; j++){
if(a[i].distance(a[j]) < d){
cnt++;
}
}
}
System.out.println(cnt + " pairs closer than " + d);
}

}``````