13,100
社区成员
发帖
与我相关
我的任务
分享
//PI.java
public class PI{
public static void main(String [] args){
final double eps =0.00000001,MAX = 100000000;
double result = 0, result1 = 0;
int i = 1; //i表示第几项
do{
result = result1;//用result保存当前计算的PI值
result1 += Math.pow(-1,i+1) /(double)(2*i-1); //将每项和累加result中
//更新PI值
i++;
}while(Math.abs(result1 - result) > eps && i <= MAX); //控制精度在eps之内
System.out.println( "PI的值为:PI = " + 4 * result);
}
}
//Random.java
import java.util.*;
public class Random{
public static void main(String args[]){
int[] RInt = new int[25]; //为整型数组对象分配内存空间
Random randomInts = new Random();
for(int i = 0; i < 25; i++){
//RInt[i] = (int)(99 * Math.random());
RInt[i] = randomInts.nextInt(100);
//System.out.println("第" + (i+1) + "随机数是:" + RInt[i]);
if(i == 0){ //第一个随机整数直接输出,之前没有整数
System.out.println("第1次" + RInt[i] );
}
else{ //判断第(i+1)个整数与第i个整数的大小关系
if(RInt[i] < RInt[i-1]){
System.out.println("第" + (i+1) + "次" + RInt[i] + " < " + RInt[i-1] );
}
else if(RInt[i] > RInt[i-1]){
System.out.println("第" + (i+1) + "次" + RInt[i] + " > " + RInt[i-1] );
}
else{
System.out.println("第" + (i+1) + "次" + RInt[i-1] + " == " + RInt[i] );
}
}
}
}
}
public class Primer{
public static void main(String args[]){
for(int i = 101; i <= 200; i++){ //第一个for循环控制i数的范围
for(int j = 2; j <= Math.floor(Math.sqrt(i)) ; j++){ //第二个for循环判断i是否为素数
if(i % j ==0){
//System.out.println(i + "不是素数!");
break;
}
if ( j == Math.floor(Math.sqrt(i)))
System.out.println(i + "是素数!");
}
}
}
}
public class MatrixCalculate{
public static int [][] add(int [][] a, int [][] b){ //实现矩阵加法运算的方法
int c[][] = new int[a.length][a[0].length];
for(int i = 0 ; i< a.length ; i++)
for(int j = 0 ; j < a[0].length ; j++){
c[i][j] = a[i][j] + b[i][j];
}
return c;
}
public static int [][] mul(int [][] a, int [][] b){ //实现矩阵乘法的方法
int c[][] = new int[a.length][b[0].length];
for(int i = 0 ; i< a.length ; i++)
for(int j = 0 ; j < a[0].length ; j++){
for(int k = 0 ; k < a[0].length ; k++)
c[i][j] += a[i][k] * b[k][j] ;
}
return c;
}
public static void main(String [] args){
int a[][] = { {1,0,0}, {0,1,0} , {0,0,1} };
int b[][] = { {1,1,1}, {2,2,2} , {3,3,3} };
int AddResult[][] = new int[3][];
int MulResult[][] = new int[3][];
AddResult = add(a,b);
MulResult = mul(a,b);
System.out.println("*********实现矩阵加法**********");
for(int i = 0 ; i< a.length ; i++)
for(int j = 0 ; j < a[0].length ; j++){
System.out.println("AddResult[" + i +"]" + "[" + j +"] = " + AddResult[i][j] );
}
System.out.println("*********实现矩阵乘法************");
for(int i = 0 ; i< a.length ; i++)
for(int j = 0 ; j < b[0].length ; j++){
System.out.println("MulResult[" + i +"]" + "[" + j +"] = " + MulResult[i][j] );
}
}
}
public class BubbleSort{
public static void sort(int [] a){
for(int i = 0 ; i< a.length - 1 ; i++){
for(int j = i + 1 ; j < a.length ; j++){
if(a[i] > a[j]){
int temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
}
}
}
public static void main(String [] args){
int [] a ={10,20,25,9,15,30,5,8,16,19} ;
sort(a);
for(int i = 0; i < a.length ; i++){
System.out.println("The sorted Number Array is: " + a[i] );
}
}
}
public class BinarySearch{
public static int binSearch(int [] a, int key){
int left = 0;
int right = a.length - 1;
int middle ;
while(left <= right){
middle = (left + right) / 2;
if(a[middle] == key){
return middle;
}
else if(key < a[middle]){
right = middle ;
}
else{
left = middle ;
}
}
return -1;
}
public static void main(String [] args){
int [] a = new int[10];
for(int i =0 ; i < 10 ; i++){
a[i] = i + 10;
}
System.out.println(binSearch(a,13));
}
}
public static void main(String[] args) {
// 除了2,所有素数都是奇数
for (int n = 101; n <= 200; n += 2)
if (isPrimer(n))
System.out.println(n + " 是素数");
}
static boolean isPrimer(int n) {
// 根据素数的定义,只有大于1的整数才有意义
if (n <= 1)
throw new IllegalArgumentException();
// 2,3是素数
if (n < 4)
return true;
// 如果是大于2的偶数,则肯定不是素数
if ((n & 1) == 0)
return false;
// 计算平方值
int max = (int) Math.sqrt(n);
// 从3开始计算是否可以被整除
for (int i = 3; i <= max; i += 2)
if (n % i == 0)
return false;// 如果可以整除,则不是素数
return true;
}
public class Test {
public static void main(String[] args) {
// π/4=1/1 - 1/3 + 1/5 - 1/7 + ...
// 分母每次加2,每项的符号,如果前一项是正,则当前项为负,如果前一项是负,当前项为正,即乘上-1
final double eps = 1e-8;
double quarterPi = 0;
double sum = 1.0;
int x = 3;
int flag = -1;
while (Math.abs(sum - quarterPi) > eps) {
quarterPi = sum;
sum += flag * 1.0 / x;
x += 2;
flag *= -1;
}
System.out.println(quarterPi * 4);
}
}