楊輝三角形
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("啥也不是");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241308.html
標籤:java
上一篇:Java面向考點復習
