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

java算法源代碼,java實(shí)現(xiàn)算法

求:用JAVA語(yǔ)言編寫(xiě)的銀行家算法的源代碼

import java.util.*;

創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供蛟河網(wǎng)站建設(shè)、蛟河做網(wǎng)站、蛟河網(wǎng)站設(shè)計(jì)、蛟河網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、蛟河企業(yè)網(wǎng)站模板建站服務(wù),十年蛟河做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

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() {

//初始化組中每個(gè)線程,隨機(jī)填充系統(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], //申請(qǐng)資源量

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

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

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

private void init() {

// 隨機(jī)填充總共、尚需、已分配量

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ù)的錯(cuò)誤則退出

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) {

//程序沒(méi)有完成時(shí)一直不斷申請(qǐng)資源

if(askFor() == false) {

try {

sleep(1000);

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

}

else

tryRequest();

if(noNeed() == true)

break;

}

//休眠一段時(shí)間模擬程序運(yùn)行

try {

sleep(1000);

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

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

synchronized(resource) {

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

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

resource[i] += allocation[i];

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

}

}

}

private void printer() {

//打印當(dāng)前資源信息

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() {

//隨機(jī)產(chǎn)生申請(qǐng)資源量并檢測(cè)是否超標(biāo)

boolean canAsk = false;

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

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

//防止申請(qǐng)量超過(guò)所需量

if(request[i] need[i])

request[i] = need[i];

}

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

if(request[i] 0)

canAsk = true;

synchronized(resource) {

//鎖住可供資源檢查是否超標(biāo)

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

if(request[i] resource[i])

//如果申請(qǐng)資源超過(guò)可供資源則等待一段時(shí)間后重新申請(qǐng)

return false;

}

}

return canAsk;

}

private void tryRequest() {

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

synchronized(resource) {

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

//依然要防止請(qǐng)求量超出范圍

if(request[i] resource[i])

return;

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

//復(fù)制資源量并減去需求量到一個(gè)副本上

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

}

}

//進(jìn)行此時(shí)刻的安全性檢查

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) //此資源量相同時(shí)遞歸下一個(gè)資源量排序并且防止超出范圍

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

//后臺(tái)線程,設(shè)定程序運(yùn)行多長(zhǎng)時(shí)間后自動(dòng)結(jié)束

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

}

}

java中排序算法代碼

package temp;

import sun.misc.Sort;

/**

* @author zengjl

* @version 1.0

* @since 2007-08-22

* @Des java幾種基本排序方法

*/

/**

* SortUtil:排序方法

* 關(guān)于對(duì)排序方法的選擇:這告訴我們,什么時(shí)候用什么排序最好。當(dāng)人們渴望先知道排在前面的是誰(shuí)時(shí),

* 我們用選擇排序;當(dāng)我們不斷拿到新的數(shù)并想保持已有的數(shù)始終有序時(shí),我們用插入排序;當(dāng)給出的數(shù)

* 列已經(jīng)比較有序,只需要小幅度的調(diào)整一下時(shí),我們用冒泡排序。

*/

public class SortUtil extends Sort {

/**

* 插入排序法

* @param data

* @Des 插入排序(Insertion Sort)是,每次從數(shù)列中取一個(gè)還沒(méi)有取出過(guò)的數(shù),并按照大小關(guān)系插入到已經(jīng)取出的數(shù)中使得已經(jīng)取出的數(shù)仍然有序。

*/

public int[] insertSort(int[] data) {

1/11頁(yè)

int temp;

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

}

}

return data;

}

/**

* 冒泡排序法

* @param data

* @return

* @Des 冒泡排序(Bubble Sort)分為若干趟進(jìn)行,每一趟排序從前往后比較每?jī)蓚€(gè)相鄰的元素的大?。ㄒ虼艘惶伺判蛞容^n-1對(duì)位置相鄰的數(shù))并在

* 每次發(fā)現(xiàn)前面的那個(gè)數(shù)比緊接它后的數(shù)大時(shí)交換位置;進(jìn)行足夠多趟直到某一趟跑完后發(fā)現(xiàn)這一趟沒(méi)有進(jìn)行任何交換操作(最壞情況下要跑n-1趟,

* 這種情況在最小的數(shù)位于給定數(shù)列的最后面時(shí)發(fā)生)。事實(shí)上,在第一趟冒泡結(jié)束后,最后面那個(gè)數(shù)肯定是最大的了,于是第二次只需要對(duì)前面n-1

* 個(gè)數(shù)排序,這又將把這n-1個(gè)數(shù)中最小的數(shù)放到整個(gè)數(shù)列的倒數(shù)第二個(gè)位置。這樣下去,冒泡排序第i趟結(jié)束后后面i個(gè)數(shù)都已經(jīng)到位了,第i+1趟實(shí)

* 際上只考慮前n-i個(gè)數(shù)(需要的比較次數(shù)比前面所說(shuō)的n-1要?。?。這相當(dāng)于用數(shù)學(xué)歸納法證明了冒泡排序的正確性

用加減乘除算法隨機(jī)出題java源代碼

import java.io.*;

import java.sql.*;

import javax.servlet.*;

import javax.servlet.http.*;

import oracle.jdbc.driver.OracleDriver;

public class GenPaperServlet extends HttpServlet

{

Connection conn;

Statement stmt;

ResultSet rs;

int total_question_num;

int total_question_in_paper;

int total_paper_num;

String curr_classid;

public GenPaperServlet()

{

conn = null;

stmt = null;

rs = null;

total_question_num = 0;

total_question_in_paper = 0;

total_paper_num = 0;

curr_classid = "";

}

public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)

throws ServletException, IOException

{

httpservletresponse.setContentType("text/html;charset=GBK");

PrintWriter printwriter = httpservletresponse.getWriter();

printwriter.println("htmlhead/headbodycenter");

printwriter.println("請(qǐng)以POST方式提交");

printwriter.println("/center/body/html");

printwriter.close();

用JAVA語(yǔ)言在網(wǎng)頁(yè)里實(shí)現(xiàn)加減乘除算法的源代碼要怎么寫(xiě)吖?!

public class Excer{

public static void main(String args[])

{

Excer ex=new Excer();

int x=0;

int y=0;

ex.math(x,y);

}

void math(int x,int y){

MyMath mm = new MyMath();

System.out.println("x="+x+" ,y="+y);

System.out.println("x+y="+mm.plus(x,y));

System.out.println("x-y="+mm.minus(x,y));

System.out.println("x*y="+mm.multi(x,y));

System.out.println("x/y="+mm.div(x,y));

}

}

class MyMath

{

int plus(int a,int b)

{

return(a+b);

}

int minus(int a,int b)

{

return(a-b);

}

int multi(int a,int b)

{

return(a*b);

}

float div(int a,int b)

{

return ((float)a/b);

}

}

網(wǎng)站欄目:java算法源代碼,java實(shí)現(xiàn)算法
文章網(wǎng)址:http://chinadenli.net/article22/hdocjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站排名網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作域名注冊(cè)、網(wǎng)站改版

廣告

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

外貿(mào)網(wǎng)站建設(shè)