您的位置:首页 > 编程语言 > Java开发

Java基础算法集50题

2015-11-20 22:48 447 查看
【程序1】 TestRabbit.java

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

    1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....

package cn.edu.hit;
/**
* 兔子问题
* 斐波那契数列求值
* @author tonylp
*题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
*小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
*1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
*/
public class rabbit {
public static final int MONTH = 15;
public static void main(String[] args) {
// TODO Auto-generated method stub
long f1 = 1L, f2 = 1L;
long f;
for(int i=3;i<MONTH;i++){
f=f1+f2;
f1=f2;
f2=f;
System.out.println("第"+i+"个月的兔子对数:"+f2);
}
/*
for(int i =1 ; i<MONTH; i++){
System.out.println("第"+i+"个月的兔子对数:"+fib(i));
}
*/
}
//递归方法实现
public static int fib(int month){
if(month == 1 || month == 2){
return 1;
}else{
return fib(month-1)+fib(month-2);
}
}
}


【程序3】FindDaffodilNumber.java

题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:

153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

package cn.edu.hit;

/**
* 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
* 例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
* 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
*
* @author tonylp
*/
public class daffodils {
public static void main(String[] args) {
int a, b, c;
int data;
for (int i = 100; i < 999; i++) {
a = i / 100;
b = (i - 100 * a) / 10;
c = i - 100 * a - 10 *b;
data = a*a*a+b*b*b+c*c*c;
if(data == i){
System.out.println(i);
}
}
}

}


【程序4】FenJie.java

    题目:将一个正整数分解质因数。例如:输入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.edu.hit;

/**
* 任意整数分解 题目:将一个正整数分解质因数。例如:输入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的值,重复执行第一步。
*
* @author tonylp
*/
public class resolveNum {
public static final int NUM = 130;

public static void main(String[] args) {
// TODO Auto-generated method stub
int k = 2;
int num = NUM;
System.out.print(num + "=");
while (num > k) {
if (num % k == 0) {
System.out.print(k + "*");
num = num / k;
} else {
k++;
}
}
System.out.println(k);
}
}


【程序6】Test1.java
GcdTest.java后者是辗转相除法

    题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

    1.程序分析:利用辗除法。

package cn.edu.hit;

import java.util.Scanner;

/**
* 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
*  1.程序分析:利用辗除法。
* @author tonylp
*/
public class comonDivisor {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int first = a;
int second = b;
System.out.println("a=" + a + ";b=" + b);
int temp;

if (a < b) {
temp = a;
a = b;
b = temp;
}
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
System.out.println("最大公约数为"+a);
System.out.println("最小公倍数为"+first*second/a);
}
}


【程序7】 StChar.java

    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

    1.程序分析:利用while语句,条件为输入的字符不为'\n'.

package cn.edu.hit;

import java.util.Scanner;

/**
* 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用while语句,条件为输入的字符不为'\n'.
*
* @author tonylp
*/
public class strIdentify {
public static void main(String[] args) {
int abcCount = 0;
int spaceCount = 0;
int numCount = 0;
int otherCount = 0;
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (Character.isDigit(ch[i])) {
numCount++;
} else if (Character.isSpaceChar(ch[i])) {
spaceCount++;
} else if (Character.isLetter(ch[i])) {
abcCount++;
} else {
otherCount++;
}
}
System.out.println("字母个数"+abcCount);
System.out.println("数字个数"+numCount);
System.out.println("空格个数"+spaceCount);
System.out.println("其他字符个数"+otherCount);
}

}


【程序8】 TestAdd.java

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

    1.程序分析:关键是计算出每一项的值。

package cn.edu.hit;
import java.util.Scanner;
/**
* 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
* 几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值。
*
* @author tonylp
*/
public class numAdd {
public static void main(String[] args) {
System.out.println("请输入数字0-9:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println("请输入数字重复次数:");
int b = sc.nextInt();
int s = 0;
System.out.println("a=" + a + ";b=" + b);
for (int i = b; i > 0; i--) {
s += i * a * Math.pow(10, (b - i));
}
System.out.println("s=" + s);
}

}


【程序9】 WanShu.java

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

package cn.edu.hit;

/**
* 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如6=1+2+3.编程 找出1000以内的所有完数。
*
* @author tonylp
*/
public class wanShu {
public static void main(String[] args) {
int k = 2;
int num = 0;
int temp = 1;
int j = 0;
for (num = 1; num <= 1000; num++) {
k = 2;
temp = 1;
j = num;
while (j >= k) {
if (j % k == 0) {
temp += k;
j = j / k;
} else {
k++;
}
}
if (temp == num) {
System.out.println(temp);
}

}
}
}


【程序10】TestBall.java

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

package cn.edu.hit;
/**
* 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;
* 再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
*/
public class testBall {
public static void main(String[] args) {
double a = 100;
double sum = 100;
for(int i =2 ;i<=10;i++){
a = a*0.5;
sum += a*2;
}
System.out.println("a="+a);
System.out.println("sum="+sum);
}
}


【程序11】 TestTN.java

    题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

    1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去
掉不满足条件的排列。

package cn.edu.hit;

/**
* 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
* 组成所有的排列后再去 掉不满足条件的排列。
*
* @author tonylp
*/
public class testTN {
public static void main(String[] args) {
int a = 0;
int b = 0;
int c = 0;
int num = 1;
for (a = 1; a < 5; a++) {
for (b = 1; b < 5; b++) {
for (c = 1; c < 5; c++) {
if (a != b && a != c && b != c) {
System.out.println("NO." + num + ":" + a + "" + b + ""
+ c);
num++;
}
}
}
}
}

}


【程序12】 MoneyAward.java

    题目:企业发放的奖金根据利润提成。利润(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.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

package cn.edu.hit;
import java.util.Scanner;
/**
* 题目:企业发放的奖金根据利润提成。 利润(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.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
*
* @author tonylp
*/
public class moneyAward {
public static void main(String[] args) {
double rate1 = 0.1, rate2 = 0.075, rate3 = 0.05, rate4 = 0.03, rate5 = 0.014, rate6 = 0.01;
long reward = 0;
System.out.println("请输入利润:");
Scanner sc = new Scanner(System.in);
long money = sc.nextLong();
if (money >= 0 && money <= 100000) {
reward = (long) (money * rate1);
} else if (money <= 200000) {
reward = (long) (100000 * rate1 + (money - 100000) * rate2);
} else if (money <= 400000) {
reward = (long) (100000 * rate1 + 100000 * rate2 + (money - 200000) * rate3);
} else if (money <= 600000) {
reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3
+ (money - 400000) * rate4);
} else if (money <= 1000000) {
reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3 + 200000
* rate4 + (money - 6000000) * rate5);
} else {
reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3 + 200000
* rate4 + 400000 * rate5 + (money - 1000000) * rate6);
}
System.out.println("奖金为:"+reward);
}
}


【程序13】FindNumber.java

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

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

package cn.edu.hit;
/**
* 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
* 1.程序分析:在10万以内判断,先将该数加上100后再开方,
* 再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
* 请看具体分析:
* @author tonylp
*/
public class findNumber {
public static void main(String[] args) {
for(int i=0;i<100000;i++){
if((Math.sqrt(i+100)%1==0)&&(Math.sqrt(i+168)%1 == 0)){
System.out.println(i);
}
}
}
}


【程序14】 TestDay.java

    题目:输入某年某月某日,判断这一天是这一年的第几天?

    1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

package cn.edu.hit;

import java.util.Scanner;

/**
* 题目:输入某年某月某日,判断这一天是这一年的第几天? 1.程序分析:以3月5日为例,应该先把前两个月的加起来, 然后再加上5天即本年的第几天,
* 特殊情况,闰年且输入月份大于3时需考虑多加一天。
*
* @author tonylp
*/
public class testDay {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
System.out.println("请输入月份:");
int month = sc.nextInt();
System.out.println("请输入天数:");
int day = sc.nextInt();
int date = 0;
int arr[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
arr[1] = 29;
}
for (int i = 0; i < month - 1; i++) {
date += arr[i];
}
date += day;
System.out.println("天数为:" + date);
}

}


【程序15】TestCompare.java

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

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

package cn.edu.hit;

import java.util.Scanner;

/**
* 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
* 1.程序分析:我们想办法把最小的数放到x上,
* 先将x与y进行比较,如果x>y则将x与y的值进行交换,
* 然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
* @author tonylp
*/
public class testCompare {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入x:");
int x = sc.nextInt();
System.out.println("请输入y:");
int y = sc.nextInt();
System.out.println("请输入z:");
int z = sc.nextInt();
int temp;
if(x>y){
temp = x;
x = y;
y= temp;
}
if(x>z){
temp = x;
x = z;
z = temp;
}
if(y>z){
temp = y;
y = z;
z = temp;
}
System.out.println("从小到大的顺序为:"+x+"<"+y+"<"+z);
}

}


【程序16】Nine.java

    题目:输出9*9口诀。

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

package cn.edu.hit;
/**
* 题目:输出9*9口诀。
* 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
* @author tonylp
*/
public class nine {
public static void main(String[] args) {
for(int i = 1;i<=9;i++){
for(int j = 1;j<=i;j++){
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println("");
}
}

}


【程序17】MonkeyEatPeach.java

    题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天     剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

    1.程序分析:采取逆向思维的方法,从后往前推断。

package cn.edu.hit;
/**
* 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
*  第二天早上又将剩下的桃子吃掉一半,又多吃了一个
*  。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
* 1.程序分析:采取逆向思维的方法,从后往前推断。
* @author tonylp
*/
public class monkeyEatPeach {
public static void main(String[] args) {
int peach = 1;
for(int i =0; i<10;i++){
peach = (peach+1)*2;
}
System.out.println(peach);
}
}


【程序19】LingXing.java

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

    *

    ***

    ******

    ********

     ******

     ***

     *

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

package cn.edu.hit;

/**
* 题目:打印出如下图案(菱形) * *** ****** ******** ****** *** *
* 1.程序分析:先把图形分成两部分来看待,前四行一个规律, 后三行一个规律,利用双重 for循环, 第一层控制行,第二层控制列。
*
* @author tonylp
*/
public class lingXing {
public static void main(String[] args) {
int[] arr = { 1, 3, 6, 8, 6, 3, 1 };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < arr[i]; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 4; i < arr.length; i++) {
for (int j = arr[i]; j < 8; j++) {
System.out.print(" ");
}
for (int j = 0; j < arr[i]; j++) {
System.out.print("*");
}
System.out.println("");
}
}

}


【程序20】TestAdd2.java

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

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

package cn.edu.hit;

/**
* 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。 1.程序分析:请抓住分子与分母的变化规律。
*
* @author tonylp
*/
public class testAdd2 {
public static void main(String[] args) {
int[] a = new int[20];
int[] b = new int[20];
double sum = 0.0;
a[0] = 2;
a[1] = 3;
b[0] = 1;
b[1] = 2;
for(int i = 2;i<20;i++){
a[i] = a[i-1]+a[i-2];
}
for(int j =2;j<20;j++){
b[j] = b[j-1]+b[j-2];
}
for(int i =0;i<20;i++){
sum+=a[i]/b[i];
}
System.out.println(sum);
}

}


【程序21】TestJieCheng.java

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

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

package cn.edu.hit;
/**
* 题目:求1+2!+3!+...+20!的和
* 1.程序分析:此程序只是把累加变成了累乘。
* @author tonylp
*/
public class testJieCheng {
public static void main(String[] args) {
int sum = 0;
int data = 1;
for(int i = 1;i<=20;i++){
data = data*i;
sum += data;
}
System.out.println(sum);
}
}


【程序22】

    题目:利用递归方法求5!。 TestJieCheng.java

    1.程序分析:递归公式:fn=fn_1*4!

package cn.edu.hit;
/**
* 题目:利用递归方法求5!
* 1.程序分析:递归公式:fn=fn_1*4!
* @author tonylp
*/
public class testDiGuiJieCheng {
public static void main(String[] args) {
int sum = jieCheng(5);
System.out.println(sum);
}
public static int jieCheng(int n){
if(n>0){
return jieCheng(n-1)*n;
}else{
return  1;
}
}

}


【程序23】TestAge.java

    题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大
    两岁。最后问第一个人,他说是10岁。请问第五个人多大?

    1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。

package cn.edu.hit;
/**
* 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。
* 问第2个人,说比第一个人大两岁。
* 最后问第一个人,他说是10岁。
* 请问第五个人多大?
* 1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。
* 要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。
* @author tonylp
*/
public class testAge {
public static void main(String[] args) {
System.out.println(age(5));
}
public static int age(int n){
int c ;
if(n == 1){
c = 10;
}else{
c = age(n-1)+2;
}
return c;
}
}


【程序24】TestNumber.java

    题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

package cn.edu.hit;
/**
* 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
*
* @author tonylp
*/
import java.util.Scanner;
public class testNumber {
public static void main(String[] args) {
System.out.println("请输入一个正整数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] a = new int[5];
int b = 0;
int n = 0;
for (int i = 4; i >= 0; i--) {
b = (int) Math.pow(10, i);
if(num/b != 0&&i>=n){
n = i+1;             //位数=最高位+1
}
a[i] = num / b;
num = num - a[i]*b;
}
System.out.println("位数:"+n);
for(int j =0;j< n;j++){
if(a[j]!=-1){
System.out.print(a[j]+"\t");
}
}
System.out.println("");
//test();
}
//另一种简单方法
/*
public static void test(){
System.out.println("请输入一个正整数:");
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
int numLength = num.length();
System.out.println("位数:"+numLength);
for(int i = numLength-1; i>=0;i--){
System.out.print(num.charAt(i)+"\t");
}
}
*/
}


【程序27】 SuShu.java

    题目:求100之内的素数

package cn.edu.hit;
/**
* 题目:求100之内的素数
* @author tonylp
*
*/
public class suShu {
public static void main(String[] args) {
int j = 0 ;
int flag = 0;
for(int i=2;i<100;i++){
j = (int) (Math.sqrt(i));
for(int k = 2; k<=j;k++){
if(i%k == 0){
flag = 1;
}
}
if(flag == 0){
System.out.println(i);
}
flag = 0;
}
}
}


【程序33】YangHui.java

    题目:打印出杨辉三角形(要求打印出10行如下图)

    1.程序分析:

     1

     1 1

     1 2 1

     1 3 3 1

     1 4 6 4 1

    1 5 10 10 5 1

package cn.edu.hit;

import java.util.Scanner;

/**
* 题目:打印出杨辉三角形(要求打印出10行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
* 现在打印出的格式不是很好看。
* @author tonylp
*
*/
public class yanghui {
public static void main(String[] args) {
System.out.println("请输入行数:");
Scanner sc = new Scanner(System.in);
int line = sc.nextInt();
int[] a = new int[line];
for (int i = 0; i < line; i++) {
a[i] = 1;
}
if (line == 1) {
System.out.println(1);
} else if (line == 2) {
System.out.println(1);
System.out.println(1 + "\t" + 1);
} else {
System.out.println(1);
System.out.println(1 + "\t" + 1);
for (int i = 1; i < line-1; i++) {
for (int j = i; j >= 1; j--) {
a[j] = a[j] + a[j - 1];
}
for(int k =0;k<i+2;k++){
System.out.print(a[k]+"\t");
}
System.out.println();
}
}
}
}


【程序37】 Test3Quit.java

    题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

package cn.edu.hit;

import java.util.Scanner;

/**
* 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子, 问最后留下 的是原来第几号的那位。
*
* @author tonylp
*
*/
public class test3Quit {
public static void main(String[] args) {
System.out.println("请输入n:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int
;
int i = 0;
int t = 0;
while (n > 1) {
if (a[i] == 0) {
t++;
if (t == 3) {
t = 0;
a[i] = 1;
n--;
}
}
i++;
if(i == a.length){
i = 0;
}
}
for(int j=0;j<a.length;j++){
if(a[j]!=1){
System.out.println(j+1);
}
}
}
}


【程序41】 MonkeyPeach.java

    题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一

    个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中

    ,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

package cn.edu.hit;

/**
* 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一
* 个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中 ,
* 拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
*
* @author tonylp
*
*/
public class monkeyPeach {
public static void main(String[] args) {
int monkey = 0;
int a =getPeach(monkey);
System.out.println(a);
}
public static int getPeach(int monkey) {
if (monkey <5) {
return (getPeach(monkey+1)*5+1);
}else{
return 1;
}
}
}


【程序44】 TestEven.java

    题目:一个偶数总能表示为两个素数之和。

package cn.edu.hit;

import java.util.Scanner;

/**
* 题目:一个偶数总能表示为两个素数之和。
*
* @author tonylp
*/
public class testEven {
public static void main(String[] args) {
System.out.println("请输入一个偶数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int j = 0;
int num2 = 0;
int flag = 0;
int tag = 0;
int temp = 0;
if (num % 2 != 0 || num == 0) {
System.out.println("输入数据错误!");
} else {
for (int i = 3; i < num; i++) {
j = (int) Math.sqrt(i);
for (int k = 2; k <= j; k++) {
if (i % k == 0) {
flag = 1;
}
}
if (flag == 0) {
num2 = num - i;
temp = (int) Math.sqrt(num2);
for (int k = 2; k <= temp; k++) {
if (num2 % k == 0) {
tag = 1;
}
}
if (tag == 0&&num2>=3) {
System.out.println(num + "=" + i + "+" + num2);
}
tag = 0;
}
flag = 0;
}
}
}
}


原创链接: http://www.cnblogs.com/tonylp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: