基本演算法demo

TTMMJJ99發表於2017-11-29
//佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.number.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/num"
                android:hint="輸入階乘數"
                />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/start"
                android:text="開始階乘"
                />
            <TextView
                android:id="@+id/textname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />
            <TextView
                android:id="@+id/textname1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <TextView
                android:id="@+id/textname2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <TextView
                android:id="@+id/textname3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <TextView
                android:id="@+id/textname4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <TextView
                android:id="@+id/textname5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <TextView
                android:id="@+id/textname6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
//主要程式碼
 
package com.bwie.number;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText editText = findViewById(R.id.num);
        Button button = findViewById(R.id.start);
        TextView textView = findViewById(R.id.textname);
        TextView textView1 = findViewById(R.id.textname1);
        final TextView textView2 = findViewById(R.id.textname2);
        TextView textView3 = findViewById(R.id.textname3);
        TextView textView4 = findViewById(R.id.textname4);
        TextView textView5 = findViewById(R.id.textname5);
        TextView textView6 = findViewById(R.id.textname6);
        TextView textView7 = findViewById(R.id.textname7);
        TextView textView8 = findViewById(R.id.textname8);
        textView.setText("0.楊輝三角\n" + get(1, 5));
        textView1.setText("1.楊輝三角\n" + get(2, 8));
        textView3.setText("3.遞迴10的階乘:\n    " + digui(10));
        textView4.setText("4.12個月後兔子對數:\n   " + feibo(12));
        textView5.setText("5.無序的二維陣列\n" + erwei(4, 0));
        textView6.setText("6.行有序的二維陣列\n" + erwei(4, 1));

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num=editText.getText().toString();
                //textView2.setText("2.大數階乘:\n" + digui(new BigInteger(num)));
                textView2.setText("2.大數階乘:\n"+jiecheng(Integer.parseInt(num)));
            }
        });
    }

    //二維陣列
    public String erwei(int num, int type) {
        String a = "";
        int[][] arr = new int[num][num];
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                Random rand = new Random();
                int ran = rand.nextInt(num * num) + 1;
                arr[i][j] = feibo(ran);
            }
        }
        switch (type) {
            case 0://無序
                for (int i = 0; i < num; i++) {
                    for (int j = 0; j < num; j++) {
                        a += arr[i][j] + "  ";
                    }
                    a += "\n";
                }
                break;
            case 1://行有序
                for (int i = 0; i < num; i++) {
                    for (int j = 0; j < num; j++) {
                        int min = arr[i][j];
                        for (int m = j; m < num; m++) {
                            if (arr[i][m] < min) {
                                min = arr[i][m];
                                arr[i][m] = arr[i][j];
                                arr[i][j] = min;
                            }
                        }
                    }
                }
                for (int i = 0; i < num; i++) {
                    for (int j = 0; j < num; j++) {
                        a += arr[i][j] + "  ";
                    }
                    a += "\n";
                }
                break;
        }
        return a;
    }

    //遞迴
    public int digui(int length) {
        int num = length;
        if (num == 1) {
            return 1;
        } else {
            return num * digui(num - 1);
        }
    }

    //遞迴2
    public BigInteger digui(BigInteger length) {
        BigInteger num = length;
        BigInteger big1 = BigInteger.valueOf(1);
        if (num.compareTo(big1) == 0) {
            return big1;
        } else {
            return num.multiply(digui(num.subtract(big1)));
        }
    }
    //大數階乘
    private BigInteger jiecheng(int n) {
        BigInteger a=BigInteger.valueOf(n);
        for (int i=1;i<=n;i++){
            a = a.multiply(BigInteger.valueOf(i));
        }
        return a;
    }

    //斐波那契
    public int feibo(int moth) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i <= moth; i++) {
            if (i == 1 || i == 2) {
                list.add(i - 1, 1);
            } else {
                list.add(i - 1, list.get(i - 2) + list.get(i - 3));
            }
        }
        return list.get(moth - 1);
    }

    //楊輝三角
    public String get(int type, int count) {
        String arr = "";
        int num[][] = new int[count][count];//這個陣列有幾層
        for (int i = 0; i < count; i++)//主要是對陣列進行賦值
        {
            for (int j = 0; j <= i; j++)//每一層的個數都是小於等於層數的,m代表層數,n代表著第幾個數
            {
                if (j == 0 || i == j)//每一層的開頭都是1,i==n的時候也是1,必須要這個,凡事都得有個開頭
                {
                    num[i][j] = 1;
                } else
                    num[i][j] = num[i - 1][j - 1] + num[i - 1][j];//這個就是遞推的方法了,例如3=1+2,3的座標就是3[3,1]=1[2,0]+2[2,1];
            }
        }

        for (int i = 0; i < count; i++)//主要是輸出陣列
        {
            if (type == 1) {
                for (int l = i; l < count; l++)//這個主要是打空格,好看一點,去掉就是直角三角形了
                {
                    arr = arr + "   ";
                }
            }
            for (int j = count - i; j <= count; j++)//這個就是列印陣列了,每層迴圈幾次就幾個
            {
                if (num[i][count - j] < 8 || i == 2) {
                    arr = arr + num[i][count - j] + "    ";
                } else {
                    arr = arr + num[i][count - j] + "  ";
                }
            }
            arr = arr + "\n";
        }
        return arr;
    }
}


相關文章