重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本人特地给你编的代码
成都网站制作、做网站,成都做网站公司-创新互联已向上千家企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。
亲测
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]);
}
}
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!!!---");
}
}
贪心算法: 思路就是对花到第一个喷泉距离从近到远排序,然后找到另一个喷泉距离最大的一个
复杂度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编写的一个求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;
}
}
① 代码:
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