适合Java初学者的简单编程算法题有哪些

发布时间:2021-10-11 10:23:00 作者:iii
来源:亿速云 阅读:136

本篇内容主要讲解“有哪些适合Java初学者的简单编程算法题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“有哪些适合Java初学者的简单编程算法题”吧!

适合Java初学者的简单编程算法题有哪些

一、前言

数学离程序员有多近?

ifelse也好、for循环也罢,代码可以说就是对数学逻辑的具体实现。所以敲代码的程序员几乎就离不开数学,难易不同而已。

那数学不好就写不了代码吗?????不,一样可以写代码,可以写出更多的CRUD出来。那你不要总觉得是产品需求简单所以你的实现过程才变成了增删改查,往往也是因为你还不具备可扩展、易维护、高性能的代码实现方案落地能力,才使得你小小年纪写出了更多的CRUD

与一锥子买卖的小作坊相比,大厂和超级大厂更会注重数学能力。

适合Java初学者的简单编程算法题有哪些

2004年,在硅谷的交通动脉 101 公路上突然出现一块巨大的广告牌,上面是一道数学题: {e 的连续数字中最先出现的 10 位质数}.com。

广告:这里的 e 是数学常数,自然对数的底数,无限不循环小数。这道题的意思就是,找出 e 中最先出现的 10 位质数,然后可以得出一个网址。进入这个网址会看到 Google 为你出的第二道数学题,成功解锁这步 Google 会告诉你,我们或许是”志同道合“的人,你可以将简历发到这个邮箱,我们一起做点改变世界的事情。

计算 e 值可以通过泰勒公式推导出来:e^x≈1 + x + x^2/2! + x^3/3! +……+ x^n/n! (1) 推导计算过程还包括埃拉托色尼筛选法(the Sieve of Eratosthenes)线性筛选法的使用。感兴趣的小伙伴可以用代码实现下。

二、编程练习题

1. 斐波那契数列

@Test
public void test_Fibonacci() {
    int month = 15;  // 15个月
    long f1 = 1L, f2 = 1L;
    long f;
    for (int i = 3; i < month; i++) {
        f = f2;
        f2 = f1 + f2;
        f1 = f;
        System.out.println("第" + i + "个月的兔子对数: " + f2);
    }
}

2. 判断素数

@Test
public void test_Prime() {
    int count = 0;
    for (int i = 101; i < 200; i++) {
        boolean b = true;// 默认此数就素数
        for (int j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                b = false; // 此数不是素数
                break;
            }
        }
        if (b) {
            count++;
            System.out.print(i + " ");
        }
    }
    System.out.println("\n素数的个数:" + count);
}

3. 水仙花数

@Test
public void test_narcissus() {
    for (int num = 101; num < 1000; num++) {
        int bbb = num / 100;
        int bb = (num % 100) / 10;
        int b = (num % 100) % 10;
        if ((bbb * bbb * bbb + bb * bb * bb + b * b * b) == num) {
            System.out.println(num);
        }
    }
}

4. 分解质因数

@Test
public void test_ZhiYinShu() {
    f(200);
}
int k = 2;
public void f(int n) {
    while (k <= n) {
        if (k == n) {
            System.out.println(n);
            break;
        } else if (n > k && n % k == 
            System.out.print(k + "*")
            n = n / k;
            f(n);
            break;
        } else if (n > k && n % k != 
            k++;
            f(n);
            break;
        }
    }
}

5. 杨辉三角

 @Test
 public void test_YangHuiSanJiao(){
     int[][] a = new int[10][10];
     for (int i = 0; i < 10; i++) {
         a[i][i] = 1;
         a[i][0] = 1;
     }
     for (int i = 2; i < 10; i++) {
         for (int j = 1; j < i; j++) {
             a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
         }
     }
     for (int i = 0; i < 10; i++) {
         for (int k = 0; k < 2 * (10 - i) - 1; k++) {
             System.out.print(" ");
         }
         for (int j = 0; j <= i; j++) {
             System.out.print(a[i][j] + "   ");
         }
         System.out.println();
     }
 }

6. 求最大公约数与最小公倍数

@Test
public void test_Prime() {
    int a = 10, b = 24;
    int m = division(a, b);
    int n = a * b / m;
    System.out.println("最大公约数: " + m);
    System.out.println("最小公倍数: " + n);
}
public int division(int x, int y) {
    int t;
    if (x < y) {
        t = x;
        x = y;
        y = t;
    }
    while (y != 0) {
        if (x == y)
            return 1;
        else {
            int k = x % y;
            x = y;
            y = k;
        }
    }
    return x;
}

7. 完全平方数

@Test
public void test_PerfectSquare() {
    for (long l = 1L; l < 100000; l++) {
        if (Math.sqrt((l + 100)) % 1 == 0) {
            if (Math.sqrt((l + 268)) % 1 == 0) {
                System.out.println(l + "加100是一个完全平方数,再加168又是一个完全平方数");
            }
        }
    }
}

8. 求主对角线之和

@Test
public void test_Sum() {
    Scanner s = new Scanner(System.in);
    int[][] a = new int[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            a[i][j] = s.nextInt();
        }
    }
    System.out.println("输入的3 * 3 矩阵是:");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print(a[i][j] + " ");
        }
        System.out.println();
    }
    int sum = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (i == j) {
                sum += a[i][j];
            }
        }
    }
    System.out.println("对角线和是 " + sum);
}

9. 完数求解

@Test
public void test_solution() {
    System.out.println("1到1000的完数有: ");
    for (int i = 1; i < 1000; i++) {
        int t = 0;
        for (int j = 1; j <= i / 2; j++) {
            if (i % j == 0) {
                t = t + j;
            }
        }
        if (t == i) {
            System.out.print(i + " ");
        }
    }
}

10. 求s=a+aa+aaa+aaaa+aa...a的值

@Test
public void test_asum() {
    long a = 2, b = 0;
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    int i = 0;
    long sum = 0;
    while (i < n) {
        b = b + a;
        sum = sum + b;
        a = a * 10;
        ++i;
    }
    System.out.println("input number: " + n);
    System.out.println(sum);
}

11. 无重复三位数

@Test
public void test_AC() {
    int count = 0;
    for (int x = 1; x < 5; x++) {
        for (int y = 1; y < 5; y++) {
            for (int z = 1; z < 5; z++) {
                if (x != y && y != z && x != z) {
                    count++;
                    System.out.print(x * 100 + y * 10 + z + "   ");
                    if (count % 4 == 0) {
                        System.out.println();
                    }
                }
            }
        }
    }
    System.out.println("共有" + count + "个三位数");
}

12. 从小到大输出数列

public class SmallToBig {
	public static void main(String[] args) {
		SmallToBig fnc = new SmallToBig();
		int a, b, c;

		System.out.println("Input 3 numbers:");
		a = fnc.input();
		b = fnc.input();
		c = fnc.input();

		if (a > b) {
			int t = a;
			a = b;
			b = t;
		}

		if (a > c) {
			int t = a;
			a = c;
			c = t;
		}

		if (b > c) {
			int t = b;
			b = c;
			c = t;
		}
		System.out.println(a + " " + b + " " + c);
	}

	public int input() {
		int value = 0;
		Scanner s = new Scanner(System.in);
		value = s.nextInt();
		return value;
	}

	public void compare(int x, int y) {// 此方法没用
		if (x > y) {
			int t = x;
			x = y;
			y = t;
		}
	}
}

13. 猴子吃桃问题

public class Monkey {
	
	public static void main(String[] args) {
		int lastdayNum = 1;
		for (int i = 2; i <= 10; i++) {
			lastdayNum = (lastdayNum + 1) * 2;
		}
		System.out.println("猴子第一天摘了 " + lastdayNum + " 个桃子");
	}
}

14. 乒乓球比赛

public class Compete {
	
	static char[] m = { 'a', 'b', 'c' };
	static char[] n = { 'x', 'y', 'z' };

	public static void main(String[] args) {
		for (int i = 0; i < m.length; i++) {
			for (int j = 0; j < n.length; j++) {
				if (m[i] == 'a' && n[j] == 'x') {
					continue;
				} else if (m[i] == 'a' && n[j] == 'y') {
					continue;
				} else if ((m[i] == 'c' && n[j] == 'x')
						|| (m[i] == 'c' && n[j] == 'z')) {
					continue;
				} else if ((m[i] == 'b' && n[j] == 'z')
						|| (m[i] == 'b' && n[j] == 'y')) {
					continue;
				} else
					System.out.println(m[i] + " vs " + n[j]);
			}
		}
	}
}

15. 求分数之和

public class FenShu {
	public static void main(String[] args) {
		int x = 2, y = 1, t;
		double sum = 0;

		DecimalFormat df = new DecimalFormat("#0.0000");

		for (int i = 1; i <= 20; i++) {
			sum += (double) x / y;
			t = y;
			y = x;
			x = y + t;
			System.out.println("第 " + i + " 次相加,和是 " + df.format(sum));
		}
	}
}

16. 求阶乘的和

public class JieCheng {
	static long sum = 0;
	static long fac = 0;

	public static void main(String[] args) {
		long sum = 0;
		long fac = 1;
		for (int i = 1; i <= 10; i++) {
			fac = fac * i;
			sum += fac;
		}
		System.out.println(sum);
	}
}

17. 回文判断

public class HuiWen {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.print("请输入一个正整数:");
		long a = s.nextLong();
		String ss = Long.toString(a);
		char[] ch = ss.toCharArray();
		boolean is = true;
		int j = ch.length;
		for (int i = 0; i < j / 2; i++) {
			if (ch[i] != ch[j - i - 1]) {
				is = false;
			}
		}
		if (is == true) {
			System.out.println("这是一个回文数");
		} else {
			System.out.println("这不是一个回文数");
		}
	}
}

18. 按顺序输出数列

public class ShunXu {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int a = s.nextInt();
		int b = s.nextInt();
		int c = s.nextInt();

		if (a < b) {
			int t = a;
			a = b;
			b = t;
		}

		if (a < c) {
			int t = a;
			a = c;
			c = t;
		}

		if (b < c) {
			int t = b;
			b = c;
			c = t;
		}

		System.out.println("从大到小的顺序输出:");
		System.out.println(a + " " + b + " " + c);
	}
}

19. 位置替换

public class TiHuan {
	
	static final int N = 8;

	public static void main(String[] args) {

		int[] a = new int[N];
		Scanner s = new Scanner(System.in);
		int index1 = 0, index2 = 0;

		System.out.println("please input numbers");
		for (int i = 0; i < N; i++) {
			a[i] = s.nextInt();
			System.out.print(a[i] + " ");
		}

		int max = a[0], min = a[0];
		for (int i = 0; i < a.length; i++) {
			if (a[i] > max) {
				max = a[i];
				index1 = i;
			}
			if (a[i] < min) {
				min = a[i];
				index2 = i;
			}
		}

		if (index1 != 0) {
			int temp = a[0];
			a[0] = a[index1];
			a[index1] = temp;
		}

		if (index2 != a.length - 1) {
			int temp = a[a.length - 1];
			a[a.length - 1] = a[index2];
			a[index2] = temp;
		}
		System.out.println("after swop:");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
	}
}

20. 1的个数

long startTime = System.currentTimeMillis();
int num = 10000000, saveNum = 1, countNum = 0, lastNum = 0;
int copyNum = num;
while (num != 0) {
    lastNum = num % 10;
    num /= 10;
    if (lastNum == 0) {
        // 如果是0那么正好是少了一次所以num不加1了
        countNum += num * saveNum;
    } else if (lastNum == 1) {
        // 如果是1说明当前数内少了一次所以num不加1,而且当前1所在位置
        // 有1的个数,就是去除当前1最高位,剩下位数,的个数。
        countNum += num * saveNum + copyNum % saveNum + 1;
    } else {
        // 如果非1非0.直接用公式计算
        // abcd...=(abc+1)*1+(ab+1)*10+(a+1)*100+(1)*1000...
        countNum += (num + 1) * saveNum;
    }
    saveNum *= 10;
}
System.out.println("1的个数:" + countNum);
System.out.println("计算耗时:" + (System.currentTimeMillis() - startTime) + "毫秒");

到此,相信大家对“有哪些适合Java初学者的简单编程算法题”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Django----做一个简单网页的教程(适合初学者)
  2. highcharts 小实例 很适合初学者

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:如何理解mysql权限参考

下一篇:Linux命令和命令行的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》