【程式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);
}
}