java经典算法50题_JAVA经典算法50题

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

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

具体分析如下:

f(1) = 1(第1个月有一对兔子)

f(2) = 1(第2个月还是一对兔子)

f(3) = 2(原来有一对兔子,第3个开始,每个月生一对兔子)

f(4) = 3(原来有两对兔子,有一对可以生育)

f(5) = 5(原来有3对兔子,第3个月出生的那对兔子也可以生育了,那么现在有两对兔子可以生育)

f(6) = 8(原来有5对兔子,第4个月出生的那对兔子也可以生育了,那么现在有3对兔子可以生育)

..............

由以上可以看出,第n个月兔子的对数为

f(n) = f(n - 1) + f(n - 2);

f(n-1)是上个月的兔子数量,是原来有的。

f(n-2)是可以生育的兔子数,即多出来的数量。第n-2个月开始后的第3个月是第n个月,此时第n-2个月时的兔子都可以生育了。

public class Demo01 {

public static void main(String args[]) {

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

System.out.println(f(i));

}

}

public static int f(int x) {

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

return 1;

} else {

return f(x - 1) + f(x - 2);

}

}

}

public class Demo01 {

public static void main(String args[]) {

math mymath = new math();

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

System.out.println(mymath.f(i));

}

}

}

class math {

public int f(int x) {

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

return 1;

} else {

return f(x - 1) + f(x - 2);

}

}

}

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

1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

public class Demo02 {

public static void main(String[] args) {

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

boolean flag = true;

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

if(i % j == 0) {

flag=false;

break;

}

}

if(flag == true) {

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

}

}

}

}

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

1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

public class Demo03 {

public static void main(String args[]) {

math mymath = new math();

for (int i = 100; i <= 999; i++) {

if (mymath.shuixianhua(i) == true) {

System.out.println(i);

}

}

}

}

class math {

public boolean shuixianhua(int x) {

int i = 0, j = 0, k = 0;

i = x / 100;

j = (x % 100) / 10;

k = x % 10;

if (x == i * i * i + j * j * j + k * k * k) {

return true;

} else {

return false;

}

}

}

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

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

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

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

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

import java.util.Scanner;

public class Demo04 {

public Demo04() {

super();

}

public void fenjie(int n) {

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

if (n % i == 0) {

System.out.print(i);

if (n != i) {

System.out.print("*");

}

fenjie(n/i);

}

}

System.exit(0); // 退出程序

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

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

int N = in.nextInt();

System.out.print( "分解质因数:" + N +"=");

new Demo04().fenjie(N);

}

}

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

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

import java.util.Scanner;

public class Demo05 {

public static void main(String[] args) {

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

Scanner in = new Scanner(System.in);

int N = in.nextInt();

System.out.println(N >= 90 ?"A": (N >= 60 ? "B":"C"));

}

}

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

1.程序分析:利用辗除法。

import java.util.Scanner;

public class Demo06 {

public static void main(String[] args) {

int a,b,m,n;

Scanner in = new Scanner(System.in);

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

a = in.nextInt();

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

b = in.nextInt();

commonDivisor use = new commonDivisor();

m = use.commonDivisor(a,b);

n = a*b/m;

System.out.println("最大公约数:"+m);

System.out.println("最小公倍数:"+n);

}

}

class commonDivisor {

public int commonDivisor(int x,int y) {

if(x < y) {

int 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;

}

}

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

1.程序分析:利用for循环语句,if条件语句。

import java.util.Scanner;

public class Demo07 {

public static void main(String[] args) {

System.out.println("请输入一个字符串;");

Scanner in = new Scanner(System.in);

String str = in.nextLine();

char[] ch = str.toCharArray();

count use = new count();

use.count(ch);

}

}

class count{

int digital,character,blank,other;

public void count(char[] arr) {

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

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

digital++;

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

character++;

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

blank++;

} else {

other++;

}

}

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

System.out.println("英文字母个数:"+character);

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

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

}

}

【程序8】   题目:求s = a + aa + aaa + aaaa + aa...a的值,其中a是一个数字。例如2 + 22 + 222 + 2222 + 22222(此时共有5个数相加),几个数相加有键盘控制。

1.程序分析:关键是计算出每一项的值。

import java.util.Scanner;

public class Demo08 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println(请输入a的值);

int a = in.nextInt();

System.out.println(请输入n个数);

int n = in.nextInt();

int s = 0,t=0;

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

t += a;

a = a*10;

s += t;

}

System.out.println(s);

}

}

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

public class Demo09 {

public static void main(String[] args) {

int s;

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

s = 0;

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

if (i % j == 0) {

s = s + j;

}

}

if (s == i)  {

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

}

}

System.out.println();

}

}

【程序10】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

public class Demo10 {

public static void main(String[] args) {

double s = 0;

double h = 100;

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

s += h;

h = h/2;

s += h;

}

System.out.println("经过路程:"+s);

System.out.println("反弹高度:"+h);

}

}

【程序11】   题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

public class Demo11 {

public static void main(String[] args) {

int count = 0;

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

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

for (int k = 1; k <= 4; k++) {

if (i != j && j != k && i != k) {

count += 1;

System.out.println(i * 100 + j * 10 + k);

}

}

}

}

System.out.println("共" + count + "个三位数");

}

}

【程序12】  题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润lirun,求应发放奖金总数sum?

1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

import java.util.Scanner;

public class Demo12 {

public static void main(String[] args) {

double sum;

System.out.println("输入当月利润:(万元)");

Scanner in = new Scanner(System.in);

double lirun = in.nextDouble();

if (lirun <= 10) {

sum = lirun * 0.1;

} else if (lirun <= 20) {

sum = 10*0.1 + (lirun - 10) * 0.075;

} else if (lirun <= 40) {

sum = 10*0.1 + 10*0.075 + (lirun - 20) * 0.05;

} else if (lirun <= 60) {

sum = 10*0.1 + 10*0.075 + 10*0.05 + (lirun - 40) * 0.03;

} else if (lirun <= 100) {

sum = 10*0.1 + 10*0.075 + 10*0.05 + 10*0.03 + (lirun - 60) * 0.015;

} else {

sum = 10*0.1 + 10*0.075 + 10*0.05 + 10*0.03 + 10*0.015 + (lirun - 100) * 0.01;

}

System.out.println("应发的奖金是:"+sum+"(万元)");

}

}

【程序13】   题目:一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?

1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上168后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:

public class Demo13 {

public static void main(String[] args) {

for(int x = 1 ; x < 100000 ; x ++) {

int value1 = Math.sqrt(x + 100);

int value2 = Math.sqrt(x + 100 + 168)

if (value1 * value1 == x && value2 * value2 == x) {

System.out.println(x+"加上100后是一个完全平方数,加上168又是一个完全平方数");

}

}

}

}

【程序14】 题目:输入某年某月某日,判断这一天是这一年的第几天?

1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本月的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

import java.util.Calendar;

import java.util.Scanner;

public class Demo14 {

public static void main(String[] args) {

System.out.println("请输入年,月,日:");

Scanner in = new Scanner(System.in);

int year = in.nextInt();

int month = in.nextInt();

int day = in.nextInt();

Calendar cal = Calendar.getInstance();

cal.set(year, month - 1, day);

int sum = cal.get(Calendar.DAY_OF_YEAR);

System.out.println("这一天是这一年的第" + sum +"天");

}

}

import java.util.*;

public class Demo14 {

public static void main(String[] args) {

int year,month,day,sum = 0;

Scanner in = new Scanner(System.in);

System.out.println("输入年:");

year = in.nextInt();

System.out.println("输入月:");

month = in.nextInt();

System.out.println("输入日:");

day = in.nextInt();

switch(month) {

case 1:

sum=0;

break;

case 2:

sum=31;

break;

case 3:

sum=59;

break;

case 4:

sum=90;

break;

case 5:

sum=120;

break;

case 6:

sum=151;

break;

case 7:

sum=181;

break;

case 8:

sum=212;

break;

case 9:

sum=243;

break;

case 10:

sum=273;

break;

case 11:

sum=304;

break;

case 12:

sum=334;

break;

default:

System.out.println("wrong input!");

return;

}

sum = sum+day;

boolean leap;

if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {

leap = true;

} else {

leap = false;

}

if(leap && month > 2) {

sum++;

}

System.out.println("It is the "+sum+"th day.");

}

}

import java.util.Scanner;

public class Demo14 {

public static void main(String[] args) {

System.out.println("请输入年 月 日:");

Scanner in = new Scanner(System.in);

int year = in.nextInt();

int month = in.nextInt();

int day = in.nextInt();

System.out.println("是该年的第"+count(year,month,day)+"天");

}

public static int count(int year,int month,int day) {

int sum = 0;

int days = 0;

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

switch(i) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

days=31;

break;

case 4:

case 6:

case 9:

case 11:

days=30;

break;

case 2:

if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {

days=29;

} else {

days=28;

}

break;

}

sum+=days;

}

sum+=day;

return sum;

}

}

【程序15】 题目:输入三个整数x,y,z,请把这三个数由小到大输出。

1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。

import java.util.Arrays;

import java.util.Scanner;

public class Demo15 {

public static void main(String[] args) {

System.out.print("请输入三个数:");

Scanner in = new Scanner(System.in);

int[] arr = new int[3];

for (int i = 0; i < 3; i++) {

arr[i] = in.nextInt();

}

Arrays.sort(arr);

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

System.out.print(arr[i] + " ");

}

}

}

if(x > y) {

int t = x;

x = y;

y = t;

}

if(x > z) {

int t = x;

x = z;

z = t;

}

if(y > z) {

int t = y;

y = z;

z = t;

}

【程序16】 题目:输出9*9口诀乘法表。

1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

出现重复的乘积(全矩形)

public class Demo16 {

public static void main(String[] args) {

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

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

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

}

System.out.println();

}

}

}

不现重复的乘积(下三角)

public class Demo16 {

public static void main(String[] args) {

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

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

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

}

System.out.println();

}

}

}

【程序17】   题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

1.程序分析:采取逆向思维的方法,从后往前推断。

public class Demo17 {

public static void main(String[] args) {

int sum = 1;

for (int i = 0; i < 9; i++) {

sum = (sum + 1) * 2;

}

System.out.println("第一天共摘"+sum);

}

}

【程序18】   题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

public class Demo18 {

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]);

}

}

}

}

}

public class Demo18 {

public String a, b, c;

public Demo18(String a, String b, String c) {

this.a = a;

this.b = b;

this.c = c;

}

public static void main(String[] args) {

Demo18 arr_a = new Demo18("a", "b", "c");

String[] b = { "x", "y", "z" };

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

for (int k = 0; k < 3; k++) {

Demo18 arr_b = new Demo18(b[i], b[j], b[k]);

if (!arr_b.a.equals(arr_b.b) & !arr_b.b.equals(arr_b.c)

& !arr_b.c.equals(arr_b.a) & !arr_b.a.equals("x")

& !arr_b.c.equals("x") & !arr_b.c.equals("z")) {

System.out.println(arr_a.a + "--" + arr_b.a);

System.out.println(arr_a.b + "--" + arr_b.b);

System.out.println(arr_a.c + "--" + arr_b.c);

}

}

}

}

}

}

【程序19】  题目:打印出如下图案(菱形)

1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控制列。

三角形:

*

***

******

********

******

***

*

public class Demo19 {

public static void main(String[] args) {

int i = 0;

int j = 0;

for ( i = 1; i <= 4; i++) {

for ( j = 1; j <= 2 * i - 1; j++) {

System.out.print("*");

}

System.out.println();

}

for ( i = 3; i >= 1; i--) {

for ( j = 1; j <= 2 * i - 1; j++) {

System.out.print("*");

}

System.out.println();

}

}

}

菱形:

*

***

*****

*******

*****

***

*

public class Demo19 {

public static void main(String[] args) {

int i = 0;

int j = 0;

for (i = 1; i <= 4; i++)  {

for (int k = 1; k <= 4 - i; k++) {

System.out.print( " " );

}

for (j = 1; j <= 2 * i - 1; j++) {

System.out.print("*");

}

System.out.println();

}

for (i = 3; i >= 1; i--) {

for (int k = 1; k <= 4 - i; k++) {

System.out.print( " " );

}

for (j = 1; j <= 2 * i - 1; j++) {

System.out.print("*");

}

System.out.println();

}

}

}

【程序20】   题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

1.程序分析:请抓住分子与分母的变化规律。

public class Demo20 {

public static void main(String[] args) {

float fm = 1.0f;

float fz = 1.0f;

float temp;

float sum = 0f;

for (int i = 0; i < 20; i++) {

temp = fm;

fm = fz;

fz = fz + temp;

System.out.println((int) fz + "/" + (int) fm);

sum += fz / fm;

}

System.out.println(sum);

}

}

【程序21】   题目:求1+2!+3!+...+20!的和。

1.程序分析:此程序只是把累加变成了累乘。

public class Demo21 {

public static void main(String[] args) {

long sum = 0;

long fac = 1;

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

fac = fac * i;

sum += fac;

}

System.out.println(sum);

}

}

【程序22】   题目:利用递归方法求5!。

1.程序分析:递归公式:f(n)=f(n-1)*4!

import java.util.Scanner;

public class Demo22 {

public static long fac(int n) {

long value = 0;

if (n == 1 || n == 0) {

value = 1;

} else if (n > 1) {

value = n * fac(n - 1);

}

return value;

}

public static void main(String[] args) {

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

Scanner in = new Scanner(System.in);

int n = in.nextInt();

System.out.println(n + "的阶乘为:" + fac(n));

}

}

【程序23】   题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两

岁。最后问第一个人,他说是10岁。请问第五个人多大?

1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。

直接求解:

public class Demo23 {

public static void main(String[] args) {

int n = 10;

for (int i = 0; i < 4; i++) {

n = n + 2;

}

System.out.println("第五个人" + n + "岁");

}

}

递归求解:

public class Demo23 {

public static int getAge(int n) {

if (n == 1) {

return 10;

}

return 2 + getAge(n - 1);

}

public static void main(String[] args) {

System.out.println("第五个的年龄为" + getAge(5));

}

}

【程序24】   题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

本题原方法:

import java.util.Scanner;

public class Demo24 {

public static void main(String[] args) {

Demo24 use = new Demo24();

System.out.println("请输入:");

Scanner in = new Scanner(System.in);

long a = in.nextLong();

if (a < 0 || a >= 100000) {

System.out.println("Error Input, please run this program Again!");

System.exit(0);

}

if (a >= 0 && a <= 9) {

System.out.println(a + "是一位数");

System.out.println("按逆序输出是:"  + a);

} else if (a >= 10 && a <= 99) {

System.out.println(a + "是二位数");

System.out.println("按逆序输出是:");

use.converse(a);

} else if (a >= 100 && a <= 999) {

System.out.println(a + "是三位数");

System.out.println("按逆序输出是:");

use.converse(a);

} else if (a >= 1000 && a <= 9999) {

System.out.println(a + "是四位数");

System.out.println("按逆序输出是:");

use.converse(a);

} else if (a >= 10000 && a <= 99999) {

System.out.println(a + "是五位数");

System.out.println("按逆序输出是:");

use.converse(a);

}

}

public void converse(long l) {

String s = Long.toString(l);

char[] ch = s.toCharArray();

for (int i = ch.length - 1; i >= 0; i--) {

System.out.print(ch[i]);

}

}

}

个人版方法:

import java.util.Scanner;

public class Demo24 {

public static void main(String[] args) {

System.out.println("请输入:");

Scanner in = new Scanner(System.in);

String str = in.next();

if (str.matches("\\d+")) { //正则表达式

System.out.println("输入的是" + str.length() + "位数");

StringBuffer buf = new StringBuffer(str);

System.out.println(buf.reverse());//字符串反转

}

}

}

作者:Traveler1688

链接:https://www.jianshu.com/p/c4954b181165

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值