1.冒泡排序?!局攸c(diǎn)】
int[] arrs= {3,656,43,76,123};
for(int i=0;i<arrs.length-1;i++) {
for(int j=0;j<arrs.length-1-i;j++) {
if(arrs[j]>arrs[j+1]) {
int temp=arrs[j];
arrs[j]=arrs[j+1];
arrs[j+1]=temp;
}
}
}
2.兩個(gè)有序數(shù)組的合并?!局攸c(diǎn)】
int[] num1=new int[]{1,2,4,6,7,123,411,5334,1414141,1314141414};
int[] num1=new int[]{0,2,57,89,113,5623,6353,134134};
//變量用于存儲(chǔ)兩個(gè)集合應(yīng)該被比較的索引(存入新集合就加一)
int a=0;
int b=0;
int[] num3=new int[num1.length+num2.length];
for(int i=0;i<num3.length;i++){
if(a<num1.length && b<num2.length){
if(num1[a]>num2[b]){
num3[i]=num2[b];
b++;
}else{
num3[i]=num2[a];
a++;
}
}else if(a<num1.length){
num3[i]=num1[a];
a++;
}else if(b<num2.length){
num3[i]=num2[b];
b++;
}
}
System.out.println("排序后:"+Arrays.toString(num3));
3.一個(gè)數(shù)組的倒序?!局攸c(diǎn)】
public static int[] reverse(int[] a){
int[] b=a;
for( int start=0,end-b.length-1;start<end;start++,end--){
int temp=b[start];
b[start]=b[end];
b[end]=temp;
}
return b;
}
4.計(jì)算一個(gè)正整數(shù)的正平方根?!玖私狻?/strong>
public static double MySqrt(int value,double t){
if(value<0||t<0)
return 0;
double left=0;
double right=value;
double mid=(right+left)/2;
double offset=2*t;
while(offset>t){
double temp=mid*mid;
if(temp>value){
right=(left+right)/2;
offset=temp-value;
}
if(temp<=value){
left=(left+right)/2;
offset=value-temp;
}
mid=(left+right)/2;
}
return mid;
}
5.快速排序算法?!局攸c(diǎn)】
public class QuickSort {
private int[] array;
public QuickSort(int[] array) { this.array = array; }
public void sort() { quickSort(array, 0, array.length - 1); }
public void print() {
for (int i = 0; i < array.length; i++) { System.out.println(array[i]); }
}
private void quickSort(int[] src, int begin, int end) {
if (begin < end) {
int key = src[begin];
int i = begin;
int j = end;
while (i < j) {
while (i < j && src[j] > key) { j--; }
if (i < j) { src[i] = src[j]; i++; }
while (i < j && src[i] < key) { i++; }
if (i < j) { src[j] = src[i]; j--; }
}
src[i] = key;
quickSort(src, begin, i - 1);
quickSort(src, i + 1, end);
}
}
}
6.二叉樹的遍歷算法。【了解】
節(jié)點(diǎn)類:
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
public TreeNode() { }
@Override
public String toString() { return "[" + val + "]"; }
}
二叉樹算法類:
public class BinaryTree {
//前序遍歷
public static void preOrder(TreeNode tree) {
if (tree == null) return;
System.out.printf(tree.val + "");
preOrder(tree.left);
preOrder(tree.right);
}
//非遞歸寫法
public static void preOrder2(TreeNode tree) {
if (tree == null) return;
Stack<TreeNode> q1 = new Stack<>();
q1.push(tree);//壓棧
while (!q1.empty()) {
TreeNode t1 = q1.pop();//出棧
System.out.println(t1.val);
if (t1.right != null) { q1.push(t1.right); }
if (t1.left != null) { q1.push(t1.left); }
}
}
//中序遍歷
public static void inOrderTraversal(TreeNode node) {
if (node == null)
return;
inOrderTraversal(node.left);
System.out.println(node.val);
inOrderTraversal(node.right);
}
//非遞歸的寫法
public static void inOrderTraversal2(TreeNode tree) {
Stack<TreeNode> stack = new Stack<>();
while (tree != null || !stack.isEmpty()) {
while (tree != null) {
stack.push(tree);
tree = tree.left;
}
if (!stack.isEmpty()) {
tree = stack.pop();
System.out.println(tree.val);
tree = tree.right;
}
}
}
//后續(xù)遍歷
public static void postOrder(TreeNode tree) {
if (tree == null) return;
postOrder(tree.left);
postOrder(tree.right);
System.out.println(tree.val);
}
//非遞歸的寫法
public static void postOrder2(TreeNode tree) {
if (tree == null) return;
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
s1.push(tree);
while (!s1.isEmpty()) {
tree = s1.pop();
s2.push(tree);
if (tree.left != null) { s1.push(tree.left); }
if (tree.right != null) { s1.push(tree.right); }
}
while (!s2.isEmpty()) { System.out.print(s2.pop().val + " "); }
}
//或者
public static void postOrder3(TreeNode tree) {
if (tree == null) return;
Stack<TreeNode> stack = new Stack<>();
stack.push(tree);
TreeNode c;
while (!stack.isEmpty()) {
c = stack.peek();
if (c.left != null && tree != c.left && tree != c.right) {
stack.push(c.left);
} else if (c.right != null && tree != c.right) {
stack.push(c.right);
} else {
System.out.print(stack.pop().val + " ");
tree = c;
}
}
}
//BFS(寬度優(yōu)先搜索(又稱廣度優(yōu)先搜索))
public static void levelOrder(TreeNode tree) {
if (tree == null) return;
LinkedList<TreeNode> list = new LinkedList<>();//鏈表,這里我們可以把它看做隊(duì)列
list.add(tree);//相當(dāng)于把數(shù)據(jù)加入到隊(duì)列尾部
while (!list.isEmpty()) {
TreeNode node = list.poll();//poll方法相當(dāng)于移除隊(duì)列頭部的元素
System.out.println(node.val);
if (node.left != null) list.add(node.left);
if (node.right != null) list.add(node.right);
}
}
//遞歸的寫法
public static void levelOrder(TreeNode tree) {
int depth = depth(tree);
for (int level = 0; level < depth; level++) { printLevel(tree, level); }
}
private static int depth(TreeNode tree) {
if (tree == null) return 0;
int leftDepth = depth(tree.left);
int rightDepth = depth(tree.right);
return Math.max(leftDepth, rightDepth) + 1;
}
private static void printLevel(TreeNode tree, int level) {
if (tree == null)
return;
if (level == 0) {
System.out.print(" " + tree.val);
} else {
printLevel(tree.left, level - 1);
printLevel(tree.right, level - 1);
}
}
//如果想把遍歷的結(jié)果存放到list中,我們還可以這樣寫
public static List<List<Integer>> levelOrder(TreeNode tree) {
if (tree == null) return null;
List<List<Integer>> list = new ArrayList<>();
bfs(tree, 0, list);
return list;
}
private static void bfs(TreeNode tree, int level, List<List<Integer>> list) {
if (tree == null) return;
if (level >= list.size()) {
List<Integer> subList = new ArrayList<>();
subList.add(tree.val);
list.add(subList);
} else { list.get(level).add(tree.val); }
bfs(tree.left, level + 1, list);
bfs(tree.right, level + 1, list);
}
//DFS(深度優(yōu)先搜索)
public static void treeDFS(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
while (!stack.empty()) {
TreeNode node = stack.pop();
System.out.println(node.val);
if (node.right != null) { stack.push(node.right); }
if (node.left != null) { stack.push(node.left); }
}
}
//遞歸的寫法
public static void treeDFS(TreeNode root) {
if (root == null) return;
System.out.println(root.val);
treeDFS(root.left);
treeDFS(root.right);
}
}
7.時(shí)間類型轉(zhuǎn)換?!菊莆铡?/strong>
public class DateFormat{
public static void fun(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
String newDate;
try{
newDate=sdf.format(new SimpleDateFormat("yyyyMMdd").parse("20121115"))
System.out.println(newDate);
}
}
public static void main(String args[]){
fun();
}
}
8.逆波蘭計(jì)算器?!玖私狻?/strong>
public class PolandNotation{
public static void main(String[] args){
//4*5-8+60+8/2
String expression="4 5 * 8 - 60 + 8 2 / +";
List<String> list=getStrList(expression);
System.out.println(list);
//計(jì)算值,得結(jié)果
int res=calc(list);
System.out.println(res);
}
public static List<String> getStrList(String exp){
String arr[]=exp.split(" ");//將字符串遍歷得到數(shù)組
List<String> list=new ArrayList<>();
for(String str:arr){
list.add(str);
}
return list;
}
//計(jì)算表達(dá)式
public static int calc(List<String> list){
Stack<String> stack=new Stack<>();
//遍歷list
for(int i=0;i<list.size;i++){
//正則表達(dá)式匹配是否是數(shù)字
if(list.get(i).matcher("\\d+")){
stack.push(list.get(i)); //是數(shù)字則放入棧中
}else{
int num2=Integer.parseInt(stack.pop()); //彈出數(shù)字1
int num1=Integer.parseInt(stack.pop()); //彈出數(shù)字2
int res=0;
//進(jìn)行運(yùn)算
if(list.get(i).equals("+")){
res=num1+num2;
}else if(list.get(i).equals("-")){
res=num1-num2;
}else if(list.get(i).equals("*")){
res=num1*num2;
}else if(list.get(i).equals("/")){
res=num1/num2;
}else{
throw new RuntimeException("不是操作符號(hào)!");
}
stack.push(""+res);
}
}
//留在棧中的值就是最后的計(jì)算表達(dá)式結(jié)果
return Integer.parseInt(stack.pop());
}
}
9.階乘?!局攸c(diǎn)】
public class Multiply {
public static int multiply(int num) {
if (num < 0) {
System.out.println("請(qǐng)輸入大于 0 的數(shù)!");
return -1;
} else if (num == 0 || num == 1) {
return 1;
} else {
return multiply(num - 1) * num;
}
}
public static void main(String[] args) {
System.out.println(multiply(10));
}
}
10.斐波那契數(shù)列?!玖私狻?/strong>
問題描述:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔子,假如兔子都不死,問每個(gè)月的兔子總數(shù)為多少?
// 這是一個(gè)菲波拉契數(shù)列問題
public class lianxi{
public static void main(String[] args) {
System.out.println("第1個(gè)月的兔子對(duì)數(shù): 1");
System.out.println("第2個(gè)月的兔子對(duì)數(shù): 1");
int f1 = 1, f2 = 1, f, M=24;
for(int i=3; i<=M; i++) {
f = f2;
f2 = f1 + f2;
f1 = f;
System.out.println("第" + i +"個(gè)月的兔子對(duì)數(shù): "+f2);
}
}
}
11.判斷101-200之間有多少個(gè)素?cái)?shù),并輸出所有素?cái)?shù)?!局攸c(diǎn)】
程序分析:判斷素?cái)?shù)的方法,用一個(gè)數(shù)分別去除2到sqrt(這個(gè)數(shù)),如果能被整除,則表明此數(shù)不是素?cái)?shù),反之是素?cái)?shù)。
public class lianxi {
public static void main(String[] args) {
int count = 0;
for(int i=101; i<200; i+=2) {
boolean b = false;
for(int j=2; j<=Math.sqrt(i); j++) {
if(i % j == 0) {
b = false; break;
} else {
b = true;
}
}
if(b == true) {
count ++;
System.out.println(i );
}
}
System.out.println( "素?cái)?shù)個(gè)數(shù)是: " + count);
}
}
12.水仙花數(shù)?!玖私狻?/strong>
問題描述:打印出所有的"水仙花數(shù)",所謂"水仙花數(shù)"是指一個(gè)三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個(gè)"水仙花數(shù)",因?yàn)?53=1的三次方+5的三次方+3的三次方。
public class lianxi {
public static void main(String[] args) {
int b1, b2, b3;
for(int m=101; m<1000; m++) {
b3 = m / 100;
b2 = m % 100 / 10;
b1 = m % 10;
if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {
System.out.println(m+"是一個(gè)水仙花數(shù)");
}
}
}
}
13.正整數(shù)分解質(zhì)因數(shù)?!玖私狻?/strong>
問題描述:將一個(gè)正整數(shù)分解質(zhì)因數(shù),例如:輸入90,打印出 90=2*3*3*5。
程序分析:對(duì)n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個(gè)最小的質(zhì)數(shù)k,然后按下述步驟完成:
如果這個(gè)質(zhì)數(shù)恰等于n,則說明分解質(zhì)因數(shù)的過程已經(jīng)結(jié)束,打印出即可;
如果n <> k,但n能被k整除,則應(yīng)打印出k的值,并用n除以k的商,作為新的正整數(shù),重復(fù)執(zhí)行第一步;
?如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步。
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print( "請(qǐng)鍵入一個(gè)正整數(shù): ");
int n = s.nextInt();
int k=2;
System.out.print(n + "=" );
while(k <= n) {
if(k == n) {
System.out.println(n);
break;
} else if( n % k == 0) {
System.out.print(k + "*");
n = n / k;
} else {
k++;
}
}
14.最大公約數(shù)和最小公倍數(shù)。【了解】
程序分析:在循環(huán)中,只要除數(shù)不等于0,用較大數(shù)除以較小的數(shù),將小的一個(gè)數(shù)作為下一輪循環(huán)的大數(shù),取得的余數(shù)作為下一輪循環(huán)的 較小的數(shù),如此循環(huán)直到較小的數(shù)的值為0,返回較大的數(shù),此數(shù)即為最大公約數(shù),最小公倍數(shù)為兩數(shù)之積除以最大公約數(shù)。
import java.util.*;
public class lianxi {
public static void main(String[] args) {
int a ,b,m;
Scanner s = new Scanner(System.in);
System.out.print( "鍵入一個(gè)整數(shù):");
a = s.nextInt();
System.out.print( "再鍵入一個(gè)整數(shù): ");
b = s.nextInt();
deff cd = new deff();
m = cd.deff(a,b);
int n = a * b / m;
System.out.println("最大公約數(shù): "+m);
System.out.println("最小公倍數(shù): "+n);
}
}
class deff{
public int deff(int x, int y){
int t;
if(x < y) {
t = x;
x = y;
y = t;
}
while(y != 0) {
if(x == y) return x;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
15.完數(shù)?!玖私狻?/strong>
問題描述:一個(gè)數(shù)如果恰好等于它的因子之和,這個(gè)數(shù)就稱為"完數(shù)",編程找出1000以內(nèi)的所有完數(shù),例如:6=1+2+3。
public class lianxi {
public static void main(String[] args) {
System.out.println("1到1000的完數(shù)有:");
for(int i=1; i<1000; i++) {
int t = 0;
for(int j=1; j<= i/2; j++) {
if(i % j == 0) {
t = t + j;
}
}
if(t == i) {
System.out.print(i + " ");
}
}
}
}
16.完全平方數(shù)?!玖私狻?/strong>
問題描述:一個(gè)整數(shù),它加上100后是一個(gè)完全平方數(shù),再加上168又是一個(gè)完全平方數(shù),請(qǐng)問該數(shù)是多少?
public class lianxi {
public static void main(String[] args) {
for(int x =1; x<100000; x++) {
if(Math.sqrt(x+100) % 1 == 0) {
if(Math.sqrt(x+268) % 1 == 0) {
System.out.println(x + "加100 是一個(gè)完全平方數(shù),再加168 又是一個(gè)完全平方數(shù)");
}
}
}
}
}
17.猴子吃桃?!玖私狻?/strong>
問題描述:猴子第一天摘下若干個(gè)桃子,當(dāng)即吃了一半,還不癮,又多吃了一個(gè)。第二天早上又將剩下的桃子吃掉一半,又多吃了一個(gè)。以后每天早上都吃了前一天剩下的一半零一個(gè)。到第10天早上想再吃時(shí),見只剩下一個(gè)桃子了。求第一天共摘了多少?
public class lianxi {
public static void main(String[] args) {
int x = 1;
for(int i=2; i<=10; i++)
x = (x+1)*2;
System.out.println("猴子第一天摘了" + x + " 個(gè)桃子");
}
}
18.乒乓球比賽。【了解】
問題描述:兩個(gè)乒乓球隊(duì)進(jìn)行比賽,各出三人。甲隊(duì)為a、b、c三人,乙隊(duì)為x、y、z三人。已抽簽決定比賽名單。有人向隊(duì)員打聽比賽的名單。a說他不和x比,c說他不和x、z比,請(qǐng)編程序找出三隊(duì)賽手的名單。
public class lianxi {
static char[] m = { 'a', 'b', 'c' };
static char[] n = { 'x', 'y', 'z' };
public static void main(String[] args) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < n.length; j++){
if (m[i] == 'a' && n[j] == 'x'){
continue;
} else if (m[i] == 'a' && n[j] == 'y') {
continue;
} else if ((m[i] == 'c' && n[j] == 'x') || (m[i] == 'c' && n[j] == 'z')) {
continue;
} else if ((m[i] == 'b' && n[j] == 'z') || (m[i] == 'b' && n[j] == 'y')) {
continue;
} else
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}
19.求1+2!+3!+...+20!的和?!局攸c(diǎn)】
public class lianxi {
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for(int i=1; i<=20; i++) {
fac = fac * i;
sum += fac;
}
ystem.out.println(sum);
}
}
20.計(jì)算年齡?!玖私狻?/strong>
問題描述:有5個(gè)人坐在一起,問第五個(gè)人多少歲?他說比第4個(gè)人大2歲。問第4個(gè)人歲數(shù),他說比第3個(gè)人大2歲。問第三個(gè)人,又說比第2人大兩歲。問第2個(gè)人,說比第一個(gè)人大兩歲。最后問第一個(gè)人,他說是10歲。請(qǐng)問第五個(gè)人多大?
public class lianxi {
public static void main(String[] args) {
int age = 10;
for(int i=2; i<=5; i++) { age =age+2; }
System.out.println(age);
}
}
21.回文數(shù)?!玖私狻?/strong>
問題描述:一個(gè)5位數(shù),判斷它是不是回文數(shù)。即12321是回文數(shù),個(gè)位與萬(wàn)位相同,十位與千位相同。
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a;
do{
System.out.print("請(qǐng)輸入一個(gè)5 位正整數(shù):");
a = s.nextInt();
}while(a<10000||a>99999);
String ss =String.valueOf(a);
char[] ch = ss.toCharArray();
if(ch[0]==ch[4]&&ch[1]==ch[3]){
System.out.println("這是一個(gè)回文數(shù)");
} else {
System.out.println("這不是一個(gè)回文數(shù)");
}
}
}
//這個(gè)更好,不限位數(shù)
public class lianxi{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean is =true;
System.out.print("請(qǐng)輸入一個(gè)正整數(shù):");
long a = s.nextLong();
String ss = Long.toString(a);
char[] ch = ss.toCharArray();
int j=ch.length;
for(int i=0; i<j/2; i++) {
if(ch[i]!=ch[j-i- 1])
is=false;
}
if(is==true){
System.out.println("這是一個(gè)回文數(shù)");
} else {
System.out.println("這不是一個(gè)回文數(shù)");
}
}
}
22.判斷100之內(nèi)的素?cái)?shù)?!局攸c(diǎn)】
//使用除sqrt(n)的方法求出的素?cái)?shù)不包括2 和3
public class lianxi {
public static void main(String[] args) {
boolean b =false;
System.out.print(2 + " ");
System.out.print(3 + " ");
for(int i=3; i<100; i+=2) {
for(int j=2; j<=Math.sqrt(i); j++) {
if(i % j == 0) {
b = false;
break;
} else{
b = true;
}
}
if(b == true) {
System.out.print(i + " ");
}
}
}
}
//該程序使用除1 位素?cái)?shù)得2 位方法,運(yùn)行效率高通用性差。
public class lianxi {
public static void main(String[] args) {
int[] a = new int[]{2, 3, 5, 7};
for(int j=0; j<4; j++)
System.out.print(a[j] + " ");
boolean b =false;
for(int i=11; i<100; i+=2) {
for(int j=0; j<4; j++) {
if(i % a[j] == 0) {
b = false; break;
} else{
b = true;
}
}
if(b == true) {
System.out.print(i + " ");
}
}
}
}
23.3*3矩陣對(duì)角線元素之和?!玖私狻?/strong>
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[][] a = new int[3][3];
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("輸入的3 * 3 矩陣是:");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int sum = 0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(i == j) {
sum += a[i][j];
}
}
}
System.out.println("對(duì)角線之和是:"+sum);
}
}
24.楊輝三角形?!玖私狻?/strong>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
public class lianxi{
public static void main(String[] args) {
int[][] a = new int[10][10];
for(int i=0; i<10; i++) {
a[i][i] = 1; a[i][0] = 1;
}
for(int i=2; i<10; i++) {
for(int j=1; j<i; j++){
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
for(int i=0; i<10; i++) {
for(int k=0; k<2*(10-i)-1; k++) {
System.out.print(" ");
}
for(int j=0; j<=i; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
25.報(bào)數(shù)游戲?!玖私狻?/strong>
問題描述:有n個(gè)人圍成一圈,順序排號(hào)。從第一個(gè)人開始報(bào)數(shù)(從1到3報(bào)數(shù)),凡報(bào)到3的人退出圈子,問最后留下的是原來(lái)第幾號(hào)的那位。
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("請(qǐng)輸入排成一圈的人數(shù):");
int n = s.nextInt();
boolean[] arr = new boolean[n];
for(int i=0; i<arr.length; i++) {
arr[i] = true;
}
int leftCount = n;
int countNum = 0;
int index = 0;
while(leftCount > 1) {
if(arr[index] == true) {
countNum ++;
if(countNum == 3) {
countNum =0;
arr[index] = false;
leftCount --;
}
}
index ++;
}
index = 0;
}
for(int i=0; i<n; i++) {
if(arr[i] == true) {
System.out.println("原排在第"+(i+1)+"位的人留下了。");
}
}
}
26.猴子分桃?!玖私狻?/strong>
問題描述:海灘上有一堆桃子,五只猴子來(lái)分。第一只猴子把這堆桃子平均分為五份,多了一個(gè),這只猴子把多的一個(gè)扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一個(gè), 它同樣把多的一個(gè)扔入海中,拿走了一份,第三、第四、第五只猴子都是這樣做的,問海灘上原來(lái)最少有多少個(gè)桃子?
public class lianxi{
public static void main (String[] args) {
int i,m,j=0,k,count;
for(i=4;i<10000;i+=4) {
count=0; m=i;
for(k=0;k<5;k++) {
j=i/4*5+1;
i=j;
if(j%4==0)
count++;
else
break;
}
i=m;
if(count==4) {
System.out.println("原有桃子"+j+" 個(gè)");
break;
}
}
}
}
27.一個(gè)偶數(shù)總能表示為兩個(gè)素?cái)?shù)之和?!玖私狻?/strong>
//由于用除sqrt(n)的方法求出的素?cái)?shù)不包括2 和3,
//因此在判斷是否是素?cái)?shù)程序中人為添加了一個(gè)3。
public class lianxi{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n,i;
do{
System.out.print("請(qǐng)輸入一個(gè)大于等于6 的偶數(shù):");
n = s.nextInt();
} while(n<6||n%2!=0);
//判斷輸入是否是>=6 偶數(shù),不是,重新輸入
fun fc = new fun();
for(i=2;i<=n/2;i++){
if((fc.fun(i))==1&&(fc.fun(n-i)==1)) {
int j=n-i;
System.out.println(n+" = "+i+" + "+j);
}
//輸出所有可能的素?cái)?shù)對(duì)
}
}
}
class fun{
//判斷是否是素?cái)?shù)的函數(shù)
public int fun (int a) {
int i,flag=0;
if(a==3){
flag=1;
return(flag);
}
for(i=2;i<=Math.sqrt(a);i++){
if(a%i==0) {
flag=0;
break;
} else
flag=1;
}
return (flag) ;
//不是素?cái)?shù),返回0,是素?cái)?shù),返回1
}
}
//解法二
import java.util.*;
public class lianxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
do{
System.out.print("請(qǐng)輸入一個(gè)大于等于6 的偶數(shù):");
n = s.nextInt();
} while(n<6||n%2!=0);
//判斷輸入是否是>=6 偶數(shù),不是,重新輸入
for(int i=3;i<=n/2;i+=2){
if(f un(i)&&fun(n-i)) {
}
//輸出所有可能的素?cái)?shù)對(duì)
}
}
static boolean fun (int a){
//判斷是否是素?cái)?shù)的函數(shù)
boolean flag=false;
if(a==3){
flag=true;return(flag);
}
for(int i=2;i<=Math.sqrt(a);i++){
if(a%i==0) {
flag=false;
break;
} else
flag=true;
}
return (flag) ;
}
}
28.選擇排序?!局攸c(diǎn)】
/**
* 選擇排序
* 在未排序序列中找到最小元素,存放到排序序列的起始位置。
* 再?gòu)氖S辔磁判蛟刂欣^續(xù)尋找最小元素,然后放到排序序列末尾。
* 以此類推,直到所有元素均排序完畢。
*/
public static void selectSort(int[] numbers) {
int size = numbers.length, temp;
for (int i = 0; i < size; i++) {
int k = i;
for (int j = size - 1; j >i; j--) {
if (numbers[j] < numbers[k])
k = j;
}
temp = numbers[i];
numbers[i] = numbers[k];
numbers[k] = temp;
}
}
29.插入排序?!局攸c(diǎn)】
/**
* 插入排序
* 從第一個(gè)元素開始,該元素可以認(rèn)為已經(jīng)被排序
* 取出下一個(gè)元素,在已經(jīng)排序的元素序列中從后向前掃描
* 如果該元素(已排序)大于新元素,將該元素移到下一位置
* 重復(fù)步驟3,直到找到已排序的元素小于或者等于新元素的位置
* 將新元素插入到該位置中
* 重復(fù)步驟2
*/
public static void insertSort(int[] numbers) {
int size = numbers.length, temp, j;
for(int i=1; i<size; i++) {
temp = numbers[i];
for(j = i; j > 0 && temp < numbers[j-1]; j--)
numbers[j] = numbers[j-1];
numbers[j] = temp;
}
}