javaSE算法基础题

编程挑战:斐波那契数列、素数、质因数与算法应用
这篇博客涵盖了多个编程挑战,包括使用斐波那契数列解决兔子问题,通过循环和条件判断找出素数,分解质因数的方法,以及计算数列和与实现最大公约数和最小公倍数的算法。此外,还展示了杨辉三角的规律和输出。这些实例展示了基础算法在解决问题中的应用。

1,題目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

//此处自己加了输入截止;自行查裴波那契数列,兔子就是标准的裴波那契数列

import java.util.Scanner;
class tt{
    public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int mouth=sc.nextInt();
		if(mouth==1){
			System.out.println("2个兔子");
		}
		int b=1;
		int a=1;
		int c=1;
		for(int i=1;i<=mouth;i++){
			c=a;//倒数第2个数
			a=b;//倒数第一个数
			b=c+b;//现在的数
			System.out.print(c*2+" ");
		}
    }
}

2、求素数(第一种一定要看哦,和你想的不一样)

第一种

class su{
    public static void  main(String[] args){
		int j;
        for(int i=100;i<200;i++){
			for(j=2;j<i;j++){
				if(i%j==0){
					break;
				}
			}
			//循环走完都没有进break;j=i判定
			if(i==j){
					System.out.print(i+" ");
				}
			
		}
    }
}

第二种(这是之前写的,范围没改)

class text{
    public static void main(String[] args){
        //求1~50所有的质数
        boolean a;
        for(int i=2;i<50;i++){
             a=true;
            for(int j=2;j<i;j++){
                if(i%j==0){
                    a=false;
                    break;
                }
            }
            if(a==false){
                continue;
            }else{
                System.out.print(i+" ");
            }
        }
    }
}

3、将一个正整数分解质因数。例如:输入90,打印出90=2335

第一种

class Demo4
{

public static void main(String[] args) 
{
	int j = 90;//
	for (int i = 2;i<=j ; )
	{
		if (j% i== 0)
		{

			//所有的i的因数
			System.out.print(i+"*");
			j = j/i;
		}else{
			i++;
		}
	}
	
}

}

第二种

import java.util.Scanner;
class zys{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.println("输入一个正整数");
		int a=sc.nextInt();
		System.out.print(a+"=");
		int temp=2;
		if(a<2){
			System.out.print("请输入大于2的数");
		}
		while(a>=temp){
			if(a%temp==0){//因子
				System.out.print(temp+"*");
				a/=temp;
			}else{
				temp++;
			}
			
		}
    }
}

4、输入两个正整数m和n,求其最大公约数和最小公倍数(两种方法都指导看,先看第二种)

第一种方法

import java.util.Scanner;
class GYAndGB
{
	public static void main(String[] args){
		System.out.println("输入两个正整数");
		Scanner sc=new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int tempM=m;
		int ys=2;
		int tempGY=1;//最大公约数
		while(ys<=tempM){
			if(tempM%ys==0){//小的那个的因数
				if(n%ys==0){//看能不能被另一个数整除
					tempGY*=ys;
					tempM=tempM/tempGY;
					ys++;
				}else{
					ys++;
					continue;
					
				}
		}else{
			ys++;
			continue;
		}
		}
		System.out.println(tempGY);
		System.out.print(tempGY*m/tempGY*n/tempGY);
	}
}

第二种方法

//此处用的取余方法是:辗转相除法;自行百度
public class TestDay06 {
    public static void main(String[] args) {
        gcdlcm gcdlcm = new gcdlcm();
        System.out.println("两个数的最大公约数是:" + gcdlcm.gcd(10,25));
        System.out.println("两个数的最小公约数是:" + gcdlcm.lcm(6, 8));
    }

    private static class gcdlcm {
        int gcd(int m, int n) {
            if (m < n) {   //这个判断是为了将大数放在前面
                int temp = n;
                n = m;
                m = temp;
            }
            if (m % n == 0) {
                return n;
            } else {
                m %= n;
                return gcd(m, n);  //这里也可以成gcd(n,m),就省掉了前面的判断了,会快一点
            }
        }

        int lcm(int m, int n) {
            int i = 1;
            if (m < n) {   //这个判断是为了将大数放在前面
                int temp = n;
                n = m;
                m = temp;
            }
            int lcm = m;
            while (lcm % n != 0) {
                lcm = m * i;
                i++;
            }
            return lcm;
        }
    }
}

5、求1+2!+3!+…+20!的和

public class jc{
 
	public static void main(String[] args) {
		long sum = 0;
		for (int i = 1; i <= 20; i++) {
			sum += fun(i);
		}
		System.out.println("1+2!+3!+…+20!的和:" + sum);
	}
 
	public static int fun(int a) {
		int s = 1;
		for (int i = a; a >= 1; a--) {
			s *= a;
		}
		return s;
	}
 }

杨辉三角

public class YHSJ {
    /**
        1
       1 1
     1 2 1
    1 3 3 1
   1 4 6 4 1
1 5 10 10 5 1
找规律,每一个数=前一行的j和j-1位置的数相加
*/
    public static void main(String[] args) {
        int[][] arr = new int[10][10];  //定义一个二维数组
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j <=i; j++) {
                    arr[i][0]=1;
                if (i==j){
                    arr[i][j]=1;
                }
                if (i >= 2 && j > 0) {
                    arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
                }
               // arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();

        }
    }
}

输出

/*
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
*/
class a{
    public static void main(String[] args){
		int x=1;//输出
		int z=1;//控制循环
        for(int i=1;i<=4;i++){//4行
			if(i%2==0){//当偶数行的时候
				for(z=x+4;z>x;z--){
					System.out.print(z);
				}
				x=z+5;
			}else{//奇数行的时候
				for(z=x;z<x+4;z++){
					System.out.print(z);
				}
				x+=3;
			}
			System.out.println();
		}
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值