过一遍经典算法题,写写代码顺便整理思路
2017-04-10 12:13
447 查看
一、兔子问题
package demo;
import java.util.Scanner;
publicclass Test01 {
/*
* 【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,
* 问每个月的兔子总数为多少? 1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....
*/
publicstaticvoid main(String[]
args) {
Scanner sc =
new Scanner(System.in);
System.out.println(math.myMath(sc.nextInt()));
}
}
class math {
publicstaticint myMath(intx)
{
if (x == 1 ||
x == 2) {
return 1;
} else {
return myMath(x - 2) +
myMath(x - 1);
}
}
}
二、判断素数
package demo;
publicclass Test02 {
/*
【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
*/
publicstaticvoid main(String[]
args) {
StringBuilder sb=new StringBuilder();
for(intx=100;x<200;x++){
if(mathTool.ispn(x)){
sb.append(x);
sb.append(",");
}
}
sb.delete(sb.length()-1,
sb.length());
System.out.println("100-200间素数包括:"+sb);
}
}
class mathTool{
publicstaticboolean ispn(intx)
{
booleanflag=true;
for(inty=2;y<x;y++){
if(x%y==0){
flag=false;
}
}
returnflag;
}
}
三、水仙花数
package demo;
publicclass Test03 {
/*
【程序3】
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数
"是指一个三位数,
*
其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
*/
publicstaticvoid main(String[]
args) {
StringBuilder sb=new StringBuilder();
for(intx=100;x<999;x++){
if(Tool03.isFlower(x)){
sb.append(x);
sb.append(",");
}
}
sb.delete(sb.length()-1,
sb.length());
System.out.println("100-999包含水仙花数:"+sb);
}
}
class Tool03{
publicstaticboolean isFlower(intx){
booleanflag=false;
intbai=x/100;
intshi=x%100/10;//%100取十位数和个位数
intge=x%10;
//结论:取上位用除,取下位用模
if((int)Math.pow(bai, 3)+(int)Math.pow(shi,
3)+(int)Math.pow(ge, 3)==x){
flag=true;
}
returnflag;
}
}
————————————————————————————————————————————————————————————————
持续更新中 17-4-10 12:19
package demo;
import java.util.Scanner;
publicclass Test01 {
/*
* 【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,
* 问每个月的兔子总数为多少? 1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....
第N个月 | 0 | 1 | 2 | 3 | 4 | 5 |
1月小兔子 | 1 | 0 | 1 | 1 | 2 | 3 |
2月小兔子 | 0 | 1 | 0 | 1 | 1 | 2 |
成兔 | 0 | 0 | 1 | 1 | 2 | 3 |
总数 | 1 | 1 | 2 | 3 | 5 | 8 |
*/
publicstaticvoid main(String[]
args) {
Scanner sc =
new Scanner(System.in);
System.out.println(math.myMath(sc.nextInt()));
}
}
class math {
publicstaticint myMath(intx)
{
if (x == 1 ||
x == 2) {
return 1;
} else {
return myMath(x - 2) +
myMath(x - 1);
}
}
}
二、判断素数
package demo;
publicclass Test02 {
/*
【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
*/
publicstaticvoid main(String[]
args) {
StringBuilder sb=new StringBuilder();
for(intx=100;x<200;x++){
if(mathTool.ispn(x)){
sb.append(x);
sb.append(",");
}
}
sb.delete(sb.length()-1,
sb.length());
System.out.println("100-200间素数包括:"+sb);
}
}
class mathTool{
publicstaticboolean ispn(intx)
{
booleanflag=true;
for(inty=2;y<x;y++){
if(x%y==0){
flag=false;
}
}
returnflag;
}
}
三、水仙花数
package demo;
publicclass Test03 {
/*
【程序3】
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数
"是指一个三位数,
*
其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
*/
publicstaticvoid main(String[]
args) {
StringBuilder sb=new StringBuilder();
for(intx=100;x<999;x++){
if(Tool03.isFlower(x)){
sb.append(x);
sb.append(",");
}
}
sb.delete(sb.length()-1,
sb.length());
System.out.println("100-999包含水仙花数:"+sb);
}
}
class Tool03{
publicstaticboolean isFlower(intx){
booleanflag=false;
intbai=x/100;
intshi=x%100/10;//%100取十位数和个位数
intge=x%10;
//结论:取上位用除,取下位用模
if((int)Math.pow(bai, 3)+(int)Math.pow(shi,
3)+(int)Math.pow(ge, 3)==x){
flag=true;
}
returnflag;
}
}
————————————————————————————————————————————————————————————————
持续更新中 17-4-10 12:19
相关文章推荐
- 在CSDN上共享了7个经典定位算法的MATLAB代码
- 【转】兄弟你留给下了上千行的思路凌乱功能脆弱算法混乱有Linq有多线程的代码拍屁股就走人了很是让我们郁闷啊
- 转载某博主整理的资料:程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大经典原创系列集锦与总结
- 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 经典面试题(二)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- JAVA经典算法收集整理
- ZZ 常用算法经典代码(C++版)
- 【HDU4313】Matrix 多校 解题报告+AC代码+思路+算法正确性证明,此为Kruskal贪心简单版本,恶心版本稍后放出【目标达成 0.2%】
- 算法竞赛入门经典 第二章 上机练习(C++代码)
- 常用算法经典代码(C++版)
- 【算法设计与分析】经典代码赏析【1】
- 经典面试题(二)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 经典面试题(四)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 【HDU4313】Matrix 多校 解题报告+AC代码+思路+算法正确性证明,此为Kruskal贪心恶心版本,非自虐倾向慎入!建议想找解题报告的童鞋看简单版本的,这个我写给自己【目标达成 0.2%】
- 数据结构与算法分析学习笔记(二)--AVL树的算法思路整理
- 兄弟你留下了上千行的思路凌乱功能脆弱算法模糊有Linq有多线程的代码拍屁股就走人了很是让我们郁闷啊
- 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 微软四道经典算法面试题(附思路)
- 常用算法经典代码(C++版)