java编程50_荐 非常经典的java编程题全集-共50题(1-10)...

非常经典的java编程题

程序1:斐波那契数列问题

题目概述:

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

代码:

public class Programming1 {

//斐波那契数列问题

public static void main(String[] args) {

System.out.println(Fibonacci(1));

System.out.println(Fibonacci(2));

System.out.println(Fibonacci(3));

System.out.println(Fibonacci(4));

}

public static int function(int month) {

/**

* @description:迭代算法求解斐波那契数列

* @param month

* @createDate: 2020/7/4 21:44

* @return: int

*/

int a = 2, b = 2, c, count = 1;

while (count < month) {

c = a;

a = a + b;

b = c;

count++;

}

return a;

}

//考虑使用递归完成计算

public static int Fibonacci(int month) {

/**

* @description:运用递归思想解决斐波那契数列

* @param month

* @createDate: 2020/7/4 21:46

* @return: int

*/

if (month == 1 || month == 2) {

return 2;

} else {

return Fibonacci(month - 1) + Fibonacci(month - 2);

}

}

}

迭代求解思路

ca0a6160a61486dccf7c8964d7d74924.png

程序2:素数问题

题目概述:

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

素数又称为质数,一个大于1的自然数,除了1和它自身之外,不能被其他任何自然数整除的数叫素数。

**判断素数的方法:**用一个数分别去除2到sqrt(这个数),如果能被整除,则表示这个数不是素数,反之是素数。

注: 之所以除到这个数的算数平方根,是因为任何一个数,都只能分解一个小于另一个大于其算数平方根的因子(或链两个因子相同,即是该数的算数平方根)

代码:

public class Programming2 {

public static void main(String[] args) {

int num;

for (int i = 101; i <= 200; i++) {

num = i;

if (isPrime(num)) {

System.out.println(i + "\t");

}

}

}

public static boolean isPrime(int num) {

/**

* @description:判断整数num是否为素数

* @param num

* @createDate: 2020/7/4 22:06

* @return: boolean

*/

boolean res = true;

for (int i = 2; i < Math.sqrt(num); i++) {

if (num == 2) {

res = true;

} else {

if (num % i == 0) {

res = false;

break;

}

}

}

return res;

}

}

95d29df85d8d8bde4b7c0f52bcf6486e.png

程序3:水仙花数

题目概述:

打印所有的水仙花数

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

代码:

public class Programming3 {

public static void main(String[] args) {

for (int i = 101; i < 1000; i++) {

int num = i;

if (isRes(num)) {

System.out.print(i + "\t");

}

}

}

public static boolean isRes(int number) {

/**

* @description:判断一个数是否为水仙花数

* @param number

* @createDate: 2020/7/4 22:12

* @return: boolean

*/

boolean res = false;

int a = number / 100;//取数字的百位

int b = number % 100 / 10;//取数字的十位

int c = number % 10;//取数字的各位

if (number == (a * a * a + b * b * b + c * c * c)) {

res = true;

}

return res;

}

}

程序4:正整数分解质因数

题目概述:

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

程序分析:对n进行分解质因数,应先找到一个最小的质数k,,然后按下述步骤完成:

(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。

(2)如果n > k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一步。

(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

代码:

public class Programming4 {

public static void main(String[] args) {

System.out.println("请输入一个整数:");

Scanner scanner = new Scanner(System.in);

float n = scanner.nextFloat();

System.out.print((int) (n) + " = ");

for (int k = 2; k <= n; ) {

if (n == k) {

System.out.println(k);

break;

} else if (n % k == 0) {

n = n / k;

System.out.print(k + "*");

} else {

k++;

}

}

}

}

程序5:成绩等级判定

题目概述:

敲写一个用来判断成绩的代码,其中90以上为‘A’,80-90之间为‘B’,60-80之间为‘C’,60分以下为‘D’

代码:

public class Programming5 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(function(101));

System.out.println(function(91));

}

public static String function(double score) {

String res = "您输入的成绩有误。";

if (score >= 90 & score <= 100) {

res = "A";

} else if (score >= 80 & score < 90) {

res = "B";

} else if (score >= 60 & score < 80) {

res = "C";

} else if (score < 60 & score >= 0) {

res = "D";

}

return res;

}

}

程序6:求两个数的最大公约数和最小公倍数

题目概述:

输入两个正整数 m, n ,求最大公约数和最小公倍数

辗转相除法:

用较小数除较大数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,如此反复,直到最后余数是0为止。如果是求两个数的最大公约数,那么最后的除数就是这两个数的最大公约数。

代码:

public class Programming6 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int num1 = scanner.nextInt();

int num2 = scanner.nextInt();

int res1 = f(num1, num2);

int res2 = num2 * num1 / res1;

System.out.println("最大公约数:" + res1 + "\t最小公倍数:" + res2);

System.out.println("最大公约数:" + gcd(num1, num2) + "\t最小公倍数:" + res2);

}

public static int f(int x, int y) {

/**

* @description:辗转相除法求取最大公约数迭代求解

* @param x

* @param y

* @createDate: 2020/7/4 23:10

* @return: int

*/

int t;

if (x < y) {

t = x;

x = y;

y = t;

}

while (y != 0) {

if (x == y) {

return x;

} else {

int k = x % y;

x = y;

y = k;

}

}

return x;

}

public static int gcd(int x, int y) {

/**

* @description:辗转相除法求取最大公约数的递归求解

* @param x

* @param y

* @createDate: 2020/7/4 23:11

* @return: int

*/

int t;

if (x < y) {

t = x;

x = y;

y = t;

}

if (x == y || y == 0) {

return x;

} else {

return gcd(y, x % y);

}

}

}

程序7:检索字符串字母数字空格

题目概述:

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

代码:

public class Programming7 {

public static void main(String[] args) {

int digital = 0;

int letter = 0;

int other = 0;

int blank = 0;

char[] ch = null;

System.out.println("请输入目标字符串:");

Scanner scanner = new Scanner(System.in);

String str = scanner.nextLine();

ch = str.toCharArray();

for (int i = 0; i < ch.length; i++) {

if (ch[i] >= '0' && ch[i] <= '9') {

digital++;

} else if ((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) {

letter++;

} else if (ch[i] == ' ') {

blank++;

} else {

other++;

}

}

System.out.println("数字个数为:" + digital);

System.out.println("字母个数为:" + letter);

System.out.println("空格个数为:" + blank);

System.out.println("其它字符个数为:" + other);

}

}

程序8:求sum=a+aa+aaa+aaaa+aa…a的值

题目概述:

代码:

public class Programming8 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("请输入数字的值:");

int base = scanner.nextInt();

System.out.println("请输入执行的次数:");

int n = scanner.nextInt();

//循环累加

int a = base, sum = base;

for (int i = 1; i < n; i++) {

a = a * 10 + 2;

sum = sum + a;

}

System.out.println("结果为:"+ sum);

}

}

程序9:完数

题目概述:

求1000以内的完数

完数:一个数恰好等于它的因子之和

代码:

public class Programming9 {

public static void main(String[] args) {

for (int j = 1; j <= 1000; j++) {

int sum = 0;

for (int i = 1; i < j; i++) {

if (j % i == 0) {

sum = sum + i;

}

}

if (sum == j) {

System.out.println(j + " ");

}

}

}

}

程序10:弹性球反弹问题

题目概述:

一个球从100米高度自由落下,每次落地后反跳回原高度的一半再落下,求它在10次落地时,共经过多少米,第十次反弹有多高。

代码:

public class Programming10 {

public static void main(String[] args) {

double h = 100;

double s = h;

for (int i = 1; i < 10; i++) {

s = s + h;

h = h / 2;

}

double h10 = h / 2;

System.out.println("10次落地时,共经过" + s + "米\t" + "第十次反弹高度为:" + h10 + "米");

}

}

本文地址:https://blog.csdn.net/weixin_45043707/article/details/107130874

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值