网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java用算法实现代码,java编程算法

用JAVA语言在网页里实现加减乘除算法的源代码要怎么写吖?!

public class Excer{

创新互联公司2013年开创至今,是专业互联网技术服务公司,拥有项目网站设计制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元莱州做网站,已为上家服务,为莱州各地企业和个人服务,联系电话:18982081108

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

}

}

用java递归方法实现

1、递归做为一种算法在程序设计语言中广泛使用,是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象。

2、递归算法一般用于解决三类问题:

1)数据的定义是按递归定义的。(Fibonacci(斐波那契)的函数)

2)问题解法按递归算法实现。(回溯)

3)数据的结构形式是按递归定义的。(树的遍历,图的搜索)

怎么用java代码实现开平方算法?

这是我应聘时写的算法代码,运行成功:

//这是用java编写的一个求2的平方根的程序,精确度可通过修改weishu参数来改变

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;//定义循环次数

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[])//定义两数相乘的函数

{

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[])//定义两数相加的函数

{

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[])//定义任一数除以2的函数

{

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语言编写的银行家算法的源代码

import java.util.*;

class ThreadTest {

static int type = 4, num = 10; //定义资源数目和线程数目

static int[] resource = new int[type]; //系统资源总数

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

static Random rand = new Random();

static Bank[] bank = new Bank[num]; //线程组

Bank temp = new Bank();

public void init() {

//初始化组中每个线程,随机填充系统资源总数

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]; //从系统资源中减去已分配的

}

printer();

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

if(resource[i] 0) {

//若出现已分配量超出系统资源总数的错误则退出

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

//运行结束释放占有资源

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

//随机产生申请资源量并检测是否超标

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

//创建副本尝试分配请求

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

//后台线程,设定程序运行多长时间后自动结束

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

}

}

Java用查找算法的一段代码如下: 其中boolean A=false; if(name.equals(arr[i])) 麻烦解释一下 尽量直白

数组从第一个开始比较,完全相同(当前数组值和输入值一模一样)A就赋值为true;不一样A的值不变

求使用java实现的快排算法

① 代码:

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用算法实现代码,java编程算法
网站网址:http://bjjierui.cn/article/dssohps.html

其他资讯