首先觀察數組可以排除幾個數:588,4375,5184 因為他們如何相加也不會以0結尾。限於篇幅,部分代碼如下,剩餘的循環可以自己添加下,採用窮舉法:
public class DoMain {
public void doit(){
int[] a = new int[] { 460, 720, 1250, 1800, 2200, 3080, 4100,
6510, 6900, 9000 };
for(int i1=0;i1<a.length;i1++){
for(int i2=i1+1;i2<a.length;i2++){
if(a[i1]+a[i2]==13750){
System.out.println(a[i1]+";"+a[i2]);
}
for(int i3=i2+1;i3<a.length;i3++){
if(a[i1]+a[i2]+a[i3]==13750){
System.out.println(a[i1]+";"+a[i2]+";"+a[i3]);
}
for(int i4=i3+1;i4<a.length;i4++){
if(a[i1]+a[i2]+a[i3]+a[i4]==13750){
System.out.println(a[i1]+";"+a[i2]+";"+a[i3]+";"+a[i4]);
}
for(int i5=i4+1;i5<a.length;i5++){
if(a[i1]+a[i2]+a[i3]+a[i4]+a[i5]==13750){
System.out.println(a[i1]+";"+a[i2]+";"+a[i3]+";"+a[i4]+";"+a[i5]);
}
for(int i6=i5+1;i6<a.length;i6++){
if(a[i1]+a[i2]+a[i3]+a[i4]+a[i5]+a[i6]==13750){
System.out.println(a[i1]+";"+a[i2]+";"+a[i3]+";"+a[i4]+";"+a[i5]+";"+a[i6]);
}
for(int i7=i6+1;i7<a.length;i7++){
if(a[i1]+a[i2]+a[i3]+a[i4]+a[i5]+a[i6]+a[i7]==13750){
System.out.println(a[i1]+";"+a[i2]+";"+a[i3]+";"+a[i4]+";"+a[i5]+";"+a[i6]+";"+a[i7]);
}
}
}
}
}
}
}
}
}
public static void main(String[] args) {
DoMain main= new DoMain();
main.doit();
}}
② 關於Java演算法編程的 做一個小游戲,電腦隨機生成三個骰子(每個骰子點數為1~6),將這三個數相加。
1 設計一個int數組!長度為3.
2 設計3個變數!a, b, c. 分別取值為1-6.int a= new Random(6)+1.重復b, c.
3 把a, b, c放入數組中!
4 循環遍歷數組把三個元素相加 輸出這把總和是
5 判斷總和能否被2整除,如果能,輸出是雙數,不能輸出是單數!
6 判斷大小 大於等於4&&小於等於10 顯示開小
大於等於11&&小於等於17顯示開大
7 遍歷數組 判斷三個元素是否相等 相等顯示開豹子。。
手機打字!!有什麼問題在問吧
③ 用java單鏈表實現一元多項式相加的演算法
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("一元多項式的相加過程:");
list1.listAll();
System.out.println(" + ");
list2.listAll();
System.out.println(" = ");
list3.listAll();
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 一元多項式的一般項類
*/
class Item{
private double coef; //一元多項式的一般項的系數
private int exp; //一元多項式的一般項的指數
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;
}
}
/**
* 鏈表結點類
*/
class Node{
private Item data;
private Node next; //鏈表結點的指針域,指向直接後繼結點
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; //頭結點指針
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; i>0 && 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; i>0 && pre.getNext()!=null; i--,pre=pre.getNext());
curr = pre.getNext();
pre.setNext(curr.getNext());
size--;
return true;
}
java是一種可以撰寫跨平台應用軟體的面向對象的程序設計語言。Java技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、游戲控制台、科學超級計算機、行動電話和互聯網,同時擁有全球最大的開發者專業社群。