1,编写程序数一下 1到100的所有整数中出现多少个数字9?

思考:

  • 能不能转化为字符串String与‘9’比较?
  • 怎样处理好每个位?

方法1:使用字符串转换

public class Test {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            String numStr = String.valueOf(i);
            for (int j = 0; j < numStr.length(); j++) {
                if(numStr.charAt(j) == '9'){
                    count++;
                }
            }
        }
        System.out.println(count);
    }

知识点:

  1. String.valueOf()转换为字符串的方法
  2. numStr.length()求字符串长度的方法
  3. numStr.charAt(j)获取字符串中指定索引位置的字符

字符串的索引和数组一样,起始为0,结束为lenth() - 1

String str = "Hello";
System.out.println(str.length());  // 输出:5
        
System.out.println(str.charAt(0));  // 输出:H
System.out.println(str.charAt(1));  // 输出:e
System.out.println(str.charAt(2));  // 输出:l
System.out.println(str.charAt(3));  // 输出:l
System.out.println(str.charAt(4));  // 输出:o

方法2:数学方法

public class Test {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            int num = i;
            while (num > 0){
                if(num % 10 == 9){
                    count++;
                }
                num /= 10;
            }
        }
        System.out.println(count);
    }

注意:在循环中不要改变i的值

我在想的时候总想一位数和两位数的分类讨论,但其实可以一起解决

2,给定两个数,求这两个数的最大公约数

例如: 输入:20 40 输出:20

约数,也叫因数,是指能够整除一个整数的数。简单说:如果 a ÷ b = c 且没有余数,那么 b 就是 a 的约数。

最大公约数(GCD,Greatest Common Divisor)

方法1:枚举法

如果b是最小值,就不可能有比b大的数作为a,b的最大公约数,比b大的数除b等于小数,所以用<=b的数依次去整除a,b,如果都能整除就是最大公约数

int gcd = 1;
int a = 40;
int b = 20;
for (int i = Math.min(a,b); i >= 1; i--) {
    if((a % i == 0)&&(b % i == 0)) {
        gcd = i;
        break;
    }
}
System.out.println(gcd);//20

方法2:辗转相除法(欧几里得算法)

int a = 40;
int b = 20;
while (b != 0) {
    int temp = a % b;
    a = b;
    b = temp;
}
System.out.println(a);//20

A(被除数)=B(除数)*q(商)+R(余数)

被除数和除数的最大公约数 = 除数和余数的最大公约数

(A,B) = (B,R)  (B,0) = B

因此我们可以使用递归的方法

public static int gcd(int a, int b) {
        if(b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

public static void main(String[] args) {
        int a = 40;
        int b = 20;
        System.out.println(gcd(a,b));//20
    }

(A,B) = (B,R)成立的数学原理

假设u是A和B共同的公因数,所以

A = a*u   B = b*u   (a,b是A,B的因数)

根据A=B*q+R得出R=A-B*q,再把上述公式代入

R=a*u-b*u*q=(a-b*q)*u

因为a,b,q是整数所以(a-b*q)也是整数

所以R=n(整数)*u

所以u也是R的公因数

u是A,B,R的公约数

所以A,B的最大公约数就能转换为B,R的最大公约数

直到余数R=0,最大公约数就是B

扩展:求最小公倍数(LCM)

public class LCM {
    // 最小公倍数 = a * b / 最大公约数
    public static int lcm(int a, int b) {
        return a * b / gcd(a, b);
    }
    
    public static int gcd(int a, int b) {
        while (b != 0) {
            int temp = a % b;
            a = b;
            b = temp;
        }
        return a;
    }
    
    public static void main(String[] args) {
        System.out.println(lcm(12, 18));  // 输出:36
        // 验证:12和18的最小公倍数是36
    }
}

3,计算1-\frac{1}{2}+\frac{1}{3}-\frac{1}{4}+...+\frac{1}{99}-\frac{1}{100}的值

直接用小数计算就行了,不需要通分求精确值,只需要关注符号的问题

double sum = 0.0;
for (int i = 1; i <= 100; i++) {
    if(i % 2 != 0) {
        sum += 1.0 / i;
    }else {
        sum -= 1.0 / i;
    }
}
System.out.println(sum);//0.688172179310195

4,求出0~n之间的所有“水仙花数”并输出。

“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1^3+5^3+3^3,则153是一个“水仙花数“。

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        //求出0~n之间的所有“水仙花数”并输出
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int tmp = i;
            int sum = 0;
            int count = 1;
            while(tmp / 10 != 0) {//判断有几位
                count++;
                tmp /= 10;
            }
            tmp = i;
            while(tmp != 0) {//求每一位然后加
                sum += Math.pow(tmp % 10,count);
                tmp /= 10;
            }
            if(sum == i) {
                System.out.println(i);
            }
        }
    }
}

5,打印x图形

由图可知满足两个条件就打印*

  1. X == Y
  2. X+Y == N-1
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    if((x == y)||(x + y == n-1)) {
                        System.out.print("*");
                    }else {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
        scanner.close();
    }

6,求斐波那契数列的第n项。(迭代实现)

public static int fibonacciSequence(int n) {
        if(n == 1) {
            return 0;
        }else if(n == 2) {
            return 1;
        }
        int prev = 0;
        int curr = 1;
        for (int i = 3; i <= n; i++) {
            int next = prev + curr;
            prev = curr;
            curr = next;
        }
        return curr;
}

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int ret = fibonacciSequence(n);
            System.out.println(ret);
        }
}

更多推荐