动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

6道经典算法面试题

05-12 16:39 737浏览
举报 T字号
  • 大字
  • 中字
  • 小字

1.斐波那契数列问题

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项 [科普] 斐波那契数列指的是这样一个数列 0, 1,1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368…

代码如下:

public class Test11 {
	public static void main(String[] args) {
		int fibonacci = fibonacci(10);
		System.out.println(fibonacci);
	}
	
	public static int fibonacci(int n) {
			if (n<=0)
            return 0;
			
	        int a=1,b = 1;int temp;
	        for(int i=2;i<n;i++){
	            temp = a;
	            a = b;
	            b = temp + b;
	        }
	        return b;
	    }
}

2.古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问题。

public class test01 {
public static void main(String[] args) {
int f1=1,f2=1,f;
int M=30;
System.out.println(1);
System.out.println(2);
for(int i=3;i<m;i++) {
f=f2;
f2=f1+f2;
f1=f;
System.out.println(f2);
}
}
}

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

public class test03 {
public static void main(String[] args) {
int a,b,c;
for(int i=101;i<1000;i++) {
a=i%10;
b=i/10%10;
c=i/100;
if(a*a*a+b*b*b+c*c*c==i)
System.out.println(i);
}
}
}

4.输入某年某月某日,判断这一天是这一年的第几天?

import java.util.*;
public class lianxi14 
{
public static void main(String[] args) 
{ 
int year, month, day; 
int days = 0; 
int d = 0; 
int e; 
input fymd = new input(); 
do 
{ 
e = 0; 
System.out.print("输入年:"); 
year =fymd.input(); 
System.out.print("输入月:");
month = fymd.input(); 
System.out.print("输入天:"); 
day = fymd.input(); 
if (year < 0 || month < 0 || month > 12 ||day < 0 || day > 31) 
{ 
System.out.println("输入错误,请重新输入!"); 
e=1 ; 
} 
}
while( e==1);
for (int i=1; i <month; 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;
}
d += days;
}
System.out.println(year + "-" + month +"-" + day + "是这年的第" +(d+day) + "天。");
}
}
class input{
public int input() {
int value = 0;
Scanner s = new Scanner(http://System.in);
value = s.nextInt();
return value;
}
}

5.输入三个整数x,y,z,请把这三个数由小到大排序输出。

import java.util.Scanner;
public class test15 {
public static void main(String[] args) {
Scanner input=new Scanner(http://System.in);
int x=input.nextInt();
int y=input.nextInt();
int z=input.nextInt();
int t=0;
if(x>y) {
t=x;
x=y;
y=t;
}
if(y>z) {
t=z;
z=y;
y=t;
}
if(x>y) {
t=x;
x=y;
y=t;
}
System.out.println(x+""+y+""+z);
}
}

6.判断101-200之间有多少个素数,并输出所有素数。 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。

public class test02 {
public static void main(String[] args) {
int count=0;
for(int i=101;i<200;i+=2) {
boolean flag=true;
for(int j=2;j<=Math.sqrt(i);j++) {
if(i%j==0) {
flag=false;
break;
}
}
if(flag==true) {
count++;
System.out.println(i);
}
}
System.out.println(count);
}
}

上述的6道算法题,都是在Java面试题中比较经典的存在,其中考察了很多的算法问题,我们可以先在动力节点在线打下良好的Java算法基础,再来着手解决这些复杂的算法问题,帮助我们查漏补缺,检验学习成果。

1人推荐
共同学习,写下你的评论
0条评论
杨晶珍
程序员杨晶珍

98篇文章贡献357785字

相关课程 更多>

作者相关文章更多>

推荐相关文章更多>

Java面试题及答案整理

提枪策马乘胜追击04-21 20:01

Spring常见面试题

代码小兵92504-17 16:07

Java零基础实战项目——五子棋

代码小兵98804-25 13:57

Java string类详解

杨晶珍05-11 14:54

6道经典算法面试题

杨晶珍05-12 16:39

发评论

举报

0/150

取消