public class Test {
淮上網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司公司2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
public static void main(String[] args) {
try{
LinkList list1 = new LinkList();
LinkList list2 = new LinkList();
LinkList list3 = null;
list1.addAt(0, new Item(1, 5));
list1.addAt(1, new Item(-1.5, 3));
list1.addAt(2, new Item(1, 1));
list2.addAt(0, new Item(0.5, 5));
list2.addAt(1, new Item(0.5, 4));
list2.addAt(2, new Item(1.5, 3));
list2.addAt(3, new Item(3, 0));
list3 = mergeLinkList(list1, list2);
System.out.println("一元多項(xiàng)式的相加過程:");
list1.listAll();
System.out.println(" + ");
list2.listAll();
System.out.println(" = ");
list3.listAll();
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 一元多項(xiàng)式的一般項(xiàng)類
*/
class Item{
private double coef; ?//一元多項(xiàng)式的一般項(xiàng)的系數(shù)
private int exp; ? //一元多項(xiàng)式的一般項(xiàng)的指數(shù)
public Item(){
this.coef = 0.0;
this.exp = 0;
}
public Item(double coef, int exp){
this.coef = coef;
this.exp = exp;
}
public double getCoef(){
return this.coef;
}
public void setCoef(double coef){
this.coef = coef;
}
public int getExp(){
return this.exp;
}
public void setExp(int exp){
this.exp = exp;
}
}
/**
* 鏈表結(jié)點(diǎn)類
*/
class Node{
private Item data;
private Node next; ? //鏈表結(jié)點(diǎn)的指針域,指向直接后繼結(jié)點(diǎn)
public Node(){
data = null;
next = null;
}
public Node(Item data, Node next){
this.data = data;
this.next = next;
}
public Item getData(){
return this.data;
}
public void setData(Item data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
/**
* 鏈表類
*/
class LinkList{
private Node head = null; //頭結(jié)點(diǎn)指針
private int size = 0;
public LinkList(){
head = new Node();
size = 0;
}
//在i位置插入元素elem
public boolean addAt(int i, Item elem) {
if(i 0 || i size){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size++;
return true;
}
//刪除i位置的元素
public boolean removeAt(int i) {
if(i 0 || i = size){
return false;
}
Node pre,curr;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = pre.getNext();
pre.setNext(curr.getNext());
size--;
return true;
}
java是一種可以撰寫跨平臺(tái)應(yīng)用軟件的面向?qū)ο蟮某绦蛟O(shè)計(jì)語言。Java?技術(shù)具有卓越的通用性、高效性、平臺(tái)移植性和安全性,廣泛應(yīng)用于PC、數(shù)據(jù)中心、游戲控制臺(tái)、科學(xué)超級(jí)計(jì)算機(jī)、移動(dòng)電話和互聯(lián)網(wǎng),同時(shí)擁有全球最大的開發(fā)者專業(yè)社群。
public class Test {
public static void main(String[] args) {
try{
LinkList list1 = new LinkList();
LinkList list2 = new LinkList();
LinkList list3 = null;
list1.addAt(0, new Item(1, 5));
list1.addAt(1, new Item(-1.5, 3));
list1.addAt(2, new Item(1, 1));
list2.addAt(0, new Item(0.5, 5));
list2.addAt(1, new Item(0.5, 4));
list2.addAt(2, new Item(1.5, 3));
list2.addAt(3, new Item(3, 0));
list3 = mergeLinkList(list1, list2);
System.out.println("一元多項(xiàng)式的相加過程:");
list1.listAll();
System.out.println(" + ");
list2.listAll();
System.out.println(" = ");
list3.listAll();
}
catch(Exception e){
e.printStackTrace();
}
}
//兩個(gè)一元多項(xiàng)式相加,返回新的一元多項(xiàng)式
public static LinkList mergeLinkList(LinkList list1, LinkList list2){
int i = 0;
Item item = new Item();
Node curr1, curr2;
LinkList list3 = new LinkList();
curr1 = list1.getHead().getNext();
curr2 = list2.getHead().getNext();
while(curr1 != null curr2 != null){
if(curr1.getData().getExp() curr2.getData().getExp()){
item = curr1.getData();
list3.addAt(i, item);
curr1 = curr1.getNext();
i++;
}
else if(curr1.getData().getExp() curr2.getData().getExp()){
item = curr2.getData();
list3.addAt(i, item);
curr2 = curr2.getNext();
i++;
}
else{
item = new Item(curr1.getData().getCoef() + curr2.getData().getCoef(), curr1.getData().getExp());
if(item.getCoef() != 0){
list3.addAt(i, item);
i++;
}
curr1 = curr1.getNext();
curr2 = curr2.getNext();
}
}
while(curr1 != null){
item = curr1.getData();
list3.addAt(i++, item);
curr1 = curr1.getNext();
}
while(curr2 != null){
item = curr2.getData();
list3.addAt(i++, item);
curr2 = curr2.getNext();
}
return list3;
}
}
/**
* 一元多項(xiàng)式的一般項(xiàng)類
*/
class Item{
private double coef; //一元多項(xiàng)式的一般項(xiàng)的系數(shù)
private int exp; //一元多項(xiàng)式的一般項(xiàng)的指數(shù)
public Item(){
this.coef = 0.0;
this.exp = 0;
}
public Item(double coef, int exp){
this.coef = coef;
this.exp = exp;
}
public double getCoef(){
return this.coef;
}
public void setCoef(double coef){
this.coef = coef;
}
public int getExp(){
return this.exp;
}
public void setExp(int exp){
this.exp = exp;
}
}
/**
* 鏈表結(jié)點(diǎn)類
*/
class Node{
private Item data;
private Node next; //鏈表結(jié)點(diǎn)的指針域,指向直接后繼結(jié)點(diǎn)
public Node(){
data = null;
next = null;
}
public Node(Item data, Node next){
this.data = data;
this.next = next;
}
public Item getData(){
return this.data;
}
public void setData(Item data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
/**
* 鏈表類
*/
class LinkList{
private Node head = null; //頭結(jié)點(diǎn)指針
private int size = 0;
public LinkList(){
head = new Node();
size = 0;
}
//在i位置插入元素elem
public boolean addAt(int i, Item elem) {
if(i 0 || i size){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size++;
return true;
}
//刪除i位置的元素
public boolean removeAt(int i) {
if(i 0 || i = size){
return false;
}
Node pre,curr;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = pre.getNext();
pre.setNext(curr.getNext());
size--;
return true;
}
//根據(jù)值value查詢結(jié)點(diǎn)是否存在,若存在返回位置,否則返回-1
public int findByValue(Item value){
Node curr;
int pos;
for(pos=0,curr=head.getNext(); curr!=null; pos++,curr=curr.getNext()){
if(curr.getData().toString().equals(value.toString())){
break;
}
}
if(curr==null){
return -1;
}
return pos;
}
public Node getHead(){
return this.head;
}
public void setHead(Node head){
this.head = head;
}
public int getSize(){
return this.size;
}
public boolean isEmpty(){
return (size==0);
}
public void listAll(){
for(Node curr=head.getNext(); curr!=null; curr=curr.getNext()){
System.out.print("(" + curr.getData().getCoef() + ", " + curr.getData().getExp() + ")\t");
}
System.out.println();
}
}
兩種方法,一種直接用循環(huán)計(jì)算,用循環(huán)依次對(duì)1到n進(jìn)行疊加,具體如下:
public?class?Exos
{
public?static?void?main(String[]?args){
int?n?=?10;
int?sum?=?0;
for(int?i=0;in;i++){
sum?=?sum?+?i+1;
}
System.out.println("The?sum?is:?"?+?sum);
}
}
另一種方式是直接采用求和公式(前n項(xiàng)和求和公式為:(首項(xiàng)+末項(xiàng))*項(xiàng)數(shù)/2):
public?class?Exos
{
public?static?void?main(String[]?args){
int?n?=?10;
int?sum?=?n*(n+1)/2;
System.out.println("The?sum?is:?"?+?sum);
}
}
如果還有不清楚的地方,歡迎追問。