Home > Back-end >  I want to cry, how to use the sieve method, the prime Numbers from 1 to 1000 I wrote the following c
I want to cry, how to use the sieve method, the prime Numbers from 1 to 1000 I wrote the following c

Time:10-19

Package sushu;

Public class Sushu1 {
Public static void main (String [] args) {
//TODO Auto - generated method stub
Int [] data;
data=https://bbs.csdn.net/topics/new int [1000].
System. The out. Println (" primes within 1 ~ 1000 as shown in the following ");

InputData (data);

CalcPrimeNum (data);

PrintPrimeNum (data);
}
//definition method 1: input 1 ~ 1000 data (data assignment method)
Public static void InputData (int [] data) {
for(int i=0; iint i=0;
Data [I]=I + 1;
}
}
//definition method 2: remove all the non-prime number in the input data
Public static void CalcPrimeNum (int [] data) {
Int I, j, temp.
Data [0]=0;
for(i=1; iFor (j=I + 1; J<1000; J++) {
If (data [j].=0) {
Temp=data [j] % data [I];
If (temp==0) {
Data [I]=0;
}
}
}
}
}

//definition method 3: a line of print format to print all the primes of 10 prime
Public static void PrintPrimeNum (int [] data) {
Int the items=0;
for(int i=0; iIf (data [I]!=0) {
System. The out. Printf (" % 5 d, "data [I]);
Items++;
If (10 items %==0)
System.out.println();

}

}
}

}

CodePudding user response:

Wrong, look at the
Public class Sushu1 {
Public static void main (String [] args) {
//TODO Auto - generated method stub
Int [] data;
data=https://bbs.csdn.net/topics/new int [1000].
System. The out. Println (" primes within 1 ~ 1000 as shown in the following ");

InputData (data);

CalcPrimeNum (data);

PrintPrimeNum (data);
}
//definition method 1: input 1 ~ 1000 data (data assignment method)
Public static void InputData (int [] data) {
for(int i=0; iData [I]=I + 1;
}
}
//definition method 2: remove all the non-prime number in the input data
Public static void CalcPrimeNum (int [] data) {
Int I, j, temp.
Data [0]=0;
for(i=1; iFor (j=I + 1; J<1000; J++) {
If (data [j].=0) {
Temp=data [j] % data [I];
If (temp==0) {
Data [I]=0;
}
}
}
}
}

//definition method 3: a line of print format to print all the primes of 10 prime
Public static void PrintPrimeNum (int [] data) {
Int the items=0;
for(int i=0; iIf (data [I]!=0) {
System. The out. Printf (" % 5 d, "data [I]);
Items++;
If (10 items %==0)
System.out.println();

}

}
}

}
  • Related