public class Excer{
創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元萊州做網(wǎng)站,已為上家服務(wù),為萊州各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話: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);
}
}
1、遞歸做為一種算法在程序設(shè)計(jì)語言中廣泛使用,是指函數(shù)/過程/子程序在運(yùn)行過程中直接或間接調(diào)用自身而產(chǎn)生的重入現(xiàn)象。
2、遞歸算法一般用于解決三類問題:
1)數(shù)據(jù)的定義是按遞歸定義的。(Fibonacci(斐波那契)的函數(shù))
2)問題解法按遞歸算法實(shí)現(xiàn)。(回溯)
3)數(shù)據(jù)的結(jié)構(gòu)形式是按遞歸定義的。(樹的遍歷,圖的搜索)
這是我應(yīng)聘時(shí)寫的算法代碼,運(yùn)行成功:
//這是用java編寫的一個(gè)求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("左邊計(jì)算到"+a[0]+"位\n");
for(i=b[0];i=1;i--)
System.out.print(b[i]);
System.out.print("右邊計(jì)算到"+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;
}
}
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() {
//初始化組中每個(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) {
//程序沒有完成時(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)量超過所需量
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)資源超過可供資源則等待一段時(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)行多長時(shí)間后自動(dòng)結(jié)束
new Timeout(30000, "---Stop!!!---");
}
}
數(shù)組從第一個(gè)開始比較,完全相同(當(dāng)前數(shù)組值和輸入值一模一樣)A就賦值為true;不一樣A的值不變
① 代碼:
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("?");
}
}
}
② 運(yùn)行:
c:\java?quicksortdemo
2?2?12?20?24?45?53?56?56?75?99