編程演算法題java
一、冒泡排序
[java] view plain
package sort.bubble;
import java.util.Random;
/**
* 依次比較相鄰的兩個數,將小數放在前面,大數放在後面
* 冒泡排序,具有穩定性
* 時間復雜度為O(n^2)
* 不及堆排序,快速排序O(nlogn,底數為2)
* @author liangge
*
*/
public class Main {
public static void main(String[] args) {
Random ran = new Random();
int[] sort = new int[10];
for(int i = 0 ; i < 10 ; i++){
sort[i] = ran.nextInt(50);
}
System.out.print("排序前的數組為");
for(int i : sort){
System.out.print(i+" ");
}
buddleSort(sort);
System.out.println();
System.out.print("排序後的數組為");
for(int i : sort){
System.out.print(i+" ");
}
}
/**
* 冒泡排序
* @param sort
*/
private static void buddleSort(int[] sort){
for(int i=1;i<sort.length;i++){
for(int j=0;j<sort.length-i;j++){
if(sort[j]>sort[j+1]){
int temp = sort[j+1];
sort[j+1] = sort[j];
sort[j] = temp;
}
}
}
}
}
二、選擇排序
[java] view plain
package sort.select;
import java.util.Random;
/**
* 選擇排序
* 每一趟從待排序的數據元素中選出最小(或最大)的一個元素,
* 順序放在已排好序的數列的最後,直到全部待排序的數據元素排完。
* 選擇排序是不穩定的排序方法。
* @author liangge
*
*/
public class Main {
public static void main(String[] args) {
Random ran = new Random();
int[] sort = new int[10];
for (int i = 0; i < 10; i++) {
sort[i] = ran.nextInt(50);
}
System.out.print("排序前的數組為");
for (int i : sort) {
System.out.print(i + " ");
}
selectSort(sort);
System.out.println();
System.out.print("排序後的數組為");
for (int i : sort) {
System.out.print(i + " ");
}
}
/**
* 選擇排序
* @param sort
*/
private static void selectSort(int[] sort){
for(int i =0;i<sort.length-1;i++){
for(int j = i+1;j<sort.length;j++){
if(sort[j]<sort[i]){
int temp = sort[j];
sort[j] = sort[i];
sort[i] = temp;
}
}
}
}
}
三、快速排序
[java] view plain
package sort.quick;
/**
* 快速排序 通過一趟排序將要排序的數據分割成獨立的兩部分, 其中一部分的所有數據都比另外一部分的所有數據都要小,
* 然後再按此方法對這兩部分數據分別進行快速排序, 整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。
* @author liangge
*
*/
public class Main {
public static void main(String[] args) {
int[] sort = { 54, 31, 89, 33, 66, 12, 68, 20 };
System.out.print("排序前的數組為:");
for (int data : sort) {
System.out.print(data + " ");
}
System.out.println();
quickSort(sort, 0, sort.length - 1);
System.out.print("排序後的數組為:");
for (int data : sort) {
System.out.print(data + " ");
}
}
/**
* 快速排序
* @param sort 要排序的數組
* @param start 排序的開始座標
* @param end 排序的結束座標
*/
public static void quickSort(int[] sort, int start, int end) {
// 設置關鍵數據key為要排序數組的第一個元素,
// 即第一趟排序後,key右邊的數全部比key大,key左邊的數全部比key小
int key = sort[start];
// 設置數組左邊的索引,往右移動判斷比key大的數
int i = start;
// 設置數組右邊的索引,往左移動判斷比key小的數
int j = end;
// 如果左邊索引比右邊索引小,則還有數據沒有排序
while (i < j) {
while (sort[j] > key && j > start) {
j--;
}
while (sort[i] < key && i < end) {
i++;
}
if (i < j) {
int temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
}
}
// 如果左邊索引比右邊索引要大,說明第一次排序完成,將sort[j]與key對換,
// 即保持了key左邊的數比key小,key右邊的數比key大
if (i > j) {
int temp = sort[j];
sort[j] = sort[start];
sort[start] = temp;
}
//遞歸調用
if (j > start && j < end) {
quickSort(sort, start, j - 1);
quickSort(sort, j + 1, end);
}
}
}
[java] view plain
/**
* 快速排序
*
* @param a
* @param low
* @param high
* voidTest
*/
public static void kuaisuSort(int[] a, int low, int high)
{
if (low >= high)
{
return;
}
if ((high - low) == 1)
{
if (a[low] > a[high])
{
swap(a, low, high);
return;
}
}
int key = a[low];
int left = low + 1;
int right = high;
while (left < right)
{
while (left < right && left <= high)// 左邊向右
{
if (a[left] >= key)
{
break;
}
left++;
}
while (right >= left && right > low)
{
if (a[right] <= key)
{
break;
}
right--;
}
if (left < right)
{
swap(a, left, right);
}
}
swap(a, low, right);
kuaisuSort(a, low, right);
kuaisuSort(a, right + 1, high);
}
四、插入排序
[java] view plain
package sort.insert;
/**
* 直接插入排序
* 將一個數據插入到已經排好序的有序數據中,從而得到一個新的、個數加一的有序數據
* 演算法適用於少量數據的排序,時間復雜度為O(n^2)。是穩定的排序方法。
*/
import java.util.Random;
public class DirectMain {
public static void main(String[] args) {
Random ran = new Random();
int[] sort = new int[10];
for (int i = 0; i < 10; i++) {
sort[i] = ran.nextInt(50);
}
System.out.print("排序前的數組為");
for (int i : sort) {
System.out.print(i + " ");
}
directInsertSort(sort);
System.out.println();
System.out.print("排序後的數組為");
for (int i : sort) {
System.out.print(i + " ");
}
}
/**
* 直接插入排序
*
* @param sort
*/
private static void directInsertSort(int[] sort) {
for (int i = 1; i < sort.length; i++) {
int index = i - 1;
int temp = sort[i];
while (index >= 0 && sort[index] > temp) {
sort[index + 1] = sort[index];
index--;
}
sort[index + 1] = temp;
}
}
}
順便添加一份,差不多的
[java] view plain
public static void charuSort(int[] a)
{
int len = a.length;
for (int i = 1; i < len; i++)
{
int j;
int temp = a[i];
for (j = i; j > 0; j--)//遍歷i之前的數字
{
//如果之前的數字大於後面的數字,則把大的值賦到後面
if (a[j - 1] > temp)
{
a[j] = a[j - 1];
} else
{
break;
}
}
a[j] = temp;
}
}
把上面整合起來的一份寫法:
[java] view plain
/**
* 插入排序:
*
*/
public class InsertSort {
public void sort(int[] data) {
for (int i = 1; i < data.length; i++) {
for (int j = i; (j > 0) && (data[j] < data[j - 1]); j--) {
swap(data, j, j - 1);
}
}
}
private void swap(int[] data, int i, int j) {
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
五、順便貼個二分搜索法
[java] view plain
package search.binary;
public class Main {
public static void main(String[] args) {
int[] sort = {1,2,3,4,5,6,7,8,9,10};
int mask = binarySearch(sort,6);
System.out.println(mask);
}
/**
* 二分搜索法,返回座標,不存在返回-1
* @param sort
* @return
*/
private static int binarySearch(int[] sort,int data){
if(data<sort[0] || data>sort[sort.length-1]){
return -1;
}
int begin = 0;
int end = sort.length;
int mid = (begin+end)/2;
while(begin <= end){
mid = (begin+end)/2;
if(data > sort[mid]){
begin = mid + 1;
}else if(data < sort[mid]){
end = mid - 1;
}else{
return mid;
}
}
return -1;
}
}
B. JAVA演算法題
//第一種方法
publicstaticvoidmethod1(int[]array){
//先排序
for(inti=0;i<array.length;i++){
for(intj=i+1;j<array.length;j++){
if(array[i]>array[j]){
intc=array[i];
array[i]=array[j];
array[j]=c;
}
}
}
//第0位不變,2n位與2n-1位互換
for(inti=2;i<array.length;i+=2){
intc=array[i];
array[i]=array[i-1];
array[i-1]=c;
}
}
上面的方法先將數組排序,然後再把除0外的第2n位與2n-1位互換,這樣可以保證奇數位的數總比兩邊的數字大,可以滿足公式
//第二種方法
publicstaticvoidmethod2(int[]array){
//直接按0,2,1,4,3,6,5,8,7...的順序排序
for(inti=0;i<array.length;i=nextIndex(i)){
for(intj=nextIndex(i);j<array.length;j=nextIndex(j)){
if(array[i]>array[j]){
intc=array[i];
array[i]=array[j];
array[j]=c;
}
}
}
}
publicstaticintnextIndex(inti){
returni==0?i+2:(i%2==0?i-1:i+3);
}
第二種方法在排序時直接採用除0外的第2n位與2n-1位互換的順序排序
publicstaticvoidmain(String[]args){
int[]array=newint[]{1,5,3,4,7,5};
method1(array);
for(inti=0;i<array.length;i++){
System.out.print(i==0?"":(i%2==0?">=":"<="));
System.out.print(array[i]);
}
}
結果:1<=4>=3<=5>=5<=7
雖然結果與例子不一樣,但也能滿足公式需求
C. 3道java編程題,求解
packageTestPerson;
/**
*(1)編寫程序實現如下功能:已知Person類包含三個公共成員變數(姓名、性別、年齡)和一個構造方法,
*Student類是Person類的派生類,包含兩個新的公共成員變數(學號、班號)、兩個公共方法(修改年齡、顯示基本信息)及一個構造方法。
*在測試類Test1中,定義一組學生對象,並初始化他們的基本信息,然後依次輸出。
*/
publicclassTest1{
publicstaticvoidmain(String[]args){
Student[]student=newStudent[3];
student[0]=newStudent("小李","男",12,20181101,01);
student[1]=newStudent("小南","女",13,20001102,01);
student[2]=newStudent("小李","男",12,20181103,01);
for(Studentstu:student){
stu.showInformation();
}
}
}
classPerson{
publicStringname;
publicStringsex;
publicintage;
publicPerson(Stringname,Stringsex,intage){
super();
this.name=name;
this.sex=sex;
this.age=age;
}
}
classStudentextendsPerson{
publiclongstudentId;
publiclongclassId;
publicvoidsetAge(intage){
age=this.age;
}
publicvoidshowInformation(){
System.out.println("我的姓名是"+name+","+"我的性別是"+sex+","+"我的年齡是"+age
+"歲,"+"我的學號是"+studentId+","+"我的班號是"+classId+"班");
}
publicStudent(Stringname,Stringsex,intage,longstudentId,
longclassId){
super(name,sex,age);
this.studentId=studentId;
this.classId=classId;
}
}
不可否認,我現在是有點閑,所以我就幫你寫第一個吧,至於後面兩個,我就不寫了,看看還有沒有其他人有點閑時間,看緣分吧
運行結果:
我的姓名是小李,我的性別是男,我的年齡是12歲,我的學號是20181101,我的班號是1班
我的姓名是小南,我的性別是女,我的年齡是13歲,我的學號是20001102,我的班號是1班
我的姓名是小李,我的性別是男,我的年齡是12歲,我的學號是20181103,我的班號是1班
D. java經典演算法題——猴子吃桃
public class Monkey
{
public static void main(String[] args)
{
int sum=0,remain=1;
//每天吃剩的桃子加一個正好是前一天桃子的一半,每天桃子的總數就是前一天剩下桃子的數量
for(int day=9;day>=1;day--)
{
sum=(remain+1)*2;
remain=sum;
System.out.println("第"+day+"天還剩"+remain+"個桃子");
}
System.out.println(sum);
}
}
E. 5道簡單的JAVA編程題(高分懸賞)
很詳細的幫你寫下,呵呵,所以要給分哦!
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "張三";
int age = 23;
char sex = '男';
String myclass = "某某專業2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
編寫完程序的後綴名是.java,如本題,文件名就是One.java。
開始\運行\cmd,進入「命令提示符窗口」,然後用javac編譯器編譯.java文件,語句:javac One.java。
(3)
編譯成功後,生成的文件名後綴是.class,叫做位元組碼文件。再用java解釋器來運行改程序,語句:java One
2、編寫程序,輸出1到100間的所有偶數
(1)for語句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i<=100;i+=2)
System.out.println(i);
}
}
(2)while語句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i <= 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while語句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i<=100);
}
}
3、編寫程序,從10個數當中找出最大值。
(1)for循環
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i < 10; i++) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while語句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i < 10) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while語句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}while(i<10);
System.out.println("最大值:" + max);
}
}
4、編寫程序,計算從1到100之間的奇數之和。
(1)for循環
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i<=100;i+=2){
sum+=i;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(2)while語句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i += 2;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(3)do…while語句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i <= 100);
System.out.println("1~100間奇數和:" + sum);
}
}
5、
(1)什麼是類的繼承?什麼是父類?什麼是子類?舉例說明。
繼承:是面向對象軟體技術當中的一個概念。如果一個類A繼承自另一個類B,就把這個A稱為"B的子類",而把B稱為"A的父類"。繼承可以使得子類具有父類的各種屬性和方法,而不需要再次編寫相同的代碼。在令子類繼承父類的同時,可以重新定義某些屬性,並重寫某些方法,即覆蓋父類的原有屬性和方法,使其獲得與父類不同的功能。另外,為子類追加新的屬性和方法也是常見的做法。繼承需要關鍵字extends。舉例:
class A{}
class B extends A{}
//成員我就不寫了,本例中,A是父類,B是子類。
(2)編寫一個繼承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年齡:" + age + "\n性別:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("學校:" + school + "\n系別:" + department + "\n專業:"
+ subject + "\n班級:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大學", "某某系別",
" 某專業", "某某班級", " 張三", 23, '男');
StudentPersonDemo.output2();
}
}
F. java演算法題
快速排序一般來說 比較適用於數據量大的情況
public class QuickSoft {
private void swap(int a[],int i,int j)
{
int tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
private int partition(int a[],int p,int r)
{
int point = a[r];
//將小於等於point的元素移到左邊區域
//將大於point的元素移到右邊區域
int index=p;
for (int i = index; i < r; ++ i) {
if (a[i]-point <= 0) {
swap(a, index++, i);
}
}
swap(a,index,r);
return index;
}
public void qsort(int a[],int p,int r)
{
if(p< r)
{
//確定拆分點,並對數組元素進行移動
//這是快速排序演算法的關鍵步驟
int q=partition(a,p,r);
//對左半段排序
qsort(a,p,q-1);
//對右半段排序
qsort(a,q+1,r);
}
}
public static void main(String[] args) {
//聲明一個類
QuickSoft ms=new QuickSoft();
int len=10;
int a[]=new int[len];
//初始化a數組
System.out.println("原始數組如下:");
for(int i=0;i< a.length;i++)
{
//產生a.length個隨機數
a[i] = (int)(Math.random()*100000);
System.out.println(a[i]);
}
System.out.println("---------------------");
System.out.println("第一次分組後");
ms.partition(a,0,len-1);
for(int i=0;i< a.length;i++)
{
System.out.println(a[i]);
}
System.out.println("---------------------");
//快速排序
ms.qsort(a, 0, len-1);
System.out.println("排序後的數組如下:");
for(int i=0;i< a.length;i++)
{
System.out.println(a[i]);
}
}
}