java 程序算法题-面试笔试题(二)

/**
     * 求某两个数的最大公因数和最小公倍数
     */
    @Test
    public void testMaxYinshu() {
        int a = 36;
        int b = 16;
        int x = a;
        int y = b;
        if (x < y) {
            int k = y;
            y = x;
            x = k;
        }
        while (y != 0) {
            if (x == y) {
                break;
            }
            int k = y;
            y = x % y;
            x = k;
        }
        System.out.println(a + "和" + b + "最大公因数是:" + x);
        System.out.println(a + "和" + b + "最小公倍数是:" + a * b / x);
    }

    @Test
    public void testCountString() {
        String s = "HHhh  hhhh 123&&";
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            //判断是否是数字
            if (Character.isDigit(chars[i])) {
                a++;
                //判断是否是字母
            } else if (Character.isLetter(chars[i])) {
                //判断是否是大写字母
                if (Character.isUpperCase(chars[i])) {
                    b++;
                } else {
                    e++;
                }
                //判断是否是空格
            } else if (Character.isSpaceChar(chars[i])) {
                c++;
            } else {
                d++;
            }
        }
        System.out.println("数字个数:" + a);
        System.out.println("大写字母个数:" + b);
        System.out.println("小写字母个数:" + e);
        System.out.println("空格个数:" + c);
        System.out.println("特殊个数:" + d);
    }

    /**
     * 实现3+33+333+3333+...位数由循环次数决定
     */
    @Test
    public void testAddCount() {
        int a = 3;
        int b = a;
        int sum = a;
        for (int i = 0; i < 2; i++) {
            a = (a * 10) + b;
            System.out.println(a);
            sum = sum + a;
        }
        System.out.println(sum);
    }

    /**
     * 二分查找
     */
    @Test
    public void testBinarySerach() {
        int[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 34, 56, 78, 90};
        int low = 0;
        int hight = ints.length - 1;
        while (low <= hight) {
            int mid = (low + hight) / 2;
            if (ints[mid] == 12) {
                System.out.println(ints[mid]);
                break;
            } else if (ints[mid] < 12) {
                low = mid + 1;
            } else {
                hight = mid - 1;
            }
        }

    }

 

/**
* 求某两个数的最大公因数和最小公倍数
*/
@Test
public void testMaxYinshu() {
int a = 36;
int b = 16;
int x = a;
int y = b;
if (x < y) {
int k = y;
y = x;
x = k;
}
while (y != 0) {
if (x == y) {
break;
}
int k = y;
y = x % y;
x = k;
}
System.out.println(a + "和" + b + "最大公因数是:" + x);
System.out.println(a + "和" + b + "最小公倍数是:" + a * b / x);
}

@Test
public void testCountString() {
String s = "HHhh hhhh 123&&";
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
//判断是否是数字
if (Character.isDigit(chars[i])) {
a++;
//判断是否是字母
} else if (Character.isLetter(chars[i])) {
//判断是否是大写字母
if (Character.isUpperCase(chars[i])) {
b++;
} else {
e++;
}
//判断是否是空格
} else if (Character.isSpaceChar(chars[i])) {
c++;
} else {
d++;
}
}
System.out.println("数字个数:" + a);
System.out.println("大写字母个数:" + b);
System.out.println("小写字母个数:" + e);
System.out.println("空格个数:" + c);
System.out.println("特殊个数:" + d);
}

/**
* 实现3+33+333+3333+...位数由循环次数决定
*/
@Test
public void testAddCount() {
int a = 3;
int b = a;
int sum = a;
for (int i = 0; i < 2; i++) {
a = (a * 10) + b;
System.out.println(a);
sum = sum + a;
}
System.out.println(sum);
}

/**
* 二分查找
*/
@Test
public void testBinarySerach() {
int[] ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 34, 56, 78, 90};
int low = 0;
int hight = ints.length - 1;
while (low <= hight) {
int mid = (low + hight) / 2;
if (ints[mid] == 12) {
System.out.println(ints[mid]);
break;
} else if (ints[mid] < 12) {
low = mid + 1;
} else {
hight = mid - 1;
}
}

}
posted @ 2020-09-01 17:26  BlackCatFish  阅读(246)  评论(0编辑  收藏  举报