Java经典算法大全 下载本文

内容发布更新时间 : 2024/5/5 16:53:12星期一 下面是文章的全部内容请认真阅读。

}

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

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

/*

* 算法: 定义一个变量b, 赋初值为0;定义一变量sum, 赋初值为0, * 进入循环后,将a + b 的值赋给b,将sum + b 的值赋给sum; * 同时,将a 增加十倍, ++ i; 继续循环; * 循环结束后,输出sum 的值。 */

package cn.com.flywater.FiftyAlgorthm; import java.util.Scanner; public class EightPlus { static long a = 2, b = 0;

public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int i = 0; long sum = 0; while(i < n) { b = b + a;

sum = sum + b; a = a * 10; ++ i; }

System.out.println(\ System.out.println(sum); }

}

/*【程序9】

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

*/

package cn.com.flywater.FiftyAlgorthm; public class NinthWanshu {

public static void main(String[] args) {

System.out.println(\到1000的完数有: \ for(int i=1; i<1000; i++) { int t = 0;

for(int j=1; j<= i/2; j++) { if(i % j == 0) {

6

t = t + j;

} }

if(t == i) {

System.out.print(i + \ } }

} }

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

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

package cn.com.flywater.FiftyAlgorthm; public class TenthTreeFall {

static double height = 100; static double distance = 100;

public static void main(String[] args) { for(int i=1; i<10; i++) { distance = distance + height;

height = height / 2; }

System.out.println(\路程:\ System.out.println(\高度:\}

}

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

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 */

/*算法:3个for循环加一个if语句; * */

package cn.com.flywater.FiftyAlgorthm; public class EleventhNumberRange { public static void main(String[] args) { int count = 0;

for(int x=1; x<5; x++) { for(int y=1; y<5; y++) { for(int z=1; z<5; z++) {

7

if(x != y && y != z && x != z) {

count ++;

System.out.print(x*100 + y*10 + z + \ \ if(count % 4 == 0) { System.out.println(); } } } }

}

System.out.println(\共有\个三位数\}

}

/*【程序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%提成,

从键盘输入当月利润I,求应发放奖金总数?

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

*/

/*注意: 要精确到小数点后多少位,用 DecimalFormat df = new DecimalFormat(\*

*/

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

public class TwelfthProfitAward { static double profit = 0; static double award = 0;

public static void main(String[] args) { Scanner s = new Scanner(System.in);

profit = s.nextInt();

System.out.println(\输入的利润是\万\ if(profit > 0 && profit <= 10) { award = profit * 0.1;

} else if(profit > 10 && profit <= 20) {

award = 10 * 0.1 + (profit - 10) * 0.075; } else if(profit > 20 && profit <= 40) {

award = 10 * 0.1 + 10 * 0.075 + (profit - 20) * 0.05;

8

} else if(profit > 40 && profit <= 60) {

award = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (profit - 40) * 0.03; } else if(profit > 60 && profit <= 100) {

award = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (profit - 60) * 0.015; } else if(profit > 100) {

award = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (profit - 100) * 0.01; }

DecimalFormat df = new DecimalFormat(\

System.out.println(\应该提取的奖金是 \万\} }

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

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

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

package cn.com.flywater.FiftyAlgorthm; public class ThirteenthTwiceSqrt {

public static void main(String[] args) { for(long l=1L; l<100000; l++) {

if(Math.sqrt((long)(l+100)) % 1 == 0) { if(Math.sqrt((long)(l+268)) % 1 == 0) {

System.out.println(l + \加100是一个完全平方数,再加168又是一个完全平方数\ } } }

} }

*【程序14】 * 作者 若水飞天

题目:输入某年某月某日,判断这一天是这一年的第几天? 1.程序分析:以3月5日为例,应该先把前两个月的加起来,

然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。 */

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

public class FourteenthYearMonthDay { public static void main(String[] args) { int year, month, day;

9

int days = 0;

int d = 0;

FourteenthYearMonthDay fymd = new FourteenthYearMonthDay();

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(\, please run this program again!\ System.exit(0);

}

for (int i=1; i

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { days = 29; } else { days = 28; }

//d += days; break;

10