java基础算法题(末考)总结三(冒泡、杨辉、回文等)

杨辉三角形

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

package tongxin;

import com.sun.jdi.InconsistentDebugInfoException;

public class Yanghui {
     
public static void main(String[] args) {
     
	int Yanghui[][]=new int[10][10];
		System.out.println("杨辉三角前十行");
		for (int i = 0; i < Yanghui.length; i++) {
     
			for (int j = 0; j < Yanghui.length; j++) {
     
				Yanghui[i][0]=Yanghui[i][i]=1;
				if (i>1&&j>0&&j<i) {
     
					Yanghui[i][j]=Yanghui[i-1][j]+Yanghui[i-1][j-1];
				}
			}
		}
		for (int i = 0; i < Yanghui.length; i++) {
     
			for (int j = 0; j <=i; j++) {
     
				System.out.print(Yanghui[i][j]+"\t");
			}
			System.out.println();

		}
}
}

冒泡排序(可选择从键盘输入数组)

package tongxin;

import java.util.Scanner;

public class Maopao {
     

	public static void main(String[] args) {
     
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入数组长度:");
		int m=scanner.nextInt();
		System.out.println("请输入数组并空空格隔开:");
		int arr[]=new int[m];
		for(int i=0;i<m;i++){
     
            arr[i]=scanner.nextInt();
        }											//定义数组并从键盘输入
		
		if (arr==null||arr.length==1) {
     
			return;
		}
//		int arr[]=new int[] {3,7,2,8,5,4};
		for (int i = 0; i < arr.length-1; i++) {
     	//外层循环是表示趟数
			for (int j = 0; j < arr.length-1-i; j++) {
     //内层循环表示第几个数被比较
				if (arr[j]>arr[j+1]) {
     
					int temp=arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=temp;
					//如果前面的大于后面的,就交换位置
				}
			}
		}
		System.out.println("冒泡排序后的数组顺序为:");

		for (int i = 0; i < arr.length; i++) {
     
			System.out.print(arr[i]+",");

		}
	}

}

判断是不是回文字符串

package tongxin;

import java.util.Scanner;

public class Huiwen {
     
	public static void main(String[] args) {
     
//		Scanner scanner  = new Scanner(System.in);
//		System.out.println("qingshuru");
//		String string = scanner.nextLine();
//		char []arr = string.toCharArray();
		System.out.println("请输入:");
		char []arr = new Scanner(System.in).nextLine().toCharArray();
		boolean b = true;
		for(int i=0; i<arr.length/2; i++) {
     
			if(arr[i] != arr[arr.length-i-1]) {
     
				b = false;
			}
		}
		if (b) {
     
			System.out.println("是回文");
		}else {
     
			System.out.println("不是回文");
		}
	}
}

辗转相除法求最大公约数

package tongxin;

import java.util.Scanner;

//辗转相除法
public class Maxgongyue {
     
public static void main(String[] args) {
     
	Scanner sc1 =new Scanner(System.in);
	System.out.println("请输入第一个数:");
	int a=sc1.nextInt();
	Scanner sc2 =new Scanner(System.in);
	System.out.println("请输入第二个数:");
	int b=sc2.nextInt();
	while(b != 0){
     
		int r = a % b;
		a = b;
		b = r;
	}
	System.out.println("最大公约数是:"+a);
}

}

输入一个数,输出个各位数并求和

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);
		}
		
	}
}

判断是否是一个完全数

package tongxin;
//判断一个数是不是完全数,完全数:28=1+2+4+7+14
import java.util.Scanner;

public class Wanquanshu {
     
	public static void main(String[] args) {
     
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入一个数:");
		int x=scanner.nextInt();
		int a = 0;
		for (int i = 1; i < x; i++) {
     
			if(x%i == 0) {
     
				a += i;
			}
		}
		if(a == x) {
     
			System.out.println("这是一个完全数");
		}else {
     
			System.out.println("啥也不是");
		}
	}
}

你可能感兴趣的:(Java总结)