欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java實現(xiàn)的算法代碼,java實現(xiàn)的算法代碼有哪些

用JAVA實現(xiàn)快速排序算法?

本人特地給你編的代碼

成都網(wǎng)站制作、做網(wǎng)站,成都做網(wǎng)站公司-創(chuàng)新互聯(lián)已向上千家企業(yè)提供了,網(wǎng)站設(shè)計,網(wǎng)站制作,網(wǎng)絡(luò)營銷等服務(wù)!設(shè)計與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗,合理的價格為您打造企業(yè)品質(zhì)網(wǎng)站。

親測

public class QuickSort {

public static int Partition(int a[],int p,int r){

int x=a[r-1];

int i=p-1;

int temp;

for(int j=p;j=r-1;j++){

if(a[j-1]=x){

// swap(a[j-1],a[i-1]);

i++;

temp=a[j-1];

a[j-1]=a[i-1];

a[i-1]=temp;

}

}

//swap(a[r-1,a[i+1-1]);

temp=a[r-1];

a[r-1]=a[i+1-1];

a[i+1-1]=temp;

return i+1;

}

public static void QuickSort(int a[],int p,int r){

if(pr){

int q=Partition(a,p,r);

QuickSort(a,p,q-1);

QuickSort(a,q+1,r);

}

}

public static void main(String[] stra){

int a[]={23,53,77,36,84,76,93,13,45,23};

QuickSort(a,1,10);

for (int i=1;i=10;i++)

System.out.println(a[i-1]);

}

}

求:用JAVA語言編寫的銀行家算法的源代碼

import java.util.*;

class ThreadTest {

static int type = 4, num = 10; //定義資源數(shù)目和線程數(shù)目

static int[] resource = new int[type]; //系統(tǒng)資源總數(shù)

//static int[] copyResource = new int[type]; //副本

static Random rand = new Random();

static Bank[] bank = new Bank[num]; //線程組

Bank temp = new Bank();

public void init() {

//初始化組中每個線程,隨機填充系統(tǒng)資源總數(shù)

for(int i = 0; i type; i++)

resource[i] = rand.nextInt(10) + 80;

System.out.print("Resource:");

for(int i = 0; i type; i++)

System.out.print(" " + resource[i]);

System.out.println("");

for(int i = 0; i bank.length; i++)

bank[i] = new Bank("#" + i);

}

public ThreadTest4() {

init();

}

class Bank extends Thread {

//銀行家算法避免死鎖

public int[]

max = new int[type], //總共需求量

need = new int[type], //尚需資源量

allocation = new int[type]; //已分配量

private int[]

request = new int[type], //申請資源量

copyResource = new int[type]; //資源副本

private boolean isFinish = false; //線程是否完成

int[][] table = new int[bank.length][type*4]; //二維資源分配表

private void init() {

// 隨機填充總共、尚需、已分配量

synchronized(resource) {

for(int i = 0; i type; i++) {

max[i] = rand.nextInt(5) + 10;

need[i] = rand.nextInt(10);

allocation[i] = max[i] - need[i];

resource[i] -= allocation[i]; //從系統(tǒng)資源中減去已分配的

}

printer();

for(int i = 0; i type; i++) {

if(resource[i] 0) {

//若出現(xiàn)已分配量超出系統(tǒng)資源總數(shù)的錯誤則退出

System.out.println("The summation of Threads' allocations is out of range!");

System.exit(1);

}

}

}

}

public Bank(String s) {

setName(s);

init();

start();

}

public Bank() {

//none

}

public void run() {

try {

sleep(rand.nextInt(2000));

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

while(true) {

//程序沒有完成時一直不斷申請資源

if(askFor() == false) {

try {

sleep(1000);

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

}

else

tryRequest();

if(noNeed() == true)

break;

}

//休眠一段時間模擬程序運行

try {

sleep(1000);

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

System.out.println(getName() + " finish!");

synchronized(resource) {

//運行結(jié)束釋放占有資源

for(int i = 0; i type; i++) {

resource[i] += allocation[i];

need[i] = allocation[i] = max[i] = 0;

}

}

}

private void printer() {

//打印當前資源信息

System.out.print(getName() + " Max:");

for(int i = 0; i type; i++)

System.out.print(" " + max[i]);

System.out.print(" Allocation:");

for(int i = 0; i type; i++)

System.out.print(" " + allocation[i]);

System.out.print(" Need:");

for(int i = 0; i type; i++)

System.out.print(" " + need[i]);

System.out.print(" Available:");

for(int i = 0; i type; i++)

System.out.print(" " + resource[i]);

System.out.println("");

}

private boolean askFor() {

//隨機產(chǎn)生申請資源量并檢測是否超標

boolean canAsk = false;

for(int i = 0; i type; i++) {

request[i] = rand.nextInt(20);

//防止申請量超過所需量

if(request[i] need[i])

request[i] = need[i];

}

for(int i = 0; i type; i++) //防止隨機申請資源全為0

if(request[i] 0)

canAsk = true;

synchronized(resource) {

//鎖住可供資源檢查是否超標

for(int i = 0; i type; i++) {

if(request[i] resource[i])

//如果申請資源超過可供資源則等待一段時間后重新申請

return false;

}

}

return canAsk;

}

private void tryRequest() {

//創(chuàng)建副本嘗試分配請求

synchronized(resource) {

for(int i = 0; i type; i++)

//依然要防止請求量超出范圍

if(request[i] resource[i])

return;

for(int i = 0; i type; i++) {

//復制資源量并減去需求量到一個副本上

copyResource[i] = resource[i];

copyResource[i] -= request[i];

}

System.out.print(getName() + " ask for:");

for(int i = 0; i type; i++)

System.out.print(" " + request[i]);

System.out.println("");

if(checkSafe() == true) {

//如果檢查安全則將副本值賦給資源量并修改占有量和需求量

for(int i = 0; i type; i++) {

resource[i] = copyResource[i];

allocation[i] += request[i];

need[i] -= request[i];

}

System.out.println(getName() + " request succeed!");

}

else

System.out.println(getName() + " request fail!");

}

}

private boolean checkSafe() {

//銀行家算法檢查安全性

synchronized(bank) {

//將線程資源信息放入二維資源分配表檢查安全性,0~type可用資源/type~type*2所需資源/type*2~type*3占有資源/type*3~-1可用+占用資源

for(int i = 0; i bank.length; i++) {

for(int j = type; j type*2; j++) {

table[i][j] = bank[i].need[j%type];

}

for(int j = type*2; j type*3; j++) {

table[i][j] = bank[i].allocation[j%type];

}

}

//冒泡排序按需求資源從小到大排

for(int i = 0; i bank.length; i++) {

for(int j = i; j bank.length-1; j++) {

sort(j, 4);

}

}

//進行此時刻的安全性檢查

for(int i = 0; i type; i++) {

table[0][i] = copyResource[i];

table[0][i+type*3] = table[0][i] + table[0][i+type*2];

if(table[0][i+type*3] table[1][i+type])

return false;

}

for(int j = 1; j bank.length-1; j++) {

for(int k = 0; k type; k++) {

table[j][k] = table[j-1][k+type*3];

table[j][k+type*3] = table[j][k] + table[j][k+type*2];

if(table[j][k+type*3] table[j+1][k+type])

return false;

}

}

}

return true;

}

private void sort(int j, int k) {

//遞歸冒泡排序

int tempNum;

if(table[j][k] table[j+1][k]) {

for(int i = type; i type*2; i++) {

tempNum = table[j][i];

table[j][i] = table[j+1][i];

table[j+1][i] = tempNum;

}

/*temp = bank[j];

bank[j] = bank[j+1];

bank[j+1] = temp;*/

}

else if(table[j][k] == table[j+1][k] k type*2) //此資源量相同時遞歸下一個資源量排序并且防止超出范圍

sort(j, k+1);

}

private boolean noNeed() {

//是否還需要資源

boolean finish = true;

for(int i = 0; i type; i++) {

if(need[i] != 0) {

finish = false;

break;

}

}

return finish;

}

}

public static void main(String[] args) {

ThreadTest t = new ThreadTest();

//后臺線程,設(shè)定程序運行多長時間后自動結(jié)束

new Timeout(30000, "---Stop!!!---");

}

}

如何用Java語言編程實現(xiàn)下面這道題?

貪心算法: 思路就是對花到第一個噴泉距離從近到遠排序,然后找到另一個噴泉距離最大的一個

復雜度O(n^2)。

import java.util.*;

public class Demo {

static long[][] flowers;

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

int n=in.nextInt();

int x1=in.nextInt();

int y1=in.nextInt();

int x2=in.nextInt();

int y2=in.nextInt();

flowers=new long[n][2];

for (int i = 0; i n; i++) {

int x=in.nextInt();

int y=in.nextInt();

flowers[i][0]=dis(x,y,x1,y1);

flowers[i][1]=dis(x,y,x2,y2);

}

Arrays.sort(flowers, (o1, o2) - {

if (o1[0]o2[0])

return -1;

else if (o1[0]==o2[0])

return 0;

else return 1;

});

long temp=0;

long temp2=0;

for (int i = 0; i flowers.length; i++) {

temp=Math.max(temp,flowers[i][1]);

}

for (int i = 0; i flowers.length; i++) {

for (int j = i+1; j flowers.length ; j++) {

if (flowers[j][1]temp2)

temp2=flowers[j][1];

}

temp=Math.min(temp,flowers[i][0]+temp2);

temp2=0;

}

System.out.println(temp);

}

public static long dis(int x,int y,int x1,int y1){

return (long) (x1 - x) *(x1-x)+ (long) (y1 - y) *(y1-y);

}

}

怎么用java代碼實現(xiàn)開平方算法?

這是我應(yīng)聘時寫的算法代碼,運行成功:

//這是用java編寫的一個求2的平方根的程序,精確度可通過修改weishu參數(shù)來改變

public class app

{ //用二分法求2的平方根

public static void main(String args[])

{

int a[],b[],s[],d[],c[],ss[];

int i,j,k;

a=new int[1000];

b=new int[1000];

s=new int[1000];

d=new int[1000];

c=new int[1000];

ss=new int[1000];

boolean jingque;

jingque=true;

a[0]=b[0]=2;

a[2]=b[2]=1;

a[1]=4;

b[1]=5;

int weishu=200;//定義循環(huán)次數(shù)

for(i=0;i1000;i++)

s[i]=0;

for(k=0;kweishu;k++)

{

hanshucheng.cheng(b,b,s);

j=s[0];

while (s[j]=2)

{

hanshuadd.add(a,b,c);

hanshuchu.chu(d,c);

hanshucopy.copy(ss,b);

hanshucopy.copy(b,d);

hanshucheng.cheng(b,b,s);

j=s[0];

// for(i=0;i=s[0];i++)

//System.out.println("s["+i+"]="+s[i]);

}

hanshucopy.copy(a,b);

hanshucopy.copy(b,ss);

}

for(i=a[0];i=1;i--)

System.out.print(a[i]);

System.out.print("左邊計算到"+a[0]+"位\n");

for(i=b[0];i=1;i--)

System.out.print(b[i]);

System.out.print("右邊計算到"+b[0]+"位\n");

for(i=a[0],j=b[0];jingque==true;i--,j--)

if(a[i]==b[j])

System.out.print(a[i]);

else

jingque=false;

System.out.print("精確到"+(a[0]-i-1)+"位\n");

}

}

class hanshucheng

{

public static void cheng( int a[],int b[],int s[])//定義兩數(shù)相乘的函數(shù)

{

int flag=0,flag1=0;

int number=b[0];

int c[]=new int[1000];

int i,j,k,u;

for(int i1=0;i11000;i1++)

s[i1]=0;

for(i=1;i=number;i++)

{

for(int i1=0;i11000;i1++)

c[i1]=0;

for(j=i,k=1;jnumber+i;j++,k++)

{

c[j]=(a[k]*b[i]+flag)%10;

flag=(a[k]*b[i]+flag)/10;

}

if (flag!=0)

{

c[j]=flag;

flag=0;

j=j+1;

}

c[0]=j-1;

//for(k=1;k=c[0];k++)

//System.out.println("c="+c[k]);

for(k=1;k=c[0];k++)

{ u=s[k];

s[k]=(u+c[k]+flag1)%10;

flag1=(u+c[k]+flag1)/10;

}

if(flag1!=0)

{

s[k]=flag1;

k=k+1;

flag1=0;

}

s[0]=k-1;

// for(k=0;k=s[0];k++)

//System.out.println(s[k]);

}

}

}

class hanshuadd

{

public static void add(int a[],int b[],int c[])//定義兩數(shù)相加的函數(shù)

{

int flag=0;int i,j,k;

int a1[]=new int[1000];

for(i=1;i=b[0];i++)

a1[i]=0;

for(j=b[0]-a[0]+1,k=1;j=b[0];j++,k++)

a1[j]=a[k];

//for(k=0;k=j;k++)

//System.out.println("a1="+a1[k]);

for(i=1;i=b[0];i++)

{

c[i]=(a1[i]+b[i]+flag)%10;

flag=(a1[i]+b[i]+flag)/10;

}

if(flag!=0)

{

c[i]=flag;

i=i+1;

flag=0;

}

c[0]=i-1;

}

}

class hanshuchu

{

public static void chu(int d[],int a[])//定義任一數(shù)除以2的函數(shù)

{

int flag=0,i;

for(i=a[0];i=1;i--)

{

d[i+1]=(flag*10+a[i])/2;

flag=(flag*10+a[i])%2;

}

if(flag!=0)

d[1]=5;

if(d[1]==0)

for(i=1;i=a[0]+1;i++)

d[i]=d[i+1];

d[i]=0;

d[0]=a[0]+1;

}

}

class hanshucopy

{

public static void copy(int a[],int b[])//定義

{

int i;

for(i=0;i=b[0];i++)

a[i]=b[i];

while (i1000)

a[i++]=0;

}

}

求使用java實現(xiàn)的快排算法

① 代碼:

public?class?quicksortdemo?{

private?int?array[];

private?int?length;

public?void?sort(int[]?inputArr)?{

if?(inputArr?==?null?||?inputArr.length?==?0)?{

return;

}

this.array?=?inputArr;

length?=?inputArr.length;

quickSort(0,?length?-?1);

}

private?void?quickSort(int?lowerIndex,?int?higherIndex)?{

int?i?=?lowerIndex;

int?j?=?higherIndex;

//?calculate?pivot?number

int?pivot?=?array[lowerIndex+(higherIndex-lowerIndex)/2];

//?Divide?into?two?arrays

while?(i?=?j)?{

while?(array[i]??pivot)?{

i++;

}

while?(array[j]??pivot)?{

j--;

}

if?(i?=?j)?{

swap(i,?j);????????????????

i++;

j--;

}

}

//?call?quickSort()?method?recursively

if?(lowerIndex??j)

quickSort(lowerIndex,?j);

if?(i??higherIndex)

quickSort(i,?higherIndex);

}

private?void?swap(int?i,?int?j)?{

int?temp?=?array[i];

array[i]?=?array[j];

array[j]?=?temp;

}

public?static?void?main(String?a[]){

quicksortdemo?sorter?=?new?quicksortdemo();

int[]?input?=?{24,2,45,20,56,75,2,56,99,53,12};

sorter.sort(input);

for(int?i:input){

System.out.print(i);

System.out.print("?");

}

}

}

② 運行:

c:\java?quicksortdemo

2?2?12?20?24?45?53?56?56?75?99

分享名稱:java實現(xiàn)的算法代碼,java實現(xiàn)的算法代碼有哪些
路徑分享:http://chinadenli.net/article43/dsedihs.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站面包屑導航域名注冊做網(wǎng)站全網(wǎng)營銷推廣動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作