8.1、求最大值算法
package study.arithmetic;
/**
*求最大数值
*我们有一串随机数列。我们的目的是找到这个数列中最大的数。如果将数列中的每一个数字看成是一颗豆子的大小,可以将下面的算法形象地称为“捡豆子”:
*首先将第一颗豆子放入口袋中。
*从第二颗豆子开始检查,如果正在检查的豆子比口袋中的还大,则将它捡起放入口袋中,同时丢掉原先口袋中的豆子。反之则继续下一颗豆子。直到最后一颗豆子。
*最后口袋中的豆子就是所有的豆子中最大的一颗。
*/
public class Max {
public static int max(int array[]){
int mval = array[0];
for (int i = 0; i < array.length; i++){
if (array[i] > mval){
mval = array[i];
}
}
return mval;
}
}
8.2、求最大公约数
package study.arithmetic;
public class Gcb {
private static int gcb(int a,int b){
if(a%b==0){
return gcb(b,a%b);
}
return b;
}
public static void main(String args[]){
System.out.println(gcb(110,99));
}
}
8.3、兔子问题
package study.arithmetic;
/**
* 题目:古典问题
* 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
* 程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
* @author TianYou
*/
public class CalculateRabbit {
private static long calculateRabbit(int month){
long rabbitOne = 1L;
long rabbitTwo = 1L;
long rabbit;
for(int i=3; i rabbit = rabbitTwo;
rabbitTwo = rabbitOne + rabbitTwo;
rabbitOne = rabbit;
}
return rabbitTwo;
}
public static void main(String args[]){
System.out.println(calculateRabbit(20));
}
}
8.4、猴子吃桃子
package study.arithmetic;
/**
* 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,
* 又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
* 以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。
* 求第一天共摘了多少。
* @author TianYou
*/
public class Monkey {
private static int MonkeyPeach(int size){
int lastdayNum = 1;
for(int i=2; i<=size; i++) {
lastdayNum = (lastdayNum+1) * 2;
}
return lastdayNum;
}
public static void main(String args[]){
System.out.println(MonkeyPeach(100));
}
}
8.5、乒乓球比赛
package study.arithmetic;
/***
* 两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。
* 已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
* @author TianYou
*/
public class EighteenthPingpang {
private static void pingpang(char[] m,char[] n){
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')) {
continue;
} else {
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}
public static void main(String[] args) {
char[] m = { 'a', 'b', 'c' };
char[] n = { 'x', 'y', 'z' };
pingpang(m,n);
}
}
8.6、数列求和
package study.arithmetic;
import java.text.DecimalFormat;
/**
* 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
* @author TianYou
*
*/
public class TwentiethFractionSum {
private static void count(){
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));
}
}
public static void main(String args[]){
count();
}
}
8.7、2的问题
package study.arithmetic;
/**
* 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
* 关键是计算出每一项的值
* @author TianYou
*/
public class Sumloop {
public static void main(String args[]) {
count(5d);
}
private static void count(double input) {
double s = 0;
double output = 0;
for(double i =1;i<=input;i++) {
output += input;
double a = output;
s+=a;
}
System.out.println(s);
}
}
8.8、自由落体球
package study.arithmetic;
/**
* 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
* @author TianYou
*
*/
public class Ex10 {
public static void count(){
double s=0;
double t=100;
for(int i=1;i<=10;i++){
s+=t;
t=t/2;
}
System.out.println(s);
System.out.println(t);
}
public static void main(String args[]){
count();
}
}
8.9、累乘
package study.arithmetic;
/**
* 题目:求1+2!+3!+...+20!的和
* @author TianYou
*/
public class Ex21 {
public static void count(){
long sum = 0;
long fac = 1;
for(int i=1; i<=10; i++) {
fac = fac * i;
sum += fac;
}
System.out.println(sum);
}
public static void main(String args[]){
count();
}
}
8.10、递归求5!
package study.arithmetic;
/**
* 递归公式:fn=fn_1*4!
* @author TianYou
*
*/
public class Ex22 {
public static void main(String args[]){
recursion(5);
}
public static 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;
}
}
9、资料分享