Java经典算法大全

}

d += days; }

System.out.println(year + \是今年的第\+ \天。\

}

public int input() { int value = 0;

Scanner s = new Scanner(System.in); value = s.nextInt(); return value; }

}

/*【程序15】

* 作者 若水飞天

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

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

*/

package cn.com.flywater.FiftyAlgorthm; import java.util.*;

public class FifteenthNumberCompare {

public static void main(String[] args) {

FifteenthNumberCompare fnc = new FifteenthNumberCompare(); int a, b, c;

System.out.println(\ a = fnc.input(); b = fnc.input();

c = fnc.input(); //

// fnc.compare(a, b);//方法调用不能通过改变形参的值来改变实参的值 // fnc.compare(b, c);// 这种做法是错的

// fnc.compare(a, c);

//System.out.println(\没有改变

if(a > b) { int t = a; a = b; b = t; }

11

if(a > c) { int t = a; a = c; c = t; }

if(b > c) { int t = b; b = c; c = t; }

System.out.println( a + \}

public int input() { int value = 0;

Scanner s = new Scanner(System.in); value = s.nextInt(); return value;

}

public void compare(int x, int y) {//此方法没用 if(x > y) { int t = x; x = y; y = t; } }

}

/*【程序16】 * 作者 若水飞天

*题目:输出9*9口诀。

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

package cn.com.flywater.FiftyAlgorthm; public class SixteenthMultiplicationTable { public static void main(String[] args) { for(int i=1; i<10; i++) {

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

System.out.print(j + \ }

System.out.println(); } } }

12

//【程序17】 //作者 若水飞天

//题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾, //又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。 //以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时, //见只剩下一个桃子了。求第一天共摘了多少。 //1.程序分析:采取逆向思维的方法,从后往前推断。 package cn.com.flywater.FiftyAlgorthm; public class SeventeenthMonkeyPeach { public static void main(String[] args) { int lastdayNum = 1;

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

lastdayNum = (lastdayNum+1) * 2;

}

System.out.println(\猴子第一天摘了 \个桃子\} }

/*【程序18】

* 作者 若水飞天

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

*/ /*

* 这个程序写得很不好,是知道结果后拼凑起来的,还不如直接写输出语句加上结果来的好。

*/

package cn.com.flywater.FiftyAlgorthm; public class EighteenthPingpong { 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')) {

13

continue;

} else

System.out.println(m[i] + \ } } }

}

题目:打印出如下图案(菱形) * *** ***** ******* ***** ***

*

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

第一层控制行,第二层控制列。 */

/*上半部分循环变量的控制方法是

* for(int i=0; i<(HEIGHT+1) / 2; i++) { for(int j=1; j

下半部分循环变量的控制方法是 for(int i=1; i<=HEIGHT/2; i++) { for(int j=1; j<=i; j++) {

* for(int k=1; k<=WIDTH-2*i-1; k++) { */

package cn.com.flywater.FiftyAlgorthm; public class NineteenthPrintRhombic { static final int HEIGHT = 7;

static final int WIDTH = 8;

public static void main(String[] args) { for(int i=0; i<(HEIGHT+1) / 2; i++) { for(int j=1; j

}

for(int k=1; k<(i+1)*2; k++) { System.out.print('*'); }

System.out.println(); }

14

for(int i=1; i<=HEIGHT/2; i++) { for(int j=1; j<=i; j++) { System.out.print(\ }

for(int k=1; k<=WIDTH-2*i-1; k++) { System.out.print('*'); }

System.out.println(); } }

}

上半部分第二重循环应改为: for(int j=0; j

* 作者 若水飞天

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

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

package cn.com.flywater.FiftyAlgorthm; import java.text.DecimalFormat; public class TwentiethFractionSum { public static void main(String[] args) { int x = 2, y = 1, t; double sum = 0;

DecimalFormat df = new DecimalFormat(\

for(int i=1; i<=20; i++) { sum += (double)x / y; t = y; y = x;

x = y + t;

System.out.println(\第 \次相加,和是 \ } }

/*【程序21】

* 作者 若水飞天

题目:求1+2!+3!+...+20!的和

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

package cn.com.flywater.FiftyAlgorthm; public class Twenty_firstFactorialSum { static long sum = 0;

15

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4