zoukankan      html  css  js  c++  java
  • Java基础题目

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

    程序分析:兔子的规律为数列1,1,2,3,5,8,13,21….

    public class Rabbit {
        public static int f(int n) {
            if (n == 1 || n == 2) {
                return 1;
            } else {
                return f(n - 1) + f(n - 2);
            }
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            System.out.println(f(n));
        }
    }
    View Code

    题目:判断101-200之间有多少个素数,并输出所有素数。

    public class Primer {
        public static boolean isPrimer(int n){
            //判断一个数能否被小于sqrt(n)的数整除
            int sqrt = (int)Math.sqrt(n);
            for (int i = 2; i <= sqrt; i++) {
                if(n % i == 0) {
                    return false;
                }
            }
            return true;
        }
        public static void main(String[] args) {
            int count=0;
            for (int i=101;i<=200;i++){
                if(isPrimer(i)==true){
                    count++;
                    System.out.print(i);
                    System.out.print(" ");
                }else {
                    continue;
                }
            }
            System.out.print(count);
    
        }
    }
    View Code

    题目:打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个”水仙花数”,因为153=1的三次方+5的三次方+3的三次方。

    public class Flower {
        public static boolean isFlower(int n) {
            int i = n / 100;
            int j = n / 10 - i * 10;
            int k = n % 10;
            if (n == Math.pow(i, 3) + Math.pow(j, 3) + Math.pow(k, 3)) {
                return true;
            }
            return false;
        }
    
        public static void main(String[] args) {
            for (int i = 100; i <= 999; i++) {
                if (isFlower(i) == true) {
                    System.out.print(i);
                    System.out.print(" ");
                } else {
                    continue;
                }
            }
        }
    }
    View Code

    题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

    public class DividePrimer {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            System.out.print(n + "=");
            for (int i = 2; i <= n; i++) {
                while (n % i == 0 && n != i) {
                    n = n / i;
                    System.out.print(i + "*");
                }
                if (n == i) {
                    System.out.print(i);
                    break;
                }
    
            }
    
        }
    }
    View Code

    题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

    程序分析:(a>b)?a:b这是条件运算符的基本例子。

    public class Score {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int score = scanner.nextInt();
            char level = (score >= 90) ? 'A' : (score < 60) ? 'C' : 'B';
            System.out.println(level);
        }
    }
    View Code

    题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

    程序分析:利用辗除法。

    public class BigDivider {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int m = scanner.nextInt();
            int n = scanner.nextInt();
            if (m < n) {
                int temp = m;
                m = n;
                n = temp;
            }
            int max = m % n;
            int min = m * n;
            while (max != 0) {
                m = n;
                n = max;
                max = m % n;
            }
            max = n;
            min = min / max;
            System.out.println(max);
            System.out.println(min);
        }
    }
    View Code

    题目:输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。

    public class CharClass {
        public static void main(String[] args) {
            int digit = 0;
            int alpha = 0;
            int space = 0;
            int other = 0;
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] >= '0' && chars[i] <= '9') {//字符是数字
                    digit++;
                } else if ((chars[i] >= 'A' && chars[i] <= 'Z') || (chars[i] >= 'a' && chars[i] <= 'z')) {
                    alpha++;
                } else if (chars[i] == ' ') {
                    space++;
                } else {
                    other++;
                }
            }
            System.out.println("字母:" + alpha);
            System.out.println("数字:" + digit);
            System.out.println("空格:" + space);
            System.out.println("其他:" + other);
        }
    }
    View Code

    题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。输出结果的形式如:2+22+222=246;

    public class Digital {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入参与运算的数1-9:");
            int n = input.nextInt();
            System.out.println("请输入运算的次数:");
            int num = input.nextInt();
            int nums = 0;
            int sum = 0;
            for (int i = 0; i < num; i++) {
                nums += Math.pow(10, i) * n;
                sum += nums;
                System.out.print(nums);
                if (i == num - 1) {
                    System.out.print("");
                } else {
                    System.out.print("+");
                }
            }
            System.out.print("=" + sum);
    
        }
    
    
    }
    View Code

    题目:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3.编程找出1000以内的所有完数。

    public class PreferDigit {
        public static void main(String[] args) {
            System.out.print("1000以内的所有完数为:");
            for (int i = 1; i <= 1000; i++) {
                int sum = 0;
                for (int j = 1; j < i; j++) {
                    if (i % j == 0) {
                        sum += j;
                    }
                }
                if (i == sum) {
                    System.out.print(i);
                    System.out.print(" ");
                }
            }
        }
    }
    View Code

    题目:输出一行字符串,中间用空格隔开,统计最后一个单词的长度(好像是这么描述的)

    public class WordLength {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            String s = "";
            while (input.hasNextLine()) {
                s = input.nextLine();
                System.out.println(s.length() - 1 - s.lastIndexOf(" "));
            }
        }
    }
    View Code
  • 相关阅读:
    Python第二弹--------类和对象
    Python第一弹--------初步了解Python
    Java标记接口
    CentOS7下的YUM源服务器搭建详解,过程写的很详细(转)
    CentOS7.0安装Nginx 1.10.0
    QT中C++与Html端通信例子
    QT基础:QMainWindow学习小结
    QT基础:QT 定时器学习
    QT3D场景快速绘制入门学习
    QT编译错误:cannot find file: *.pro
  • 原文地址:https://www.cnblogs.com/shun998/p/12377170.html
Copyright © 2011-2022 走看看