zoukankan      html  css  js  c++  java
  • Java练习十题集(一)

    Java练习十题集(一):

    1. 将26个字母按形如手机键盘的对应形式转换成0~9数字。

    2.写两个线程,其中一个线程打印1-52,另一个打印A-Z,打印顺序为12A34B56C....5152Z。

    3.使用循坏输出九九乘法表。输出如下结果:

    1×1=1
    2×1=2,2×2=4
    3×1=3,3×2=6,3×3=9
    ......
    9×1=9,9×2=18,9×3=27,… 9×9=81

    4.Java求最大公约数和最小公倍数。

    5.2~200之间有多少素数,分别是多少。

    6.分别编写出计算Hermite多项式Hn (x) 值的递推和递归函数。Hn (x) 定义为
    H0(x)=1 (n=0)
    H1(x)=2x (n=1)
    Hn(x)=2xHn-1(x) -2(n- 1)Hn-2(x) (n>1)

    7.写出计算Ackemam函数Ack (m, n)的递归计算函数。对于m≥0, n≥0, Ack(m,n)定义为:
    Ack(0,n)= n+1
    Ack(m,0)= Ack(m-1,1)
    Ack(m,n)= Ack(m- 1,Ack(m,n-1)) 

    8.编写函数,求1-3+5-7+...+n。提示:函数应该有一个参数,通过这个参数得到n的值。

    9.小萌的副本生涯

    【题目描述】
    在主城站街很久之后,小萌决定不能就这样的浪费时间虚度青春,他打算去打副本。
    这次的副本只有一个BOSS,而且BOSS是不需要击杀的,只需要和它比智力…….
    BOSS会列出一正整数的序列,由小萌先开始,然后两个人轮流从序列的任意一端取数,取得的数
    累加到积分里,当所有数都取完,游戏结束。
    假设小萌和BOSS都很聪明,两个人取数的方法都是最优策略,问最后两人得分各是多少。
    输入
    第一行:一个正整数N(2 ≤ N ≤ 100),表示序列中正整数的个数。
    第二行至末尾:用空格隔开的N个正整数(1 ≤ a[i] ≤ 200)
    输出
    只有一行,用空格隔开的两个数,小萌的得分和BOSS的得分。
    样例输入
    6
    4 7 2 9 5 2
    样例输出
    11 18

    10.假设你能重返过去,现在让你回到2015年,你能选择一支股票进行投资,你拥有这支股票未来n天的价格走势图,为了躲避证监会
    的监控,你只有一次买入卖出机会。现在要求实现一个程序计算哪天买入哪天卖出能获得最大收益。

    输入
    第一行为天数n
    接下来n行 为数组的n个整数元素,代表第n天该股票的价格
    输出
    输出为b,s #代表第b天买入,第s天卖出
    天数从0开始
    如果没有适合的买入卖出输出-1,-1
    同样收益时越晚买入越早卖出更符合需要
    样例输入
    5
    2
    1
    4
    5
    3
    样例输出
    1, 3

    1. 将26个字母按形如手机键盘的对应形式转换成0~9数字。

    import java.util.HashMap;
    import java.util.Map;
    
    public class Test {
    
        public static void main(String[] args) {
            Map<String,Integer> map = getMapKV();
            String str = "Hello World";
            System.out.println(str);
            str = str.toUpperCase();
            for(int i = 0; i < str.length(); i++) {
                System.out.print(map.get(str.charAt(i)+""));
            }
            System.out.println( );
        }
    
        static Map<String,Integer> getMapKV(){
            Map<String,Integer> map = new HashMap<>();
            map.put(" ", 0);
            for(int i = 0; i < 26; i++){
                String key = "" + (char)(i+'A');
                if(i<3){
                    map.put(key,2);
                }else if(i < 6){
                    map.put(key,3);
                }else if(i < 9){
                    map.put(key,4);
                }else if(i < 12){
                    map.put(key,5);
                }else if(i < 15){
                    map.put(key,6);
                }else if(i < 18){
                    map.put(key,7);
                }else if(i < 21){
                    map.put(key,8);
                }else if(i < 26){
                    map.put(key,9);
                }
            }
            return map;
        }
    
    }
    

     

    2.写两个线程,其中一个线程打印1-52,另一个打印A-Z,打印顺序为12A34B56C....5152Z。

    public class Test {
    
        public static void main(String[] args) {
            threadTest();
        }
        static void threadTest(){
            Object obj = new Object();
            Digit digit = new Digit(obj);
            Letter letter = new Letter(obj);
            Thread th1 = new Thread(digit);
            Thread th2 = new Thread(letter);
            th1.start();//数字的线程先运行,数字先执行
            th2.start();
        }
    }
    class Digit implements Runnable{
        private Object obj;//声明一个类的引用
        public Digit(Object obj){
            this.obj = obj;	//通过构造器将共享的资源-->对象传进来
        }
        @Override
        public void run() {
            synchronized(obj){//给共享资源上锁
                for(int i = 1;i < 53;i++ ){
                    System.out.print(i);
                    if(i % 2 == 0){
                        obj.notify();//唤醒其他线程
                        try {
                            obj.wait();//等待并释放锁
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    class Letter implements Runnable{    
        private Object obj;
        public Letter(Object obj){
            this.obj = obj;
        }
        @Override
        public void run() {
            synchronized(obj){
                for(int i = 0;i < 26;i++ ){
                    System.out.print((char)(i+'A'));
                    obj.notify();//唤醒其他线程
                    try {
                        obj.wait();//释放锁等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

     

    3.使用循坏输出九九乘法表。输出如下结果:

    1×1=1
    2×1=2,2×2=4
    3×1=3,3×2=6,3×3=9
    ......
    9×1=9,9×2=18,9×3=27,… 9×9=81

    public class Test {
    
        public static void main(String[] args) {
            multiplicationTableTest();
            System.out.println("----------------------");
            multiplicationTableRecursionTest(9);
        }
    
        static void multiplicationTableTest() {
            for (int i = 1; i <= 9; i++) {
                for (int j = 1; j <= i; j++) {
                    if (i == j) {
                        System.out.print(j + "*" + i + "=" + i * j + " ");
                    } else {
                        System.out.print(j + "*" + i + "=" + i * j + ",");
                    }
                }
                System.out.println();
            }
        }
    
        /**
         * 使用递归方法输出99乘法表
         */
        public static void multiplicationTableRecursionTest(int i) {
            if (i == 1) {
                System.out.println("1*1=1");
            } else {
                multiplicationTableRecursionTest(i - 1);
                for (int j = 1; j <= i; j++) {
                    if (i == j) {
                        System.out.print(j + "*" + i + "=" + i * j + " ");
                    } else {
                        System.out.print(j + "*" + i + "=" + i * j + ",");
                    }
                }
                System.out.println(" ");
            }
        }
    }
    

      

    4.Java求最大公约数和最小公倍数。

    public class Test {
    
        public static void main(String[] args) {
    
            int gcd = greatestCommonDivisor(120,90);
            System.out.println(gcd);
            int lcm = leastCommonMultiple(120,90);
            System.out.println(lcm);
    
        }
    
        static int greatestCommonDivisor(int m, int n){
            //辗转相除
            int result = 0;
            int max = m > n ? m : n;
            int min = m > n ? n : m;
            if(max % min == 0){
                result = min;
            }
            while(max % min > 0){
                max = max % min;
                if(max < min) {  //交换再辗转相除
                    max = max + min;
                    min = max - min;
                    max = max - min;
                }
                if(max % min == 0){
                    result =  min;
                }
            }
            return result;
        }
    
        static int leastCommonMultiple(int m, int n) {
            int ret = m * n / greatestCommonDivisor(m, n);
            return ret;
        }
    
    }
    

      

    5.2~200之间有多少素数,分别是多少。

    素数又称质数,有无限个。质数(素数)定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。

    public class Test {
    
        public static void main(String[] args) {
    
            primeTest();
    
        }
    
        static void primeTest(){
            System.out.println("1~200内的素数有:");
            int count = 0;
            for(int i = 1; i < 200; i++){
                if(isPrime(i)){
                    System.out.print(i+",");
                    count++;
                }
            }
            System.out.println();
            System.out.println("共有"+count+"个素数。");
        }
    
        static boolean isPrime(int n ){
    
            boolean bln = true;
            if(n < 2 ){
                return false;
            }
            for(int i = 2; i < n/2; i++) {
                if(n%i == 0 ){
                    bln = false;
                    return bln;
                }
            }
            return bln;
        }
    
    }
    

      

    6.分别编写出计算Hermite多项式Hn (x) 值的递推和递归函数。Hn (x) 定义为
    H0(x)=1 (n=0)
    H1(x)=2x (n=1)
    Hn(x)=2xHn-1(x) -2(n- 1)Hn-2(x) (n>1)

    public class Test {
    
        public static void main(String[] args) {
            testRecH();
        }
    
        static void testRecH(){
            int x = recH(12);
            System.out.println(x);
        }
    
        static int recH(int n){
            if(n == 0){
                return 1;
            }else if(n == 1){
                return 2*n;
            }else {
                return 2*recH(n-1)-2*(n-1)*recH(n-2);
            }
        }
    }
    

      

    7.写出计算Ackemam函数Ack (m, n)的递归计算函数。对于m≥0, n≥0, Ack(m,n)定义为:
    Ack(0,n)= n+1
    Ack(m,0)= Ack(m-1,1)
    Ack(m,n)= Ack(m- 1,Ack(m,n-1)) 

    public class TestAck {
    
        public static void main(String[] args) {
            testRrecAck();
        }
    
        static void testRrecAck(){
            long x = recAck(3,5);
            System.out.println(x);
        }
    
        static long recAck(long m, long n){
            if(m == 0){
                return n+1;
            } if(n == 0){
                return recAck(m-1,1);
            }else{
                return recAck(m-1,recAck(m,n-1));
            }
        }
    }
    

      

    8.编写函数,求1-3+5-7+...+n。提示:函数应该有一个参数,通过这个参数得到n的值。

    public class Test {
    
        public static void main(String[] args) {
            testSumFunc(12);
        }
    
        static void testSumFunc(int n) {
            for(int i = 1; i <= n; i++) {
                if(i%2 == 0) {
                    int x = (-1)*(2*i-1);
                    System.out.print(x);
                }else {
                    if(i != 1) {
                        System.out.print("+"+(2*i-1));
                    } else {
                        System.out.print(i);
                    }
                }
            }
            System.out.print("=" + sumFunc(n));
            System.out.println( );
        }
    
        static int sumFunc(int m) {
            int sum = 0;
            for(int i = 1; i <= m; i++) {
                if(i%2 == 0) {
                    sum += (-1)*(2*i-1);
                }else {
                    sum+= (2*i-1);
                }
            }
            return sum;
        }
    
        static int sumFunc2(int m) {
           if (m % 2 == 0) {
                return -m;
           } else {
               return m;
           }
        }
    }
    

      

    9. 小萌的副本生涯

    【题目描述】
    在主城站街很久之后,小萌决定不能就这样的浪费时间虚度青春,他打算去打副本。
    这次的副本只有一个BOSS,而且BOSS是不需要击杀的,只需要和它比智力…….
    BOSS会列出一正整数的序列,由小萌先开始,然后两个人轮流从序列的任意一端取数,取得的数
    累加到积分里,当所有数都取完,游戏结束。
    假设小萌和BOSS都很聪明,两个人取数的方法都是最优策略,问最后两人得分各是多少。
    输入
    第一行:一个正整数N(2 ≤ N ≤ 100),表示序列中正整数的个数。
    第二行至末尾:用空格隔开的N个正整数(1 ≤ a[i] ≤ 200)
    输出
    只有一行,用空格隔开的两个数,小萌的得分和BOSS的得分。
    样例输入
    6
    4 7 2 9 5 2
    样例输出
    11 18

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args) {
            Test();
        }
    
        static void Test() {
            System.out.print("第一行:一个正整数N(2 ≤ N ≤ 100),表示序列中正整数的个数。");
            Scanner in = new Scanner(System.in);
            int x = in.nextInt();
            System.out.println(x);
            in = new Scanner(System.in);
            String s = in.nextLine();
            System.out.println(s);
            String[] str = s.split("\s+");
            System.out.println("数组长度:"+str.length);
            int[] arr = new int[str.length];
            for (int i = 0; i < arr.length; i++) {
                if(!"".equals(str[i].trim())){
                    arr[i] = Integer.parseInt(str[i].trim());
                }
            }
            System.out.println(Arrays.toString(arr));
            play(arr);
        }
    
        static void play(int[] arr){
            int cute = 0, boss = 0;
            int i = 0, k = arr.length-1;
            int count = 0;
            while (i <= k){
                if(arr[i] >= arr[k]){
                    if(count % 2 == 0){
                        cute += arr[i];
                    }else{
                        boss += arr[i];
                    }
                    i++;
                } else {
                    if(count % 2 == 0){
                        cute += arr[k];
                    }else{
                        boss += arr[k];
                    }
                    k--;
                }
                count++;
            }
            System.out.println(cute+" "+boss);
        }
    
    }
    

      

    10.假设你能重返过去,现在让你回到2015年,你能选择一支股票进行投资,你拥有这支股票未来n天的价格走势图,为了躲避证监会
    的监控,你只有一次买入卖出机会。现在要求实现一个程序计算哪天买入哪天卖出能获得最大收益。

    输入
    第一行为天数n
    接下来n行 为数组的n个整数元素,代表第n天该股票的价格
    输出
    输出为b,s #代表第b天买入,第s天卖出
    天数从0开始
    如果没有适合的买入卖出输出-1,-1
    同样收益时越晚买入越早卖出更符合需要
    样例输入
    5
    2
    1
    4
    5
    3
    样例输出
    1, 3

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args) {
            Test();
        }
    
        static void Test(){
            System.out.print("please enter N : ");
            Scanner in  = new Scanner(System.in);
            System.out.println();
            int n = in.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++){
                in  = new Scanner(System.in);
                arr[i] =  in.nextInt();
            }
    //        System.out.println(Arrays.toString(arr));
            int max = indexExtremeValueArr(arr,arr.length,true);
            int min = indexExtremeValueArr(arr,max,false);
            if(max == 0 && min == 0){
                System.out.println("-1, -1");
            }else {
                System.out.println(min+" , "+max);
            }
    
        }
    
        /**
         * 0~ x 中最大或者最小数的位置
         * @param arr
         * @param x
         * @param flag true取最大值,false取最小值(数组前x个数中)
         * @return
         */
        static int indexExtremeValueArr(int[] arr,int x, boolean flag){
            int index = 0;
            x = x < arr.length ? x : arr.length;
            if(arr.length == 1) {
                return 0;
            }
            if(flag){   //或取极大值的位置
                for(int i = 1; i < x; i++) {
                    if(arr[i] > arr[i-1]){
                        index = i;
                    }
                }
            } else {    //或取极小值的位置
                for(int i = 1; i < x; i++) {
                    if(arr[i] <= arr[i-1]){
                        index = i;
                    }
                }
            }
            return index;
        }
    }
    

      

  • 相关阅读:
    算法与数据结构 (四) 排序 一 交换类排序
    算法与数据结构 (三) 二叉树的简单应用 二叉查找树,二叉堆排序
    算法与数据结构 (二) 二叉树的简单实现,非递归的前序遍历 中序遍历 后序遍历
    算法与数据结构 (一) 链表,栈,队列的简单实现
    服务器端的redis和MySQL的远程连接的简单解决方案
    记一次自定义监听器使用spring 管理的bean的问题
    基于java开发的RBAC模型权限管理系统
    2019 本科java开发春招面经(实习)
    记一次Bootstrap框架下 使用Ajax失效的问题
    [转]在static代码块或static变量的初始化过程中使用ServiceManager提供的api的陷阱
  • 原文地址:https://www.cnblogs.com/loytime/p/10101939.html
Copyright © 2011-2022 走看看