程序 21 ? 题目: 求 1+2!+3!+...+20! 的和 ?
1. 程序分析: 此程序只是把累加变成了累乘 ?
public class Ex21 {
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! ?
import java.util.Scanner;
public class Ex22 {
public static void main(String[] args) {
Scanner s = new Scanner(System. in );
int n = s.nextInt();
Ex22 tfr = new Ex22();
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;
}
}
程序 23 ? 题目: 有 5 个人坐在一起, 问第五个人多少岁? 他说比第 4 个人大 2 岁问第 4 个人岁数, 他说比第 3 个人大 2 岁问第三个人, 又说比第 2 人大两岁问第 2 个人, 说比第一个人大两岁最后问第一个人, 他说是 10 岁请问第五个人多大? ?
1. 程序分析: 利用递归的方法, 递归分为回推和递推两个阶段要想知道第五个人岁数, 需知道第四人的岁数, 依次类推, 推到第一人 (10 岁), 再往回推 ?
public class Ex23 {
static int getAge(int n) {
if (n == 1) {
return 10;
}
return 2 + getAge(n - 1);
}
public static void main(String[] args) {
System.out.println("第五个的年龄为:" + getAge(5));
}
}
程序 24 ? 题目: 给一个不多于 5 位的正整数, 要求: 一求它是几位数, 二逆序打印出各位数字 ?
import java.util.Scanner;
public class Ex24 {
public static void main(String[] args) {
Ex24 tn = new Ex24();
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]);
}
}
}
程序 25 ? 题目: 一个 5 位数, 判断它是不是回文数即 12321 是回文数, 个位与万位相同, 十位与千位相同 ?
import java.util.Scanner;
public class Ex25 {
static int[] a = new int[5];
static int[] b = new int[5];
public static void main(String[] args) {
boolean is = false;
Scanner s = new Scanner(System. in );
long l = s.nextLong();
if (l > 99999 || l < 10000) {
System.out.println("Input error, please input again!");
l = s.nextLong();
}
for (int i = 4; i >= 0; i--) {
a[i] = (int)(l / (long) Math.pow(10, i));
l = (l % (long) Math.pow(10, i));
}
System.out.println();
for (int i = 0, j = 0; i < 5; i++, j++) {
b[j] = a[i];
}
for (int i = 0, j = 4; i < 5; i++, j--) {
if (a[i] != b[j]) {
is = false;
break;
} else {
is = true;
}
}
if (is == false) {
System.out.println("is not a Palindrom!");
} else if (is == true) {
System.out.println("is a Palindrom!");
}
}
}
程序 26 ? 题目: 请输入星期几的第一个字母来判断一下是星期几, 如果第一个字母一样, 则继续 ? 判断第二个字母 ?
1. 程序分析: 用情况语句比较好, 如果第一个字母一样, 则判断用情况语句或 if 语句判断第二个字母 ?
import java.util.Scanner;
public class Ex26 {
public static void main(String[] args) {
// 保存用户输入的第二个字母
char weekSecond;
// 将 Scanner 类示例化为 input 对象, 用于接收用户输入
Scanner input = new Scanner(System. in );
// 开始提示并接收用户控制台输入
System.out.print("请输入星期值英文的第一个字母, 我来帮您判断是星期几:");
String letter = input.next();
// 判断用户控制台输入字符串长度是否是一个字母
if (letter.length() == 1) {
// 利用取第一个索引位的字符来实现让 Scanner 接收 char 类型输入
char weekFirst = letter.charAt(0);
switch (weekFirst) {
case m:
// 当输入小写字母时, 利用 switch 结构特性执行下一个带 break 语句的 case 分支, 以实现忽略用户控制台输入大小写敏感的功能
case M:
System.out.println("星期一 (Monday)");
break;
case t:
// 当输入小写字母时, 利用 switch 结构特性执行下一个带 break 语句的 case 分支, 以实现忽略用户控制台输入大小写敏感的功能
case T:
System.out.print("由于星期二 (Tuesday) 与星期四 (Thursday) 均以字母 T 开头, 故需输入第二个字母才能正确判断:");
letter = input.next();
// 判断用户控制台输入字符串长度是否是一个字母
if (letter.length() == 1) {
// 利用取第一个索引位的字符来实现让 Scanner 接收 char 类型输入
weekSecond = letter.charAt(0);
// 利用或 (||) 运算符来实现忽略用户控制台输入大小写敏感的功能
if (weekSecond == U || weekSecond == u) {
System.out.println("星期二 (Tuesday)");
break;
// 利用或 (||) 运算符来实现忽略用户控制台输入大小写敏感的功能
} else if (weekSecond == H || weekSecond == h) {
System.out.println("星期四 (Thursday)");
break;
// 控制台错误提示
} else {
System.out.println("输入错误, 不能识别的星期值第二个字母, 程序结束!");
break;
}
} else {
// 控制台错误提示
System.out.println("输入错误, 只能输入一个字母, 程序结束!");
break;
}
case w:
// 当输入小写字母时, 利用 switch 结构特性执行下一个带 break 语句的 case 分支, 以实现忽略用户控制台输入大小写敏感的功能
case W:
System.out.println("星期三 (Wednesday)");
break;
case f:
// 当输入小写字母时, 利用 switch 结构特性执行下一个带 break 语句的 case 分支, 以实现忽略用户控制台输入大小写敏感的功能
case F:
System.out.println("星期五 (Friday)");
break;
case s:
// 当输入小写字母时, 利用 switch 结构特性执行下一个带 break 语句的 case 分支, 以实现忽略用户控制台输入大小写敏感的功能
case S:
System.out.print("由于星期六 (Saturday) 与星期日 (Sunday) 均以字母 S 开头, 故需输入第二个字母才能正确判断:");
letter = input.next();
// 判断用户控制台输入字符串长度是否是一个字母
if (letter.length() == 1) {
// 利用取第一个索引位的字符来实现让 Scanner 接收 char 类型输入
weekSecond = letter.charAt(0);
// 利用或 (||) 运算符来实现忽略用户控制台输入大小写敏感的功能
if (weekSecond == A || weekSecond == a) {
System.out.println("星期六 (Saturday)");
break;
// 利用或 (||) 运算符来实现忽略用户控制台输入大小写敏感的功能
} else if (weekSecond == U || weekSecond == u) {
System.out.println("星期日 (Sunday)");
break;
// 控制台错误提示
} else {
System.out.println("输入错误, 不能识别的星期值第二个字母, 程序结束!");
break;
}
} else {
// 控制台错误提示
System.out.println("输入错误, 只能输入一个字母, 程序结束!");
break;
}
default:
// 控制台错误提示
System.out.println("输入错误, 不能识别的星期值第一个字母, 程序结束!");
break;
}
} else {
// 控制台错误提示
System.out.println("输入错误, 只能输入一个字母, 程序结束!");
}
}
}
程序 27 ? 题目: 求 100 之内的素数 ?
public class Ex27 {
public static void main(String args[])
{
int sum,i;
for(sum=2;sum<=100;sum++)
{
for(i=2;i<=sum/2;i++)
{
if(sum%i==0)
break;
}
if(i>sum/2)
System.out.println(sum+"是素数");
}
}
}
程序 28 ? 题目: 对 10 个数进行排序 ?
1. 程序分析: 可以利用选择法, 即从后 9 个比较过程中, 选择一个最小的与第一个元素交换, ? 下次类推, 即用第二个元素与后 8 个进行比较, 并进行交换 ?
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Ex28 {
public static void main(String[] args) {
int arr[] = new int[11];
Random r = new Random();
for (int i = 0; i < 10; i++) {
arr[i] = r.nextInt(100) + 1; // 得到 10 个 100 以内的整数
}
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.print("\nPlease Input a int number:");
Scanner sc = new Scanner(System. in );
arr[10] = sc.nextInt(); // 输入一个 int 值
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
程序 29 ? 题目: 求一个 3*3 矩阵对角线元素之和 ?
1. 程序分析: 利用双重 for 循环控制输入二维数组, 再将 a[i][i] 累加后输出 ?
public class Ex29 {
public static void main(String[] args){
double sum=0;
int array[][]={{1,2,3},{4,5, 6},{7,7,8}};
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
if(i==j)
sum=sum + array[i][j];
}
System.out.println( sum);
}
}
程序 30 ? 题目: 有一个已经排好序的数组现输入一个数, 要求按原来的规律将它插入数组中 ?
1. ? 程序分析: 首先判断此数是否大于最后一个数, 然后再考虑插入中间的数的情况, 插入后此元素之后的数, 依次后移一个位置 ?
import java.util.Random;
public class ArraySort {
public static void main(String[] args)
{ int temp=0;
int myarr[] = new int[12];
Random r=new Random();
for(int i=1;i<=10;i++)
myarr[i]=r.nextInt(1000);
for (int k=1;k<=10;k++)
System.out.print(myarr[k]+",");
for(int i=1;i<=9;i++)
for(int k=i+1;k<=10;k++)
if(myarr[i]>myarr[k])
{
temp=myarr[i];
myarr[i]=myarr[k];
myarr[k]=temp;
}
System.out.println("");
for (int k=1;k<=10;k++)
System.out.print(myarr[k]+",");
myarr[11]=r.nextInt(1000);
for(int k=1;k<=10;k++)
if(myarr[k]>myarr[11])
{
temp=myarr[11];
for(int j=11;j>=k+1;j--)
myarr[j]=myarr[j-1];
myarr[k]=temp;
}
System.out.println("");
for (int k=1;k<=11;k++)
System.out.print(myarr[k]+",");
}
}
程序 31 ? 题目: 将一个数组逆序输出 ?
程序分析: 用第一个与最后一个交换 ?
其实, 用循环控制变量更简单:
for(int k=11;k>=1;k--)
System.out.print(myarr[k]+",");
程序 32 ? 题目: 取一个整数 a 从右端开始的 4~7 位 ?
程序分析: 可以这样考虑: ?
(1) 先使 a 右移 4 位 ?
(2) 设置一个低 4 位全为 1, 其余全为 0 的数可用~(~0 < <4) ?
(3) 将上面二者进行 & 运算 ?
public class Ex32 {
public static void main(String[] args) {
int a = 0;
long b = 18745678;
a = (int) Math.floor(b % Math.pow(10, 7) / Math.pow(10, 3));
System.out.println(a);
}
}
程序 33 ?
题目: 打印出杨辉三角形 (要求打印出 10 行如下图) ?
1. 程序分析: ?
1 ?
1 ? 1 ?
1 ? 2 ? 1 ?
1 ? 3 ? 3 ? 1 ?
1 ? 4 ? 6 ? 4 ? 1 ?
1 ? 5 ? 10 ? 10 ? 5 ? 1 ?
public class Ex33 {
public static void main(String args[]){
int i,j;
int a[][];
a=new int[8][8];
for(i=0;i<8;i++){
a[i][i]=1;
a[i][0]=1;
}
for(i=2;i<8;i++){
for(j=1;j<=i-1;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for(i=0;i<8;i++){
for(j=0;j<i;j++){
System.out.printf(" "+a[i][j]);
}
System.out.println();
}
}
}
程序 34 ? 题目: 输入 3 个数 a,b,c, 按大小顺序输出 ?
1. 程序分析: 利用指针方法 ?
public class Ex34 {
public static void main(String[] args) {
int[] arrays = {
800,
56,
500
};
for (int i = arrays.length; --i >= 0;) {
for (int j = 0; j < i; j++) {
if (arrays[j] > arrays[j + 1]) {
int temp = arrays[j];
arrays[j] = arrays[j + 1];
arrays[j + 1] = temp;
}
}
}
for (int n = 0; n < arrays.length; n++) System.out.println(arrays[n]);
}
}
程序 35 ? 题目: 输入数组, 最大的与第一个元素交换, 最小的与最后一个元素交换, 输出数组 ?
import java.util. * ;
public class Ex35 {
public static void main(String[] args) {
int i,
min,
max,
n,
temp1,
temp2;
int a[];
System.out.println("输入数组的长度:");
Scanner keyboard = new Scanner(System. in );
n = keyboard.nextInt();
a = new int[n];
for (i = 0; i < n; i++) {
System.out.print("输入第" + (i + 1) + "个数据");
a[i] = keyboard.nextInt();
}
// 以上是输入整个数组
max = 0;
min = 0;
// 设置两个标志, 开始都指向第一个数
for (i = 1; i < n; i++) {
if (a[i] > a[max]) max = i; // 遍历数组, 如果大于 a[max], 就把他的数组下标赋给 max
if (a[i] < a[min]) min = i; // 同上, 如果小于 a[min], 就把他的数组下标赋给 min
}
// 以上 for 循环找到最大值和最小值, max 是最大值的下标, min 是最小值的下标
temp1 = a[0];
temp2 = a[min]; // 这两个 temp 只是为了在交换时使用
a[0] = a[max];
a[max] = temp1; // 首先交换 a[0] 和最大值 a[max]
if (min != 0) { // 如果最小值不是 a[0], 执行下面
a[min] = a[n - 1];
a[n - 1] = temp2; // 交换 a[min] 和 a[n-1]
} else { // 如果最小值是 a[0], 执行下面
a[max] = a[n - 1];
a[n - 1] = temp1;
}
for (i = 0; i < n; i++) { // 输出数组
System.out.print(a[i] + " ");
}
}
}
程序 36 ? 题目: 有 n 个整数, 使其前面各数顺序向后移 m 个位置, 最后 m 个数变成最前面的 m 个数 ?
程序 37 ?
题目: 有 n 个人围成一圈, 顺序排号从第一个人开始报数 (从 1 到 3 报数), 凡报到 3 的人退出圈子, 问最后留下的是原来第几号的那位 ?
import java.util.Scanner;
public class Ex37 {
public static void main(String[] args) {
Scanner s = new Scanner(System. in );
int n = s.nextInt();
boolean[] arr = new boolean[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = true; // 下标为 TRUE 时说明还在圈里
}
int leftCount = n;
int countNum = 0;
int index = 0;
while (leftCount > 1) {
if (arr[index] == true) { // 当在圈里时
countNum++; // 报数递加
if (countNum == 3) { // 报道 3 时
countNum = 0; // 从零开始继续报数
arr[index] = false; // 此人退出圈子
leftCount--; // 剩余人数减一
}
}
index++; // 每报一次数, 下标加一
if (index == n) { // 是循环数数, 当下标大于 n 时, 说明已经数了一圈,
index = 0; // 将下标设为零重新开始
}
}
for (int i = 0; i < n; i++) {
if (arr[i] == true) {
System.out.println(i);
}
}
}
}
程序 38 ?
题目: 写一个函数, 求一个字符串的长度, 在 main 函数中输入字符串, 并输出其长度 ?
import java.util.Scanner;
public class Ex38 {
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
System.out.println("请输入一个字符串");
String mys= s.next();
System.out.println(str_len(mys));
}
public static int str_len(String x)
{
return x.length();
}
}
?
题目: 编写一个函数, 输入 n 为偶数时, 调用函数求 1/2+1/4+...+1/n, 当输入 n 为奇数时, 调用函数 1/1+1/3+...+1/n
程序 39?
题目: 字符串排序 ?
import java.util. * ;
public class test {
public static void main(String[] args) {
ArrayList < String > list = new ArrayList < String > ();
list.add("010101");
list.add("010003");
list.add("010201");
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
程序 40 ?
题目: 海滩上有一堆桃子, 五只猴子来分第一只猴子把这堆桃子凭据分为五份, 多了一个, 这只猴子把多的一个扔入海中, 拿走了一份第二只猴子把剩下的桃子又平均分成五份, 又多了一个, 它同样把多的一个扔入海中, 拿走了一份, 第三第四第五只猴子都是这样做的, 问海滩上原来最少有多少个桃子? ?
public class Dg {
static int ts = 0; // 桃子总数
int fs = 1; // 记录分的次数
static int hs = 5; // 猴子数...
int tsscope = 5000; // 桃子数的取值范围. 太大容易溢出.
public int fT(int t) {
if (t == tsscope) {
// 当桃子数到了最大的取值范围时取消递归
System.out.println("结束");
return 0;
} else {
if ((t - 1) % hs == 0 && fs <= hs) {
if (fs == hs) {
System.out.println("桃子数 =" + ts + "时满足分桃条件");
}
fs += 1;
return fT((t - 1) / 5 * 4); // 返回猴子拿走一份后的剩下的总数
} else {
// 没满足条件
fs = 1; // 分的次数重置为 1
return fT(ts += 1); // 桃子数加 + 1
}
}
}
public static void main(String[] args) {
new Dg().fT(0);
}
}
程序 41
java 排序算法的比较
import java.util.*;
import java.io.*;
public class SortAlgorithm
{
static Random rand = new Random();
void bubbleSort(int[] numlist) // 冒泡排序算法
{
int temp;
for(int j=1;j<numlist.length;j++)
for(int i=0;i<numlist.length-j;i++)
if(numlist>numlist[i+1])
{
temp = numlist[i+1];
numlist[i+1] = numlist;
numlist = temp;
}
}
void selectionSort (int[] numlist) // 选择排序算法
{
int temp;
for(int i=0;i<numlist.length-1;i++)
for(int j=i+1;j<numlist.length;j++)
if(numlist>numlist[j])
{
temp = numlist[j];
numlist[j] = numlist;
numlist = temp;
}
}
void insertSort (int[] numlist) // 插入排序算法
{
int temp,in,out;
for(out=1;out<numlist.length;out++)
{
temp=numlist[out];
in=out;
while(in>0 && numlist[in-1]>=temp)
{
numlist[in]=numlist[in-1];
--in;
}
numlist[in]=temp;
}
}
void display (int[] num) // 打印出排序结果
{
for(int i = 0;i<num.length;i++)
System.out.print(num+" ");
System.out.println("");
}
static int pRand(int mod) // 生成随即数组
{
return Math.abs(rand.nextInt())%mod;
}
public static void main(String args[])throws IOException
{
SortAlgorithm sortAlgorithm = new SortAlgorithm();
int[] numList = new int[10];
for(int i = 0;i<numList.length;i++)
numList = pRand(100); // 调用 pRand 方法, 把随即生成的数据输入到
// 数组中
System.out.println("随即生成的数组是:");
// 打印出原数组,
for(int j =0;j<numList.length;j++)
System.out.print(numList[j]+" ");
System.out.println("");
long begin = System.currentTimeMillis(); // 排序开始时间, 调用系统的当前时间
sortAlgorithm.bubbleSort(numList); // 执行冒泡排序
long end = System.currentTimeMillis(); // 排序结束时间, 调用系统当前时间
System.out.println("冒泡排序用时为:" + (end-begin)); // 排序用时
System.out.println("排序后的数组为:");
sortAlgorithm.display(numList);
begin = System.currentTimeMillis();
sortAlgorithm.selectionSort(numList);
end = System.currentTimeMillis();
System.out.println("选择排序用时为:" +(end-begin));
System.out.println("排序后的数组为:");
sortAlgorithm.display(numList);
begin = System.currentTimeMillis();
sortAlgorithm.insertSort(numList);
end = System.currentTimeMillis();
System.out.println("插入排序用时为:" + (end-begin));
System.out.println("排序后的数组为:");
sortAlgorithm.display(numList);
}
}
程序 42
题目如下: 用 122345 这六个数字, 用 java 写一个 main 函数, 打印出所有不同的排列, 如: 512234412345 等, 要求:"4" 不能在第三位,"3" 与 "5" 不能相连
static int[] bits = new int[] { 1, 2, 3, 4, 5 };
/**
* @param args
*/
public static void main(String[] args) {
sort("", bits);
}
private static void sort(String prefix, int[] a) {
if (a.length == 1) {
System.out.println(prefix + a[0]);
}
for (int i = 0; i < a.length; i++) {
sort(prefix + a, copy(a, i));
}
}
private static int[] copy(int[] a,int index){
int[] b = new int[a.length-1];
System.arraycopy(a, 0, b, 0, index);
System.arraycopy(a, index+1, b, index, a.length-index-1);
return b;
}
JAVA 经典算法 40 题 (下)
来源: http://www.bubuko.com/infodetail-2483671.html