java 面试题和算法

来源:互联网 发布:三级域名如何注册 编辑:程序博客网 时间:2024/05/17 01:25
package com.test.haoTest;

import java.io.UnsupportedEncodingException;

public class T11 {

    /**
     * @param args
     */
    public static void main(final String[] args) {

        final String number = Integer.toString(64, 8);
        System.out.println("number= " + number);
        final float t1 = (float) 17.1 % 4;
        System.out.println("t1= " + t1);
        final double q3 = 17.1 % 4;
        System.out.println("q3= " + q3);
        final int q1 = 2 % 5;
        System.out.println("q1= " + q1);
        final int q2 = 2 / 5;
        System.out.println("q2= " + q2);
        final int i1 = 153 % 10;
        final int i4 = 153 % 100;
        final int i2 = i4 / 10;
        final int i3 = 153 / 100;
        System.out.println("i1=" + i1);
        System.out.println("i2=" + i2);
        System.out.println("i3=" + i3);

        // TODO Auto-generated method stub
        final String s1 = "1567342";
        if (s1.compareTo("123345") >= 0) {
            System.out.println("ok1");
        }

        // 获取前一个Char值
        final int code = s1.codePointAt(1);

        final int codeBefore = s1.codePointBefore(5);
        System.out.println("codeBefore=" + codeBefore);
        System.out.println("codeBefore=" + (char) codeBefore);
        System.out.println("code=" + code);
        System.out.println("code1=" + (char) code);
        final char cr = s1.charAt(5);
        System.out.println("Cr=" + cr);
        final int cr1 = s1.charAt(5);
        System.out.println("Cr1=" + cr1);

        if (s1.charAt(5) == '2') {
            System.out.println("ok-----");
        }
        final int c = "we".charAt(1);
        System.out.println("C0=" + c);
        System.out.println("C1=" + (char) c);
        final int c2 = "12364".charAt(3);
        System.out.println("C2=" + new Character((char) c2));
        final int index = s1.indexOf(51);
        System.out.println("index=" + index);
        if (index > 0) {
            System.out.println("ok2");
        }

        // / ....
        final String s2 = "a1234560";
        final byte[] b = s2.getBytes();
        System.out.println("----1");
        System.out.println(b.length);
        final String s3 = "我 是你的";
        byte[] by1 = null;
        try {
            by1 = s3.getBytes("UTF-8");
            System.out.println("----2");
            System.out.println(by1.length);
            by1 = s3.getBytes("GBK");
            System.out.println("----3");
            System.out.println(by1.length);
            // G B2312
            by1 = s3.getBytes("GB2312");
            System.out.println("----G B2312");
            System.out.println(by1.length);
        } catch (final UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            final String str = "Welcome to RoseIndia.";
            // Copy the contents of the String to a byte array using the ASCII
            // encoding.
            final byte[] arr = str.getBytes("ASCII");
            // Create a new String using the contents of the byte array.
            final String newStr = new String(arr);
            // Display the contents of the byte array.
            System.out.println("The new String equals \"" + newStr + "\"");
        } catch (final Exception ex) {
            ex.printStackTrace();
        }

    }

}

上面是第一个类

package com.test.haoTest;

public class TestRabbitValue {

    /**
     * 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月 又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
     * 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
     */

    public static void main(final String[] args) {
        // TODO Auto-generated method stub
        months(9);
        primeNumber();
        getPowerOf(2, 3);
        final String score = foo(90);
        System.out.println("score= " + score);
        final TestRabbitValue tr = new TestRabbitValue();
        final long peachsum = tr.getCountPeach();
        System.out.println("tao zi1 = " + peachsum);
        toSum();

        final long peachCount = tr.tao(9);
        System.out.println("tao zi2 = " + peachCount);
    }

    public static void months(final int n) {
        int sum = 0;
        final int[] num = new int[n];
        num[0] = 1;
        num[1] = 1;

        if (n == 1 || n == 2) {
            sum = 1;

        } else {
            for (int j = 2; j <= n - 1; j++) {
                num[j] = num[j - 1] + num[j - 2];
            }

        }

        System.out.println("sum= " + sum + ",num= " + num[n - 1]);
    }

    /**
     * 判断101-200之间有多少个素数,并输出所有素数。 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
     * 则表明此数不是素数,反之是素数。
     */
    public static void primeNumber() {
        boolean flag;
        int count = 0;
        for (int i = 101; i <= 200; i++) {
            flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                ++count;
                System.out.println("素数1=" + i + "\n ");
            }
        }
        System.out.println("count=" + count);
    }

    /**
     * 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。 例如:153是一个
     * "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
     * 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
     */

    public static void flower() {
        int num1 = 0; // 个位
        int num2 = 0; // 十位
        int num3 = 0; // 百位
        for (int i = 100; i < 1000; i++) {
            num1 = i % 10;
            num2 = i % 100 / 10;
            num3 = i / 100;
            if (i == num1 * num1 * num1 + num2 * num2 * num2 + num3 * num3 * num3) {
                System.out.println(i);
            }
        }
    }

    /**
     * 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
     * 几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值。
     */
    public static void getPowerOf(final int a, final int num) {
        int sum = 0;
        for (int i = 1; i <= num; i++) {
            int t = i;
            while (t > 0) {
                // System.out.println( "ran: "+a*(Math.pow(10,t-1)));
                sum += a * Math.pow(10, t - 1);
                t--;
            }
        }
        System.out.println("sum: " + sum);
    }

    private static String foo(final int score) {
        // return score >= 90 ? "A " : score >= 60 ? "B " : "C ";
        return score >= 90 ? "A" : score < 60 ? "C" : "B";
    }

    // 求完数
    // 一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数。
    public static void toSum() {
        for (int i = 2; i < 1000; i++) {
            int sum = 0;
            for (int j = 1; j < i; j++) {
                if (i % j == 0) {
                    sum = sum + j;
                }
            }
            if (sum == i) {
                System.out.println(i + "是个完数.");
            }
        }
    }

    /**
     * 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
     * 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
     * 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
     */
    long i;// 变量
    long j = 1;// 最后一天剩下的个数

    public long getCountPeach() {

        for (int day = 9; day > 0; day--) {
            i = (j + 1) * 2;
            j = i;
            // System.out.println(j);
        }
        return j;
    }

    // ///////////////////////////////
    long k;

    // 递归算法
    public long tao(final int day) {
        if (day > 0) {
            k = (tao(day - 1) + 1) * 2;
        } else if (day == 0) // 第最后一天的个数
        {
            return 1;
        }
        return k;
    }
}

上面是第二类

package com.budco.bmp.brand.plugin.actos.segment.helper;

public class TestMaxSum {

    // /固定值 基础数据
    // 养老 医疗 失业 生育 工伤
    // 400 180 40 16 10
    //
    // /给出一个总数,满足任意一个或多个值相加等于总数就返回各个值,没有参与到相加里面的就为0
    // 例如:
    // 总数 466 ,返回的是 400 + 0 + 40 + 16 + 10
    // 总数 580 ,返回的是 400 + 180 + 0 + 0 + 0
    // 总数 440 ,返回的是 400 + 0 + 40 + 0 + 0
    //
    //
    static int[] num = new int[] { 400, 180, 40, 16, 10 };
    static final String NO_RESULT = "No results";

    static String check(final int[] num, final int value) {
        int sum = 0;
        final int[] addNum = new int[] { 0, 0, 0, 0, 0 };
        for (int i = 0; i < num.length; i++) {
            if (num[i] > value) {
                continue;
            }
            sum += num[i];
            addNum[i] = num[i];
            if (sum == value) {
                final StringBuffer result = new StringBuffer("count " + value + " return is ");
                for (int j = 0; j < addNum.length - 1; j++) {
                    result.append(addNum[j] + " + ");
                }
                result.append(addNum[addNum.length - 1]);
                return result.toString();
            } else if (sum > value) {
                sum -= num[i];
                addNum[i] = 0;
            }
        }
        return NO_RESULT;
    }

    /**
     * @param args
     */
    public static void main(final String[] args) {
        // TODO Auto-generated method stub
        System.out.println(check(num, 466));
        System.out.println(check(num, 580));
        System.out.println(check(num, 440));
        System.out.println(check(num, 210));
        // 可以
    }
    /*
     * http://topic.csdn.net/u/20110907/15/b1ae9f15-4450-47da-b99c-99da8587e244.html
     */
}

上面是第三类