初級演算法題(代碼為java編寫)

來源:互聯網
上載者:User

標籤:oid   markdown   進位轉換   cat   mat   自然數   很多   變數   數值   

分享幾個演算法題,經典可能有點算不上,只能算是初學演算法的一點小題目吧

聲明:每道題思路可能不止一種,可能有很多種,具體詳情請自己百度,Thank you!

1.交換兩個數(盡量用多種方法)

分析

  • swap1,最常用的用一個temp變數來交換
  • swap2,兩個數相加的和然後減去b的值,那麼剩下的值就為a,此時將值賦值給b,這樣就把值交換了
  • swap3,一個數異或同一個數兩次後還是那個數
/** * @author Harry * 1.交換兩個數(盡量用多種方法) */public class day01 {        public static void swap1(int a,int b) {        int temp = a;        a = b;        b = temp;        System.out.println(a+","+b);    }        public static void swap2(int a,int b) {        a = a + b;        b = a - b;        a = a - b;        System.out.println(a + "," + b);    }        public static void swap3(int a,int b) {        a = a^b;        b = a^b;        a = a^b;        System.out.println(a + "," + b);    }        public static void main(String[] args) {        swap1(1, 2);        swap2(1, 2);        swap3(1, 2);    }}
2.楊輝三角

分析:思路也有很多種,相應的實現代碼也有很多,具體的自己百度吧,這裡僅展現一種!觀察後會發現每一行的第一個和最後一個的數值都為1,而剩下的數為上一行本列的數和上一行本列的前一個數的和。

/** * @author Harry * 2.楊輝三角 */public class day02 {    public static void main(String[] args) {        int MMax = 10;  //列印行數        int arr[][] = new int[MMax+1][MMax+1];  //定義一個數組來裝所有數                for (int i = 0; i < arr.length; i++) {            for(int j=0;j<=i;j++) {                if(j==0 || j==i ) {                    //將第一個和最後一個都賦值為1                    arr[i][j] = 1;                }else {                    //將上一行的本列數與上一行的本列前一個數相加得到此處的數字                    arr[i][j] = arr[i-1][j] + arr[i-1][j-1];                }            }        }                for (int i = 0; i < arr.length; i++) {            for(int j=0;j<=i;j++) {                System.out.print(arr[i][j] + "\t");            }            System.out.println();        }    }}
3.斐波那契數列

分析:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610...很明顯的可以看出來從第三個數開始,每個數的值為前兩個數的和,直接遞迴解決,至於不太懂遞迴的同學,多去看看遞迴的原理,我就不多說了。

/** *  * @author Harry * 3.著名的菲波拉契(Fibonacci)數列 */public class day03 {        public static int f(int n) {        if(n==0) return 0;        if(n==1||n==2)             return 1;        else            return f(n-1) + f(n-2);    }        public static void main(String[] args) {        int x = 20;        for(int i=0; i<20; i++)            System.out.println(f(i));    }}
4.判斷迴文字串

分析:例如ABCDBCA 和 ABCDDBCA 都屬於迴文字串,思路很簡單,從兩端開始對比是否相同,只要有一個不同返回false,相同返回true

/** * @author Harry * 4.迴文字串 */public class day04 {        public static void f(char[] ch) {        for(int i=0;i<ch.length;i++) {            if(ch[i] != ch[ch.length-i-1]) {                System.out.println("不是迴文字串");                return;            }        }        System.out.println("是迴文字串");    }        public static void main(String[] args) {        String str = "ABCDCBAA";        char[] ch = str.toCharArray();        f(ch);    }}
5.使用Java 計算 9223372036854775807 + 1的值

分析:個人使用了最簡單的方法來算這種數,直接用大整數類型BigInteger

import java.math.BigInteger;/** * @author Harry * 5.使用Java 計算 9223372036854775807 + 1的值 */public class day05 {    public static void main(String[] args) {         BigInteger b = new BigInteger("9223372036854775807");         System.out.println(b.add(new BigInteger("1")));    }}
6.角穀定理

角穀定理:一個自然數,若為偶數,則把它除以2,若為奇數,則把它乘以3加1.經過如此有限運算後,總可以得到自然數值1.求經過多少次可以得到自然數1.

/** * @author Harry * 角穀定理,輸入一個自然數,若為偶數,則把它除以2,若為奇數,則把它乘以* 3加1. */public class day06_02 {    public static void main(String[] args) {        int a = 3;        int count = 0;        while(a != 1) {            if(a % 2 == 0)                 a /= 2;            else                a = a * 3 + 1;            count++;        }        System.out.println(count + "次");    }}
7.將十進位轉換為二進位

分析:將十進位轉換為二進位,兩種方法。(提示:1.使用jdk內建方法 2.使用逢二進一方法)

/** * @author Harry * 3.將十進位轉換為二進位,兩種方法。 *  (提示:1.使用jdk內建方法 2.使用逢二進一方法) */public class day7 {    public static void convert1(int a) {        String str = Integer.toBinaryString(a);        System.out.println(str);    }        public static void convert2(int a) {        int n;        String str = "";        while(a != 0) {            n = a%2;            a /= 2;            str = n + str;            str.concat(str);        }        System.out.println(str);    }        public static void main(String[] args) {        int a = 100;        convert1(a);        convert2(a);    }}

如有錯誤的地方還請提醒我,讓我改正,以免誤人子弟,如果你們還有更好的題目,也請在評論區留言,以後會考慮加在文章內的!

初級演算法題(代碼為java編寫)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.