码迷,mamicode.com
首页 > 编程语言 > 详细

初级算法题(代码为java编写)

时间:2018-03-11 17:38:23      阅读:185      评论:0      收藏:0      [点我收藏+]

标签: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编写)

标签:oid   markdown   进制转换   cat   mat   自然数   很多   变量   数值   

原文地址:https://www.cnblogs.com/drinkoo/p/8543802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!