【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
package arithmetic;
public class FirstRabbit {
public static final int MONTH = 15;
public static void main(String[] args) {
Long f1 = 1L, f2 = 1L, f;
for (int i = 3; i <= MONTH; i++) {
f = f2;
f2 += f1;
f1 = f;
System.out.println("第" + i + "个月的兔子数为:" + f2);
}
}
}
运行结果如下:
【程序2】
题目:判断101-200之间有多少个素数,并输出所有素数。
概念:质数又称素数。
指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数(不包括0)整除的数。
判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。为什么要sqrt?比如判断了100/2,就没必要判断100/50,依次类推。
package arithmetic;
public class SecondPrimeNumber {
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);
}
}
运行结果如下:
【程序3】
题目:打印出所有的"水仙花数(narcissus number)",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
package arithmetic;
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);
}
}
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);
}
}
}
运行结果如下:
【程序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的值,重复执行第一步。
package arithmetic;
import java.util.Scanner;
public class FourthPrimeFactor {
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);
}
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;
}
}
}
}
运行结果如下:
【程序5】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。
package arithmetic;
import java.util.Scanner;
public class FifthCondition {
public static void main(String[] args) {
Scanner str = new Scanner(System.in);
int s = str.nextInt();
FifthCondition fc = new FifthCondition();
char grade = fc.compare(s);
System.out.println(grade);
}
public char compare(int s) {
return s > 90 ? 'A' : s > 60 ? 'B' : 'C';
}
}
运行结果如下是:
【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
package arithmetic;
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);
}
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】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
程序分析:利用while语句,条件为输入的字符不为 '\n '。
package arithmetic;
import java.util.*;
public class SeventhCharacterStatistics {
static int digital = 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++;
}
}
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 的值。
package arithmetic;
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 += a;
sum += b;
a = a * 10;
++i;
System.out.print(b+",");
}
System.out.println("\ninput number: " + n);
System.out.println(sum);
}
}
运行结果如下:
【程序9】
题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。
例如 6=1+2+3.编程 找出1000以内的所有完数。
程序分析:一个整数的因子是能够整除它的整数。不过我们一般考虑的是正整数,比如8的因子有1,2,4和8。如果除去这个整数本身就是真因子了。
package arithmetic;
public class NinthWanshu {
public static void main(String[] args) {
System.out.println("1到1000的完数有: ");
for (int i = 1; i < 1000; i++) {
int t = 0;
// 真因子,排除掉本身,所以要除以2
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次反弹多高?
package arithmetic;
public class TenthTreeFall {
static double height = 100;
static double distance = 100; //弹回50+落地50的距离
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
distance += height;
height = height / 2;
}
System.out.println("路程:" + distance);
System.out.println("高度:" + height / 2);
}
}
运行结果如下: