java基础算法题(末考)总结一

1.斐波那契数列

package tongxin;
//输出斐波那契数列的前16项
public class Fibonacci {
	public static void main(String[] args) {
		int max=15;
		int i=0,j=1,k=1;
		while (k<max) {
			System.out.print(" "+i+" "+j);
			i=i+j;
			j=i+j;
			k=k+2;
			
		}
		System.out.println();
	}
}

2.最小公倍数

感谢刘志猛同学的帮助

package tongxin;

import java.util.Scanner;

//由键盘输入两个整数,求这两个数的最小公倍数
public class Min_gongbei {
	public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入第一个整数:");
	int  a=sc.nextInt();
	System.out.println("请输入第二个整数:");
	int  b=sc.nextInt();
	int  m=min(a,b);
	int  n=a*b/m;
	System.out.print(n);
	}

	private static int min(int a, int b) {
		return a%b==0?b:min(b:a%b);
	}
}

3.求阶乘之和

求1+2!+3!+4!+……+n!=sum

package tongxin;

import java.util.Scanner;

//求1+2!+3!+4!+……+n!=sum
public class Jiechenghe {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入一个数");
		int n=scanner.nextInt();
		int sum=0;
		for (int i = 1; i <=n; i++) {//确定第几个数
			int a=1;
			for (int j = 1; j <=i; j++) {//数的阶乘
				
				a=a*j;
			}
				sum=sum+a;
		}

		System.out.println(sum);
	}
}

4.阶乘分式之和

求1-1/2!+1/3!-1/4!+…+1/n!

package tongxin;

import java.util.Scanner;

public class Jiechengfenshi {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入一个数");
		int n=scanner.nextInt();
		double sum=0;
		for (int i = 1; i <=n; i++) {
			double a=1;
			for (int j = 1; j <=i; j++) {
				a *= j;
			}
			System.out.println(a+"  "+1/a);
			if(i%2 != 0) {
				sum += 1/a;
			}else {
				sum -= 1/a;
			}
		}
		System.out.println(sum);
	}
}

5.3000米绳子每天减去一半,几天时间绳子长度小于5m

package tongxin;

public class shengzi {
	public static void main(String[] args) {
		int i=3000;
		int count=0;
		while (i>=5) {
			i = i/2;
			count++;
		}
		System.out.println(count);
	}
}

6.计算并输出一个整数各位数字之和

如3679各位数和为3+6+7+9

package tongxin;

import java.util.*;

public class Shuhe {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入一个整数(小于10000):");
		int x=sc.nextInt();
		int a,b,c,d,e;
		int sum=0;
		a=x%10;//个位
		b=x/10%10;//十位
		c=x/10/10%10;//百位	
		d=x/10/10/10%10;//千位
		e=x/10/10/10/10%10;//万位
		sum=a+b+c+d+e;
		if (x>0&x<10) {
			System.out.println("个位:"+a+"和为:"+sum);
		}else if (x>10&x<100) {
			System.out.println("个位:"+a+"十位:"+b+"和为:"+sum);
		}else if (x>100&x<1000) {
			System.out.println("个位:"+a+"十位:"+b+"百位:"+c+"和为:"+sum);
		}else if (x>1000&x<10000) {
			System.out.println("个位:"+a+"十位:"+b+"百位:"+c+"千位:"+d+"和为:"+sum);
		}
		
	}
}

7.水仙花数

package tongxin;

import java.util.Scanner;

public class Shuixianhua {
	public static void main(String[] args) {
		for (int i = 100; i < 1000; i++) {
			int a=i%10;
			int b=i/10%10;
			int c=i/10/10%10;
			if (a*a*a+b*b*b+c*c*c==i) {
				System.out.println(i+"\t");
			}
			
		}
	}
}

8.数字图案

//数字图案:
/* 1 3 6 10 15
2 5 9 14
4 8 13
7 12
11*/

package tongxin2;
//数字图案:
	/*	1 3 6 10 15 
		2 5 9 14
		4 8 13
		7 12
		11*/
import java.util.Scanner;

public class shuzituan {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入行:");
		int n=sc.nextInt();
		int [][]tuan=new int[n][n];
		int p = 1;
		for (int i =0; i < n; i++) {
			for (int j = 0; j <=i; j++) {
				tuan[i-j][j]=p;
				p++;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
					System.out.print(tuan[i][j]>0?tuan[i][j]+" ":" ");	
			}
			System.out.println();
		}
	}
}

9.素数

package tongxin;
//100以内的素数
public class sushu {
	public static void main(String[] args) {
		for(int i=2;i<=100;i++) {
			int a = 0;
			for(int j=1;j<=i; j++) {
				if(i%j==0) {
					a++;
				}
			}
			if(a<=2) {//因数小于等于两个则输出
				System.out.print(i+"\t");
			}
		}
	}
}

特别鸣谢:王跃坤学长代码指导

  • 13
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值