JAVA之经典算法

package Set.Java.algorithm;

import java.util.Scanner;

public class algorithm {

/**
* 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
* 1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....
**/
public static void getRabbitValue() {
Scanner in = new Scanner( System.in );
System.out.print( "斐波那契,输入第N位数:" );
int N = in.nextInt();
System.out.print( "斐波那契算法:" + algorithm.getRabbit( N ) + "\n" );
}

public static int getRabbit(int n) {
if (n == 1) {
return 1;
} else if (n == 2) {
return 1;
} else {
return getRabbit( n - 1 ) + getRabbit( n - 2 );
}
}


/**
* 【程序2】 题目:判断101-200之间有多少个素数,并输出所有素数。
* 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
*/
public static void getPrime() {
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.println( "素数:" + i );
}
}

}

/**
* 【程序3】 题目:打印出所有的 水仙花数 ,所谓 水仙花数 是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 水仙花数 ,因为153=1的三次方+5的三次方+3的三次方。
* 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
*/
public static void getNarcissus() {
for ( int i = 100; i <= 999; i++ ) {
if (algorithm.getNarcissusValue( i ) == true) {
System.out.println( "水仙花数算法:" + i );
}
}
}

public static boolean getNarcissusValue(int n) {
int i = 0, j = 0, k = 0;
i = n / 100;
j = (n % 100) / 10;
k = n % 10;
if (n == 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的值,重复执行第一步。
*/

public static void getFenjie() {
Scanner in = new Scanner( System.in );
System.out.print( "请输入N的值:" );
int N = in.nextInt();
System.out.print( "分解质因数:" + N + "=" );
fenjie( N );
}

public static void fenjie(int n) {
for ( int i = 2; i <= n; i++ ) {
if (n % i == 0) {
System.out.print( i );
}
fenjie( n / i );
}
}


/**
* 【程序5】 题目:利用条件运算符的嵌套来完成此题:学习成绩=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
* 1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
*/
public static void getConditional() {
Scanner in = new Scanner( System.in );
System.out.print( "请输入N的值:" );
int N = in.nextInt();
System.out.print( (N >= 90) ? "A" : ((N >= 60) ? "B" : "C") );
}


/**
* 【程序6】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
* 1.程序分析:利用辗除法。
*/
public static void getMultiple() {

Scanner in = new Scanner( System.in );
System.out.print( "请输入一个正整数:" );
int a = in.nextInt();
System.out.print( "请再输入一个正整数:" );
int b = in.nextInt();
int m = new algorithm().commonDivisor( a, b );
int n = a * b / m;
System.out.print( "最大公约数:" + m );
System.out.print( "最小公倍数:" + n );
}

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条件语句。
*/
public static void getNumber() {
Scanner in = new Scanner( System.in );
System.out.print( "输入字符串,计算各字符个数:" );
String string = in.nextLine();
char[] chars = string.toCharArray();
algorithm.getNumberValue( chars );
}

public static void getNumberValue(char[] chars) {
int digital = 0, character = 0, blank = 0, other = 0;
for ( int i = 0; i < chars.length; i++ ) {
if (chars[i] >= '0' && chars[i] <= '9') {
digital++;
} else if ((chars[i] >= 'a' && chars[i] <= 'z') || chars[i] >= 'A' && chars[i] <= 'Z') {
character++;
} else if (chars[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.程序分析:关键是计算出每一项的值。
*/
public static void getCount() {
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 static void getFctor() {
int n;
for ( int i = 1; i <= 10; i++ ) {
n=0;
for ( int j = 1; j < i; j++ ) {
if (i % j == 0) {
n = n + j;
}
if (n == i) {
System.out.println( i + " " );
}
}
}
}


/**
* 【程序10】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
*/
public static void getFree(){
double s=0;
double height = 100;
for ( int i=1;i<=10;i++ ){
s+=height;
height=height/2;
s+=height;
}
System.out.println("经过路程:"+s);
System.out.println("反弹高度:"+height);
}


public static void main(String[] args) {

// System.out.print( "斐波那契算法:" + algorithm.getRabbit( 3 ) + "\n" );
// algorithm.getPrime();
// algorithm.getNarcissus();

//algorithm.getFenjie();

//algorithm.getConditional();
//algorithm.getMultiple();
//algorithm.getNumber();
//algorithm.getCount();
//algorithm.getFctor();
//algorithm.getFree();
}
}
posted @ 2017-10-11 14:46  GᎭ•Cristin  阅读(206)  评论(0编辑  收藏  举报