版权声明:本文为博主原创文章,未经博主允许不得转载。
Java经典问题算法大全
/*【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
*/
/*【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
*/
package cn.com.flywater.FiftyAlgorthm;
public class FirstRabbit {
public static final int MONTH = 15;
public static void main(String[] args) {
long f1 = 1L, f2 = 1L;
long f;
for(int i=3; i<MONTH; i++) {
f = f2;
f2 = f1 + f2;
f1 = f;
System.out.print("第" + i +"个月的兔子对数: ");
System.out.println(" " + f2);
}
public static final int MONTH = 15;
public static void main(String[] args) {
long f1 = 1L, f2 = 1L;
long f;
for(int i=3; i<MONTH; i++) {
f = f2;
f2 = f1 + f2;
f1 = f;
System.out.print("第" + i +"个月的兔子对数: ");
System.out.println(" " + f2);
}
}
}
/*【程序2】
* 作者 若水飞天
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。 */
package cn.com.flywater.FiftyAlgorthm;
public class SecondPrimeNumber {
public static int count = 0;
public static int count = 0;
public static void main(String[] args) {
for(int i=101;i<200;i++){
boolean b = true;//默认此数就素数
for(int j=2;j<=Math.sqrt(i);j++){
if(i%j==0){
b = false; //此数不是素数
break;
}
}
if(b){
count++;
System.out.print(i+" ");
}
}
System.out.println("/n素数的个数:"+count);
}
for(int i=101;i<200;i++){
boolean b = true;//默认此数就素数
for(int j=2;j<=Math.sqrt(i);j++){
if(i%j==0){
b = false; //此数不是素数
break;
}
}
if(b){
count++;
System.out.print(i+" ");
}
}
System.out.println("/n素数的个数:"+count);
}
}
/*【程序3】
作者 若水飞天
题目:打印出所有的"水仙花数(narcissus number)",所谓"水仙花数"是指一个三位数,
其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 */
题目:打印出所有的"水仙花数(narcissus number)",所谓"水仙花数"是指一个三位数,
其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 */
package cn.com.flywater.FiftyAlgorthm;
public class ThirdNarcissusNum {
static int b, bb, bbb;
public static void main(String[] args) {
for(int num=101; num<1000; num++) {
ThirdNarcissusNum tnn = new ThirdNarcissusNum();
tnn.f(num);
}
static int b, bb, bbb;
public static void main(String[] args) {
for(int num=101; num<1000; num++) {
ThirdNarcissusNum tnn = new ThirdNarcissusNum();
tnn.f(num);
}
}
public void f(int m) {
bbb = m / 100;
bb = (m % 100) / 10;
b = (m % 100) % 10;
if((bbb * bbb * bbb + bb * bb * bb + b * b * b) == m) {
System.out.println(m);
}
}
bbb = m / 100;
bb = (m % 100) / 10;
b = (m % 100) % 10;
if((bbb * bbb * bbb + bb * bb * bb + b * b * b) == m) {
System.out.println(m);
}
}
}
/*【程序4】
/*【程序4】
作者 若水飞天
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 */
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 */
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class FourthPrimeFactor {
static int n, k = 2;
static int n, k = 2;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
n = s.nextInt();
System.out.print(n + "=" );
FourthPrimeFactor fpf = new FourthPrimeFactor();
fpf.f(n);
}
Scanner s = new Scanner(System.in);
n = s.nextInt();
System.out.print(n + "=" );
FourthPrimeFactor fpf = new FourthPrimeFactor();
fpf.f(n);
}
public void f(int n) {
while(k <= n) {
if(k == n) {
System.out.println(n);
break;
} else if(n > k && n % k == 0) {
System.out.print(k + "*");
n = n / k;
f(n);
break;
} else if(n > k && n % k != 0) {
k++;
f(n);
break;
}
}
}
while(k <= n) {
if(k == n) {
System.out.println(n);
break;
} else if(n > k && n % k == 0) {
System.out.print(k + "*");
n = n / k;
f(n);
break;
} else if(n > k && n % k != 0) {
k++;
f(n);
break;
}
}
}
}
/*【程序5】
/*【程序5】
作者 若水飞天
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。 */
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。 */
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class FifthCondition {
//public static final int S1 = 90;
//public static final int S2 = 60;
static int grade;
//public static final int S2 = 60;
static int grade;
public static void main(String[] args) {
Scanner str = new Scanner(System.in);
int s = str.nextInt();
FifthCondition fc = new FifthCondition();
grade = fc.compare(s);
if(grade == 1) {
System.out.print('A');
} else if(grade == 2) {
System.out.print('B');
} else {
System.out.println('C');
}
Scanner str = new Scanner(System.in);
int s = str.nextInt();
FifthCondition fc = new FifthCondition();
grade = fc.compare(s);
if(grade == 1) {
System.out.print('A');
} else if(grade == 2) {
System.out.print('B');
} else {
System.out.println('C');
}
}
public int compare(int s) {
return s > 90 ? 1
: s > 60 ? 2
:3;
}
return s > 90 ? 1
: s > 60 ? 2
:3;
}
}
/*【程序6】
/*【程序6】
作者 若水飞天
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。 */
/*
* 在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回
* 较大的数,此数即为最小公约数,最小公倍数为两数之积除以最小公倍数。
* */
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。 */
/*
* 在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回
* 较大的数,此数即为最小公约数,最小公倍数为两数之积除以最小公倍数。
* */
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class SixthCommonDiviser {
public static void main(String[] args) {
int a, b;
Scanner s1 = new Scanner(System.in);
Scanner s2 = new Scanner(System.in);
a = s1.nextInt();
b = s2.nextInt();
SixthCommonDiviser scd = new SixthCommonDiviser();
int m = scd.division(a, b);
int n = a * b / m;
System.out.println("最大公约数: " + m);
System.out.println("最小公倍数: " + n);
}
int a, b;
Scanner s1 = new Scanner(System.in);
Scanner s2 = new Scanner(System.in);
a = s1.nextInt();
b = s2.nextInt();
SixthCommonDiviser scd = new SixthCommonDiviser();
int m = scd.division(a, b);
int n = a * b / m;
System.out.println("最大公约数: " + m);
System.out.println("最小公倍数: " + n);
}
public int division(int x, int y) {
int t;
if(x < y) {
t = x;
x = y;
y = t;
}
while(y != 0) {
if(x == y) return 1;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
/*【程序7】
作者 若水飞天
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为 '/n '. */
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为 '/n '. */
package cn.com.flywater.FiftyAlgorthm;
import java.util.*;
public class SeventhCharacterStatistics {
static int digital = 0;
static int character = 0;
static int other = 0;
static int blank = 0;
static int character = 0;
static int other = 0;
static int blank = 0;
public static void main(String[] args) {
char[] ch = null;
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
ch = s.toCharArray();
for(int i=0; i<ch.length; i++) {
if(ch[i] >= '0' && ch[i] <= '9') {
digital ++;
} else if((ch[i] >= 'a' && ch[i] <= 'z') || ch[i] > 'A' && ch[i] <= 'Z') {
character ++;
} else if(ch[i] == ' ') {
blank ++;
} else {
other ++;
}
}
char[] ch = null;
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
ch = s.toCharArray();
for(int i=0; i<ch.length; i++) {
if(ch[i] >= '0' && ch[i] <= '9') {
digital ++;
} else if((ch[i] >= 'a' && ch[i] <= 'z') || ch[i] > 'A' && ch[i] <= 'Z') {
character ++;
} else if(ch[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个数相加),几个数相加有键盘控制。
*/
/*
* 算法: 定义一个变量b, 赋初值为0;定义一变量sum, 赋初值为0,
* 进入循环后,将a + b 的值赋给b,将sum + b 的值赋给sum;
* 同时,将a 增加十倍, ++ i; 继续循环;
* 循环结束后,输出sum 的值。
*/
题目:求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("input number: " + n);
System.out.println(sum);
}
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("input number: " + n);
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("1到1000的完数有: ");
for(int i=1; i<1000; i++) {
int t = 0;
for(int j=1; j<= i/2; j++) {
if(i % j == 0) {
t = t + j;
}
}
if(t == i) {
System.out.print(i + " ");
}
}
}
}
/*【程序10】
作者 若水飞天
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,
求它在 第10次落地时,共经过多少米?第10次反弹多高?
*/
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,
求它在 第10次落地时,共经过多少米?第10次反弹多高?
*/
package cn.com.flywater.FiftyAlgorthm;
public class TenthTreeFall {
static double height = 100;
static double distance = 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("路程:" + distance);
System.out.println("高度:" + height / 2);
}
for(int i=1; i<10; i++) {
distance = distance + height;
height = height / 2;
}
System.out.println("路程:" + distance);
System.out.println("高度:" + height / 2);
}
}
/*【程序11】
* 作者 若水飞天
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
*/
/*算法:3个for循环加一个if语句;
*
*/
package cn.com.flywater.FiftyAlgorthm;
* 作者 若水飞天
题目:有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++) {
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("共有" + count + "个三位数");
int count = 0;
for(int x=1; x<5; x++) {
for(int y=1; y<5; y++) {
for(int z=1; z<5; z++) {
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("共有" + count + "个三位数");
}
}
/*【程序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("#0.0000");
*
*/
package cn.com.flywater.FiftyAlgorthm;
import java.text.DecimalFormat;
import java.util.*;
import java.util.*;
public class TwelfthProfitAward {
static double profit = 0;
static double award = 0;
static double award = 0;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
profit = s.nextInt();
System.out.println("输入的利润是" + profit + "万");
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;
} 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("#0.00000");
System.out.println("应该提取的奖金是 " + df.format(award) + "万");
Scanner s = new Scanner(System.in);
profit = s.nextInt();
System.out.println("输入的利润是" + profit + "万");
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;
} 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("#0.00000");
System.out.println("应该提取的奖金是 " + df.format(award) + "万");
}
}
/*【程序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又是一个完全平方数");
}
}
}
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.*;
import java.io.*;
public class FourteenthYearMonthDay {
public static void main(String[] args) {
int year, month, day;
int days = 0;
int d = 0;
FourteenthYearMonthDay fymd = new FourteenthYearMonthDay();
System.out.print("Input the year:");
year =fymd.input();
System.out.print("Input the month:");
month = fymd.input();
System.out.print("Input The Day:");
day = fymd.input();
if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) {
System.out.println("Input error, please run this program again!");
System.exit(0);
}
int year, month, day;
int days = 0;
int d = 0;
FourteenthYearMonthDay fymd = new FourteenthYearMonthDay();
System.out.print("Input the year:");
year =fymd.input();
System.out.print("Input the month:");
month = fymd.input();
System.out.print("Input The Day:");
day = fymd.input();
if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) {
System.out.println("Input error, please run this program again!");
System.exit(0);
}
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;
//d += days;
break;
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
//d += days;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
//d += days;
break;
case 6:
case 9:
case 11:
days = 30;
//d += days;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
days = 29;
} else {
days = 28;
}
//d += days;
break;
}
d += days;
}
System.out.println(year + ":" + month + ":" + day + "是今年的第" + (d+day) + "天。");
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
days = 29;
} else {
days = 28;
}
//d += days;
break;
}
d += days;
}
System.out.println(year + ":" + month + ":" + day + "是今年的第" + (d+day) + "天。");
}
public int input() {
int value = 0;
Scanner s = new Scanner(System.in);
value = s.nextInt();
return value;
}
}
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("Input 3 numbers:");
a = fnc.input();
b = fnc.input();
c = fnc.input();
//
// fnc.compare(a, b);//方法调用不能通过改变形参的值来改变实参的值
// fnc.compare(b, c);// 这种做法是错的
// fnc.compare(a, c);
//System.out.println("result:" + a +" " + b + " " + c);// 没有改变
if(a > b) {
int t = a;
a = b;
b = t;
}
if(a > c) {
int t = a;
a = c;
c = t;
}
if(b > c) {
int t = b;
b = c;
c = t;
}
FifteenthNumberCompare fnc = new FifteenthNumberCompare();
int a, b, c;
System.out.println("Input 3 numbers:");
a = fnc.input();
b = fnc.input();
c = fnc.input();
//
// fnc.compare(a, b);//方法调用不能通过改变形参的值来改变实参的值
// fnc.compare(b, c);// 这种做法是错的
// fnc.compare(a, c);
//System.out.println("result:" + a +" " + b + " " + c);// 没有改变
if(a > b) {
int t = a;
a = b;
b = t;
}
if(a > c) {
int t = a;
a = c;
c = t;
}
if(b > c) {
int t = b;
b = c;
c = t;
}
System.out.println( a + " " + b + " " + c);
}
}
public int input() {
int value = 0;
Scanner s = new Scanner(System.in);
value = s.nextInt();
return value;
}
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;
}
}
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 + "*" + i + "=" + j*i + " " );
}
System.out.println();
}
for(int i=1; i<10; i++) {
for(int j=1; j<=i; j++) {
System.out.print(j + "*" + i + "=" + j*i + " " );
}
System.out.println();
}
}
}
//【程序17】
//作者 若水飞天
//题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,
//又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
//以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,
//见只剩下一个桃子了。求第一天共摘了多少。
//1.程序分析:采取逆向思维的方法,从后往前推断。
//【程序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("猴子第一天摘了 " + lastdayNum + " 个桃子");
int lastdayNum = 1;
for(int i=2; i<=10; i++) {
lastdayNum = (lastdayNum+1) * 2;
}
System.out.println("猴子第一天摘了 " + lastdayNum + " 个桃子");
}
}
/*【程序18】
* 作者 若水飞天
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。
已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
* 作者 若水飞天
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。
已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
*/
/*
* 这个程序写得很不好,是知道结果后拼凑起来的,还不如直接写输出语句加上结果来的好。
*/
package cn.com.flywater.FiftyAlgorthm;
/*
* 这个程序写得很不好,是知道结果后拼凑起来的,还不如直接写输出语句加上结果来的好。
*/
package cn.com.flywater.FiftyAlgorthm;
public class EighteenthPingpong {
static char[] m = { 'a', 'b', 'c' };
static char[] n = { 'x', 'y', 'z' };
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++) {
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')) {
continue;
} else
System.out.println(m[i] + " vs " + n[j]);
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')) {
continue;
} else
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}
}
题目:打印出如下图案(菱形)
*
***
*****
*******
*****
***
*
1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,
第一层控制行,第二层控制列。
*/
/*上半部分循环变量的控制方法是
* for(int i=0; i<(HEIGHT+1) / 2; i++) {
for(int j=1; j<WIDTH/2-i; j++) {
for(int k=1; k<(i+1)*2; k++) {
下半部分循环变量的控制方法是
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;
第一层控制行,第二层控制列。
*/
/*上半部分循环变量的控制方法是
* for(int i=0; i<(HEIGHT+1) / 2; i++) {
for(int j=1; j<WIDTH/2-i; j++) {
for(int k=1; k<(i+1)*2; k++) {
下半部分循环变量的控制方法是
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;
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<WIDTH/2-i; j++) {
System.out.print(" ");
}
for(int k=1; k<(i+1)*2; k++) {
System.out.print('*');
}
System.out.println();
}
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 i=0; i<(HEIGHT+1) / 2; i++) {
for(int j=1; j<WIDTH/2-i; j++) {
System.out.print(" ");
}
for(int k=1; k<(i+1)*2; k++) {
System.out.print('*');
}
System.out.println();
}
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<WIDTH/2-i; j++)
上半部分第三重循环应改为: for(int k=1; k<=WIDTH-2*i; k++)
上半部分第二重循环应改为: for(int j=0; j<WIDTH/2-i; j++)
上半部分第三重循环应改为: for(int k=1; k<=WIDTH-2*i; k++)
/*【程序20】
* 作者 若水飞天
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
1.程序分析:请抓住分子与分母的变化规律。
*/
* 作者 若水飞天
题目:有一分数序列: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("#0.0000");
for(int i=1; i<=20; i++) {
sum += (double)x / y;
t = y;
y = x;
x = y + t;
System.out.println("第 " + i + " 次相加,和是 " + df.format(sum));
}
int x = 2, y = 1, t;
double sum = 0;
DecimalFormat df = new DecimalFormat("#0.0000");
for(int i=1; i<=20; i++) {
sum += (double)x / y;
t = y;
y = x;
x = y + t;
System.out.println("第 " + i + " 次相加,和是 " + df.format(sum));
}
}
/*【程序21】
* 作者 若水飞天
题目:求1+2!+3!+...+20!的和
1.程序分析:此程序只是把累加变成了累乘。
*/
package cn.com.flywater.FiftyAlgorthm;
public class Twenty_firstFactorialSum {
static long sum = 0;
static long fac = 0;
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for(int i=1; i<=10; i++) {
fac = fac * i;
sum += fac;
}
System.out.println(sum);
}
static long sum = 0;
static long fac = 0;
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for(int i=1; i<=10; i++) {
fac = fac * i;
sum += fac;
}
System.out.println(sum);
}
}
/*【程序22】
* 作者 若水飞天
题目:利用递归方法求5!。
1.程序分析:递归公式:fn=fn_1*4!
* 作者 若水飞天
题目:利用递归方法求5!。
1.程序分析:递归公式:fn=fn_1*4!
*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class Twenty_secondFactorialRecursion {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Twenty_secondFactorialRecursion tfr = new Twenty_secondFactorialRecursion();
System.out.println(tfr.recursion(n));
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Twenty_secondFactorialRecursion tfr = new Twenty_secondFactorialRecursion();
System.out.println(tfr.recursion(n));
}
public long recursion(int n) {
long value = 0 ;
if(n ==1 || n == 0) {
value = 1;
} else if(n > 1) {
value = n * recursion(n-1);
}
return value;
}
long value = 0 ;
if(n ==1 || n == 0) {
value = 1;
} else if(n > 1) {
value = n * recursion(n-1);
}
return value;
}
}
/*【程序23】
* 作者 : 若水飞天
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。
问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。
问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。
要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。
**/
/*【程序23】
* 作者 : 若水飞天
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。
问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。
问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。
要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。
**/
package cn.com.flywater.FiftyAlgorthm;
public class Twenty_thirdPeopleAge {
public static void main(String[] args) {
int age = 10;
for(int i=2; i<=5; i++) {
age += 2;
}
System.out.println(age);
}
int age = 10;
for(int i=2; i<=5; i++) {
age += 2;
}
System.out.println(age);
}
}
/*【程序24】
* 作者 若水飞天
题目:给一个不多于5位的正整数,
要求:一、求它是几位数,二、逆序打印出各位数字。
说明: 这个算法实现虽然实现了这个功能,但不健壮,当输入字符是,会出现异常。
* 作者 若水飞天
题目:给一个不多于5位的正整数,
要求:一、求它是几位数,二、逆序打印出各位数字。
说明: 这个算法实现虽然实现了这个功能,但不健壮,当输入字符是,会出现异常。
*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class Twenty_fourthNumber {
public static void main(String[] args) {
Twenty_fourthNumber tn = new Twenty_fourthNumber();
Scanner s = new Scanner(System.in);
long a = s.nextLong();
if(a < 0 || a > 100000) {
System.out.println("Error Input, please run this program Again");
System.exit(0);
}
if(a >=0 && a <=9) {
System.out.println( a + "是一位数");
System.out.println("按逆序输出是" + '/n' + a);
} else if(a >= 10 && a <= 99) {
System.out.println(a + "是二位数");
System.out.println("按逆序输出是" );
tn.converse(a);
} else if(a >= 100 && a <= 999) {
System.out.println(a + "是三位数");
System.out.println("按逆序输出是" );
tn.converse(a);
} else if(a >= 1000 && a <= 9999) {
System.out.println(a + "是四位数");
System.out.println("按逆序输出是" );
tn.converse(a);
} else if(a >= 10000 && a <= 99999) {
System.out.println(a + "是五位数");
System.out.println("按逆序输出是" );
tn.converse(a);
}
}
public void converse(long l) {
String s = Long.toString(l);
char[] ch = s.toCharArray();
for(int i=ch.length-1; i>=0; i--) {
System.out.print(ch[i]);
}
}
String s = Long.toString(l);
char[] ch = s.toCharArray();
for(int i=ch.length-1; i>=0; i--) {
System.out.print(ch[i]);
}
}
}
这个算法实在太土了,也许只有我若水飞天才会这样写,
下面写一个优雅一点的
这个算法实在太土了,也许只有我若水飞天才会这样写,
下面写一个优雅一点的