java算法小程序(一)

1.求101到200之间有多少个素数,输出所有的素数

//1.101到200之间有多少个素数,输出所有的素数
/**
 * 判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数。反之则是。
 * 
 * java.lang.Math.sqrt(double a) 返回正确舍入的一个double值的正平方根。特殊情况:
 * 如果参数是NaN或小于为零,那么结果是NaN.
 * 如果参数是正无穷大,那么结果为正无穷大.
 * 如果参数是正零或负零,那么结果是一样的参数.
 */
public class Test1 {

	public static void main(String[] args) {
		int count = 0;// 计算素数的数量
		for (int i = 101; i <= 200; i++) {
			boolean b = true;// 用来判断是不是素数
			for (int j = 2; j <= Math.sqrt(i); j++) {
				if (i % j == 0) {
					b = false;// 不是素数
					break;
				}
			}
			if (b) {
				count++;
				System.out.println(i + "是素数");
			}
		}
		System.out.println("素数的个数为:" + count);

	}

}

2.冒泡排序数组

//2.冒泡排序数组
/**
 * 在要排列的一组数中,对当前还未排好序的范围内的全部数,
 * 自上而下对相邻的两个数一次比较,让较大的数往下沉,较小的往上冒。
 * 
 */
public class Test2 {
	
	public static void main(String[] args) {
		int[] a = {49,38,56,89,90,64,78,23,34,78,88};
		int temp = 0;
		for(int i = 0;i<a.length-1;i++){
			for(int j = 0;j<a.length-1-i;j++){
				if(a[j]>a[j+1]){//较小的数在上面
					temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
				}
			}
		}
		for(int i = 0;i<a.length;i++){
			System.out.println(a[i]);
		}
	}
}

3.递归问题

//3.递归
/**
 * 有5个人坐在一起,问第5个人多少岁,他说比第4个人大2岁。
 * 问第4个人,说比第3个大2岁,
 * 问第3个人,说比第2个大2岁,
 * 问第2个人,说比第1个大2岁,
 * 问第1个人,说他10岁。
 * 问第5个人多少岁?
 */
public class Test3 {

	public static void main(String[] args) {
		System.out.println(suishu(5));
	}
	
	public static int suishu(int i){
		if(i==1){
			return 10;
		}
		else{
			return suishu(i-1)+2;
		}
	}
}


4.一球从100米高度自由落下,每次落地后反跳回原高度的一半,在落下,
求它在第10次落地时,共经过多少米?第10次反弹多高?

import java.util.Scanner;

/**
 * 4.一球从100米高度自由落下,每次落地后反跳回原高度的一半,在落下,
 * 求它在第10次落地时,共经过多少米?第10次反弹多高?
 */
public class Test4 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);// 创建输入流扫描器
        System.out.println("请输入落地的次数:");// 提示用户输入
        String line = scanner.nextLine();
		int i =Integer.parseInt(line);
		System.out.println("第"+i+"次落地时共经过"+Luodi(i)[1]+"米"+"\n"+"第"+i+"次落地时反弹的高度为:"+Luodi(i)[0]+"米");
	}
	public static double[] Luodi(double i){
		double[] d = new double[2];
		if(i==1){
			d[0] = 50;//第1次落地时反弹多高
			d[1] = 100;//第1次落地时共经过多少米
		}
		else{
			d[0] = Luodi(i-1)[0]/2;//第i次落地时反弹多高
			d[1] = Luodi(i-1)[1] + Luodi(i-1)[0]*2;//第i次落地时共经过多少米
		}
		return d;
	}
}

5.求1!+2!+3!+...+20!的和 

/**
 *5.求1!+2!+3!+...+20!的和 
 */
public class Test5 {

	public static void main(String[] args) {
		System.out.println(factorial(20));//1+1*2+1*2*3+1*2*3*4+...+...
	}
	
	public static long factorial(int num){
		long count = 0;//阶乘的和
		long factorial = 1;//阶乘
		for(int i = 1;i<=num;i++){
			factorial = factorial * i;
			count+=factorial;//累加
		}
		return count;
	}
}

6.不借助第三个变量,将两个变量的值互换

/**
 * 6.不借助第三个变量,将两个变量的值互换
 * @author MaoFachang
 *
 */
public class Test6 {
	
	public static void main(String[] args) {
		int a = 8;
		int b = 9;
		a = a+b;//17
		b = a-b;//17-9=8
		a = a-b;//17-8=9
		System.out.println(a+"\n"+b);
		System.out.println("-----end----");
		
		int x = 8;
		int y = 9;
		x=x^y;
		y=x^y;
		x=x^y;
		System.out.println(x+"\n"+y);
		System.out.println("-----end----");
		
		int i = 8,j = 9;
		i = i*j;//	8*9=72
		j = i/j;//	72/9=8
		i = i/j;//	72/8=9
		System.out.println(i+"\n"+j);
		System.out.println("-----end----");
	}
}





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值