java語言完整代碼實(shí)現(xiàn) java語言編程實(shí)例

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

貪心算法: 思路就是對(duì)花到第一個(gè)噴泉距離從近到遠(yuǎn)排序,然后找到另一個(gè)噴泉距離最大的一個(gè)

創(chuàng)新互聯(lián)2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元東昌府做網(wǎng)站,已為上家服務(wù),為東昌府各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

復(fù)雜度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語言實(shí)現(xiàn)單向鏈表

1.先定義一個(gè)節(jié)點(diǎn)類

package com.buren;

public class IntNode {

//定義一個(gè)節(jié)點(diǎn)類

int

info;

//定義屬性,節(jié)點(diǎn)中的值

IntNode next;

//定義指向下一個(gè)節(jié)點(diǎn)的屬性

public IntNode(int

i){ //構(gòu)造一個(gè)next為空的節(jié)點(diǎn)

this(i,null);

}

public IntNode(int i,IntNode

n){ //構(gòu)造值為i指向n的節(jié)點(diǎn)

info=i;

next=n;

}

}

2.再定義一個(gè)鏈表類,這是主要部分

package com.buren;

public class IntSLList {

private IntNode head,tail;

//定義指向頭結(jié)點(diǎn)和尾結(jié)點(diǎn)的指針,

//如果大家看著這個(gè)不像指針的話,那就需要對(duì)指針有更深刻的了解

public

IntSLList(){

//定義一個(gè)空節(jié)點(diǎn)

head=tail=null;

}

public boolean

isEmpty(){

//判斷節(jié)點(diǎn)是否為空

return

head==null;

//這行代碼看起來似乎很神奇,其實(shí)真的很神奇,偶是服了

}

public void addToHead(int el){

//將el插入到頭結(jié)點(diǎn)前

head=new

IntNode(el,head);

//將節(jié)點(diǎn)插入到頭結(jié)點(diǎn)前,作為新的投節(jié)點(diǎn)

if(head==tail){

//給空鏈表插入節(jié)點(diǎn)時(shí)

tail=head;

//頭結(jié)點(diǎn)和尾結(jié)點(diǎn)指向同一個(gè)節(jié)點(diǎn)

}

}

public void addToTail(int

el){

//向鏈表的尾部增加結(jié)點(diǎn)

if(!isEmpty()){

//判斷鏈表是否為空

tail.next=new

IntNode(el);

//新建立一個(gè)值為el的節(jié)點(diǎn),將鏈表的尾結(jié)點(diǎn)指向新節(jié)點(diǎn)

tail=tail.next;

//更新尾指針的指向

}else{

head=tail=new

IntNode(el);

//如果鏈表為空,新建立一個(gè)節(jié)點(diǎn),將頭尾指針同時(shí)指向這個(gè)節(jié)點(diǎn)

}

}

public int

deleteFromHead(){

//刪除頭結(jié)點(diǎn),將節(jié)點(diǎn)信息返回

int

el=head.info;

//取出節(jié)點(diǎn)信息

if(head==tail){

//如果鏈表中只有一個(gè)節(jié)點(diǎn)

head=tail=null;

//刪除這一個(gè)節(jié)點(diǎn)

}else{

head=head.next;

//如果鏈表中不止一個(gè)節(jié)點(diǎn),將頭結(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)作為頭結(jié)點(diǎn)

}

return

el;

//返回原頭結(jié)點(diǎn)的值

}

public int

deleteFromTail(){

//刪除尾結(jié)點(diǎn),返回尾結(jié)點(diǎn)的信息

int

el=tail.info;

//取出尾結(jié)點(diǎn)的值

if(head==tail){

// 如果鏈表中只有一個(gè)節(jié)點(diǎn)

head=tail=null;

//刪除這個(gè)節(jié)點(diǎn)

}else{

IntNode

temp;

//定義中間變量

for(temp=head;temp.next!=tail;temp=temp.next);

//找出尾結(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn),注意最后的分號(hào),

//這個(gè)for循環(huán)是沒有循環(huán)體的,目的在于找出尾結(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)

//在整個(gè)程序中用了很多次這樣的寫法,相當(dāng)經(jīng)典啊

tail=temp;

//將找出來的節(jié)點(diǎn)作為尾結(jié)點(diǎn),刪除原來的尾結(jié)點(diǎn)

tail.next=null;

//將新尾結(jié)點(diǎn)的指向設(shè)為空

}

return

el;

//返回原尾結(jié)點(diǎn)的信息

}

public void

printAll(){

//打印鏈表中所有節(jié)點(diǎn)的信息

if(isEmpty()){

//如果鏈表為空

System.out.println("This

list is

empty!");

//輸出提示信息

return;

//返回到調(diào)用的地方

}

if(head==tail){

//當(dāng)鏈表中只有一個(gè)節(jié)點(diǎn)時(shí)

System.out.println(head.info);

//輸出這個(gè)節(jié)點(diǎn)的信息,就是頭結(jié)點(diǎn)的信息

return;

}

IntNode

temp;

//定義一個(gè)中間變量

for(temp=head;temp!=null;temp=temp.next){

//遍歷整個(gè)鏈表

System.out.print(temp.info+"

");

//輸出每個(gè)節(jié)點(diǎn)的信息

}

System.out.println();

//輸出一個(gè)換行,可以沒有這一行

}

public boolean isInList(int

el){

//判斷el是否存在于鏈表中

IntNode

temp;

//定義一個(gè)中間變量

for(temp=head;temp!=null

temp.info!=el;temp=temp.next);

//將el找出來,注意最后的分

return

temp!=null;

// 如果存在返回true,否則返回flase,這兩行代碼很有思想

}

public void delete(int

el){

//刪除鏈表中值為el的節(jié)點(diǎn)

if(head.info==el

head==tail){

//如果只有一個(gè)節(jié)點(diǎn),并且節(jié)點(diǎn)的值為el

head=tail=null;

//刪除這個(gè)節(jié)點(diǎn)

}else

if(head.info==el){

// 不止一個(gè)節(jié)點(diǎn),而頭結(jié)點(diǎn)的值就是el

head=head.next;

//刪除頭結(jié)點(diǎn)

}else{

IntNode

pred,temp;

//定義兩個(gè)中間變量

for(pred=head,temp=head.next;temp.info!=el

temp.next!=null;pred=pred.next,temp=temp.next);

//跟上面的類似,自己琢磨吧,也是要注意最后的分號(hào)

pred.next=temp.next;

//將temp指向的節(jié)點(diǎn)刪除,最好畫一個(gè)鏈表的圖,有助于理解

if(temp==tail){

//如果temp指向的節(jié)點(diǎn)是尾結(jié)點(diǎn)

tail=pred;

//將pred指向的節(jié)點(diǎn)設(shè)為尾結(jié)點(diǎn),

}

}

}

//下面這個(gè)方法是在鏈表中值為el1的節(jié)點(diǎn)前面插入一個(gè)值為el2的節(jié)點(diǎn),

//用類似的思想可以再寫一個(gè)在鏈表中值為el1的節(jié)點(diǎn)后面插入一個(gè)值為el2的節(jié)點(diǎn)

public boolean insertToList(int el1,int

el2){

//定義一個(gè)插入節(jié)點(diǎn)的方法,插入成功返回true,否則返回false

IntNode

pred,temp; //定義兩個(gè)中間變量

if(isEmpty()){

//判斷鏈表是否為空

return

false;

//如果鏈表為空就直接返回false

}

if(head.info==el1

head==tail){

//如果鏈表中只有一個(gè)節(jié)點(diǎn),并且這個(gè)節(jié)點(diǎn)的值是el1

head=new

IntNode(el2,head);

//新建立一個(gè)節(jié)點(diǎn)

return

true;

}else if(head.info==el1){

IntNode t=new

IntNode(el2);

t.next=head;

head=t;

return

true;

}else{

for(pred=head,temp=head.next;temp!=null

temp.info!=el1;pred=pred.next,temp=temp.next);

if(temp!=null){

IntNode

a=new IntNode(el2);

pred.next=a;

a.next=temp;

return

true;

}else{

System.out.println(el1+"

NOT EXEISTS!");

return

false;

}

}

}

3.下面是測試代碼

public static void main(String[] args){

IntSLList test=new

IntSLList();

//test.addToHead(7);

test.addToTail(7);

System.out.println(test.insertToList(7,5));

test.printAll();

System.out.println(test.isInList(123));

}

}

JAVA語言實(shí)現(xiàn)

實(shí)現(xiàn)代碼如下:

erface StackPK{

void Push(Object obj);

Object Pop;

boolean isEmpty;

Size;

}

public LinkStack implements StackPK{

private SLLNode{

private Object data;

private SLLNode next;

SLLNode{}

SLLNode(Object obj){

data=obj;

}

public void Data(Object o){

(onull)

throw IllegalArgumentException("object is null");

data=o;

}

public Object getData{

data;

}

public void Next(SLLNode n){

next=n;

}

public SLLNode getNext{

next;

}

public String toString{

(String)data;

}

}

private SLLNode top;

public LinkStack{

top=null;

}

public void Push(Object obj){

(objnull)

throw IllegalArgumentException("n is null");

(topnull)

{

SLLNode temp= SLLNode(obj);

top=temp;

}

{

SLLNode temp= SLLNode(obj);

temp.Next(top);

top=temp;

}

}

public Object Pop{

SLLNode temp;

(topnull)

throw IllegalArgumentException("stack is empty");

temp=top;

top=top.getNext;

temp.getData;

}

public boolean isEmpty{

(top null)

true;

false;

}

public Size{

SLLNode cnt;

cnt=top;

count=0;

(cnt null)

0;

while(cnt != null){

count;

cnt=cnt.getNext;

}

count;

}

public void (String args){

LinkStack ls= LinkStack;

Object arr= String;

for( i=0;iarr.length;i)

ls.Push(arr[i]);

while(ls.Size0)

{

.out.prln(ls.Pop);

}

}

}

用java語言怎么實(shí)現(xiàn)從鍵盤輸入一個(gè)數(shù)

你好。示例代碼如下:

import java.util.Scanner;

public class Num {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in) ;

System.out.println("請(qǐng)輸入一個(gè)數(shù)字:");

int a = sc.nextInt() ;

System.out.println("您輸入的數(shù)字為:" + a);

}

}

名稱欄目:java語言完整代碼實(shí)現(xiàn) java語言編程實(shí)例
URL地址:http://muchs.cn/article36/hjohsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、云服務(wù)器網(wǎng)站營銷、品牌網(wǎng)站制作品牌網(wǎng)站設(shè)計(jì)、建站公司

廣告

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

成都網(wǎng)頁設(shè)計(jì)公司