JAVA二十二道經(jīng)典編程題

一.打印水仙花數(shù):所謂“水仙花數(shù)”是指一個三位數(shù),其各位數(shù)字立方和等于該數(shù)本身

int a, b, c;

for(int i = 1; i <1000; i++){//因為水仙花數(shù)是 一個三位數(shù),所以循環(huán)體范圍控制在1000以內(nèi)

a = i % 10;//用求余法取個位數(shù)

b = i%100/10;//取十位數(shù)上的值

c = i /100;//除以100取百位上的值,因為c是int類型

if(i == a*a*a + b*b*b + c*c*c){

System.out.print(i+" ");

}

}

二打印一個實心菱形圖案

for(int i =1; i <=4; i++){//把菱形圖案分成上下兩部分,上四下三,i控制上部分的行數(shù)循環(huán)1,2,3,4

for(int j= 1; j <=4-i; j++){//j被定義來控制*前面的空格數(shù)量

System.out.print(" ");

}

for(int k = 1; k <= 2*i - 1; k++){//k被定義來打印每行*的數(shù)量

System.out.print("*");

}

System.out.println("");

}

for(int i = 1; i <=3; i++){//i在這里控制下部分菱形循環(huán)的行數(shù)

for(int j = 1; j <= i;j++){//j來控制下部分*前面的空格

System.out.print(" ");

}

for(int k = 5; k >=2*i-1; k--){//k被定義來打印每行*的數(shù)量

System.out.print("*");

}

System.out.println("");

}

三從鍵盤接收一個名次(int) 使用switch-case作如下分支

名次為 1 獎IPHONE8

名次為 2 獎IPHONE7

名次為 3 獎IPHONE6

名次為 4 獎IPHONE5

其它名次 努力吧,少年

System.out.println("請輸入你的名次:");

Scanner scanner = new Scanner(System.in);//創(chuàng)建一個對象

//number 前面的數(shù)據(jù)類型選擇取決于后面next獲取到的返回值

int number = scanner.nextInt();// next()獲取一個字符串 nextInt()獲取一個整數(shù) nextFloat() 獲取一個浮點型

switch(number){

case 1:

? System.out.println("名次為1,獎勵一臺蘋果8");

? break;

case 2:

? System.out.println("名次為2,獎勵一臺蘋果7");

? break;

case 3:

? System.out.println("名次為3,獎勵一臺蘋果6");

? break;

case 4:

? System.out.println("名次為4,獎勵一臺蘋果5");

? break;

default:

? System.out.println("努力吧,小伙子!");

? break;

}

四.判斷給定的某個年份是否是閏年。

閏年的判斷規(guī)則如下:

(1)若某個年份能被4整除但不能被100整除,則是閏年。

(2)若某個年份能被400整除,則也是閏年。

System.out.println("請輸入年份:");

Scanner scanner = newScanner(System.in);

int year = scanner.nextInt();//用戶自己輸入int類型 的年份

if(year%4 == 0 && year%100 != 0 || year %400 == 0){//判斷條件

System.out.println(year+"年是閏年");

}else{

System.out.println(year+"年不是閏年");

}

五.100以內(nèi)的數(shù)逢7過

System.out.println("逢7必過的數(shù)都在這了:");

int num = 0;

for(int i = 1; i <=100; i++){//控制循環(huán)體的范圍是100

if(i %10 == 7 ||i %7 == 0 || i/10 == 7){//該數(shù)不能有7、不能被7整除

continue;

}else{

System.out.print(i+" ");

num++;

if(num % 10 == 0){

System.out.print("\n");

}

}

}

六.打印九九乘法表

//該題主要訓(xùn)練的是for循環(huán)的運用

int i,j;

for(i = 1; i<=9; i++){//i控制乘法表的行數(shù)

for(j = 1; j<=i; j++){//j控制列數(shù)

System.out.print(j+"*"+i+"="+(i*j)+" ");//計算的算法

}

System.out.println();

}

七.打印2到10000的所有素數(shù),每行顯示8個素數(shù)

//素數(shù)只能被1和自身整除,沒有約數(shù),如2,3,5,7,11,13

int count = 0;

int j;

for(int i = 2; i <= 10000; i++){//因為2是最小的素數(shù),所以從2 開始,循環(huán)范圍在10000以內(nèi)

for(j =2; j <= Math.sqrt(i);j++){//從2開始循環(huán),對i進行開平方

if(i % j == 0){//如果j小于i的開平方,就讓i%j取余,能被整除,不是素數(shù)

? break;

}

}

if(j > Math.sqrt(i)){//不能被整除,判斷j是否大于i的平方根,大于,是素數(shù)

System.out.print(i+" ");

count ++;

if(count % 8 == 0){//一行八個數(shù)

System.out.println("");

}

}

}

八.打印一個空心菱形

???????????? *

??????? *??????? *

*???????????????????? *

???? *?????????? *

?????????? *

public static void main(String [] args){//main方法

for(int i = 1; i <= 4; i++){//把空心菱形分成上下兩部分,i表示上半部分的行數(shù)

for(int j =1; j <=4-i; j++){//j表示*里面需要打印的空格數(shù)

System.out.print(" ");

}

for(int k =1; k<= 2*i-1; k++){//k表示要打印的*數(shù)

if(k == 1 || k == 2*i-1){//如果要打印的*是第一個或者最后一個就打印出來

System.out.print("*");

}else{

System.out.print(" ");//如果不是就打印空格

}

}

System.out.println();

}

for(int h =1; h <=3; h++){//h表示下半部分要打印的行數(shù)

for(int j =1; j <=h; j++){//j表示要*里面要打印的空格數(shù)

System.out.print(" ");

}

for(int k =1; k <=7-2*h; k++){

if(k == 1 || k ==7-2*h){//如果*是第一個或者最后一個就打印出來

System.out.print("*");

}else{

System.out.print(" ");//如果不是就打印空格

}

}

System.out.println();

}

九.有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子對數(shù)為多少?

程序分析:兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21....

public static void main(String[] args){

int n = 10;

System.out.println("第"+n+"個月兔子總數(shù)為"+fun(n));

}

private static int fun(int n){

if(n==1 || n==2)

???return 1;

else

???return fun(n-1)+fun(n-2);

}

}

十.輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。

public static void main(String[] args){

int m,n;

try{

m = Integer.parseInt(args[0]);

n = Integer.parseInt(args[1]);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("輸入有誤");

return;

}

max_min(m,n);

}

//求最大公約數(shù)和最小公倍數(shù)

private static void max_min(int m, int n){

int temp = 1;

int yshu = 1;

int bshu = m*n;

if(n

temp = n;

n = m;

m = temp;

}

while(m!=0){

temp = n%m;

n = m;

m = temp;

}

yshu = n;

bshu /= n;

System.out.println(m+"和"+n+"的最大公約數(shù)為"+yshu);

System.out.println(m+"和"+n+"的最小公倍數(shù)為"+bshu);

}

}

十一.一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為"完數(shù)"。例如6=1+2+3.編程找出1000以內(nèi)的所有完數(shù)。

public static void main(String[] args){

int n = 10000;

compNumber(n);

}

//求完數(shù)

private static void compNumber(int n){

int count = 0;

System.out.println(n+"以內(nèi)的完數(shù):");

for(int i=1;i

int sum = 0;

for(int j=1;j

if((i%j)==0){

sum += j;

if(sum==i){

??System.out.print(i+" ");

??if((count++)%5==0)

????System.out.println();

????}

}

}

}

}

}

十二.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制。

public static void main(String[] args){

System.out.print("求s=a+aa+aaa+aaaa+...的值,請輸入a的值:");

Scanner scan = new Scanner(System.in).useDelimiter("\\s*");//以空格作為分隔符

int a = scan.nextInt();

int n = scan.nextInt();

scan.close();//關(guān)閉掃描器

System.out.println(expressed(2,5)+add(2,5));

}

//求和表達式

private static String expressed(int a,int n){

StringBuffer sb = new StringBuffer();

StringBuffer subSB = new StringBuffer();

for(int i=1;i

??subSB = subSB.append(a);

??sb = sb.append(subSB);

??if(i

????sb = sb.append("+");

}

sb.append("=");

return sb.toString();

}

//求和

private static long add(int a,int n){

long sum = 0;

long subSUM = 0;

for(int i=1;i

subSUM = subSUM*10+a;

sum = sum+subSUM;

}

return sum;

}

}

十三.一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地時,共經(jīng)過多少米?第10次反彈多高?

import java.util.Scanner;

public class Prog10{

public static void main(String[] args){

System.out.print("請輸入小球落地時的高度和求解的次數(shù):");

Scanner scan = new Scanner(System.in).useDelimiter("\\s");

int h = scan.nextInt();

int n = scan.nextInt();

scan.close();

distance(h,n);

}

//小球從h高度落下,經(jīng)n次反彈后經(jīng)過的距離和反彈的高度

private static void distance(int h,int n){

double length = 0;

for(int i=0;i

length += h;

h /=2.0 ;

}

System.out.println("經(jīng)過第"+n+"次反彈后,小球共經(jīng)過"+length+"米,"+"第"+n+"次反彈高度為"+h+"米");

}

}

十四.有1、2、3、4個數(shù)字,能組成多少個互不相同且無重復(fù)數(shù)字的三位數(shù)?都是多少?

程序分析:可填在百位、十位、個位的數(shù)字都是1、2、3、4。組成所有的排列后再去 掉不滿足條件的排列。

public class Prog11{

public static void main(String[] args){

int count = 0;

int n = 0;

for(int i=1;i<5;i++){

for(int j=1;j<5;j++){

if(j==i)

??continue;

for(int k=1;k<5;k++){

if(k!=i && k!=j){

n = i*100+j*10+k;

??System.out.print(n+" ");

??if((++count)%5==0)

??System.out.println();

}

}

}

}

System.out.println();

System.out.println("符合條件的數(shù)共:"+count+"個");

}

}

十五.輸入某年某月某日,判斷這一天是這一年的第幾天?

程序分析:以3月5日為例,應(yīng)該先把前兩個月的加起來,然后再加上5天即本年的第幾天,特殊情況,閏年且輸入月份大于3時需考慮多加一天。

import java.util.Scanner;

public class Prog14{

public static void main(String[] args){

Scanner scan = new Scanner(System.in).useDelimiter("\\D");//匹配非數(shù)字

System.out.print("請輸入當(dāng)前日期(年-月-日):");

int year = scan.nextInt();

int month = scan.nextInt();

int date = scan.nextInt();

scan.close();

System.out.println("今天是"+year+"年的第"+analysis(year,month,date)+"天");

}

//判斷天數(shù)

private static int analysis(int year, int month, int date){

int n = 0;

int[] month_date = new int[] {0,31,28,31,30,31,30,31,31,30,31,30};

if((year%400)==0 || ((year%4)==0)&&((year%100)!=0))

??month_date[2] = 29;

for(int i=0;i

??n += month_date[i];

return n+date;

}

}

十六.猴子吃桃問題:猴子第一天摘下若干個桃子,當(dāng)即吃了一半,還不癮,又多吃了一個第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。

程序分析:采取逆向思維的方法,從后往前推斷。

public class Prog17{

public static void main(String[] args){

int m = 1;

??for(int i=10;i>0;i--)

????m = 2*m + 2;

??System.out.println("小猴子共摘了"+m+"桃子");

}

}

十七.求1+2!+3!+...+20!的和

程序分析:此程序只是把累加變成了累乘。

public class Prog21{

public static void main(String[] args){

long sum = 0;

for(int i=0;i<20;i++)

??sum += factorial(i+1);

System.out.println(sum);

}

//階乘

private static long factorial(int n){

int mult = 1;

for(int i=1;i

??mult *= i;

return mult;

}

}

十八.一個5位數(shù),判斷它是不是回文數(shù)。即12321是回文數(shù),個位與萬位相同,十位與千位相同。

import java.io.*;

public class Prog25{

public static void main(String[] args){

int n = 0;

System.out.print("請輸入一個5位數(shù):");

BufferedReader bufin = new BufferedReader(new InputStreamReader(System.in));

try{

??n = Integer.parseInt(bufin.readLine());

}catch(IOException e){

e.printStackTrace();

}finally{

try{

??bufin.close();

}catch(IOException e){

e.printStackTrace();

}

}

palin(n);

}

private static void palin(int n){

int m = n;

int[] a = new int[5];

if(n<10000 || n>99999){

System.out.println("輸入的不是5位數(shù)!");

return;

}else{

??for(int i=0;i<5;i++){

??a[i] = n%10;

??n /= 10;

??}

??if(a[0]==a[4] && a[1]==a[3])

????System.out.println(m+"是一個回文數(shù)");

??else

????System.out.println(m+"不是回文數(shù)");

????}

???}

}

十九.對10個數(shù)進行排序

程序分析:可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換, 下次類推,即用第二個元素與后8個進行比較,并進行交換。

public class Prog28{

public static void main(String[] args){

int[] a = new int[]{31,42,21,50,12,60,81,74,101,93};

for(int i=0;i<10;i++)

for(int j=0;j

if(a[j]>a[j+1]){

int temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

for(int i=0;i

??System.out.print(a[i]+" ");

}

}

二十.求一個3*3矩陣對角線元素之和

程序分析:利用雙重for循環(huán)控制輸入二維數(shù)組,再將a[i][i]累加后輸出。

public class Prog29{

public static void main(String[] args){

int[][] a = new int[][] {{100,2,3,},{4,5,6},{17,8,9}};

matrSum(a);

}

private static void matrSum(int[][] a){

int sum1 = 0;

int sum2 = 0;

for(int i=0;i

??for(int j=0;j

?? if(i==j) sum1 += a[i][j];

?? if(j==a.length-i-1) sum2 += a[i][j];

??}

System.out.println("矩陣對角線之和分別是:"+sum1+"和"+sum2);

}

}

二十一.將一個數(shù)組逆序輸出。

程序分析:用第一個與最后一個交換。

public class Prog31{

public static void main(String[] args){

int[] A = new int[]{1,2,3,4,5,6,7,8,9,};

print(A);

System.out.println();

int[] B = reverse(A);

print(B);

}

private static int[] reverse(int[] A){

for(int i=0;i

int temp = A[A.length-i-1];

A[A.length-i-1] = A[i];

A[i] = temp;

}

return A;

}

private static void print(int[] A){

for(int i=0;i

??System.out.print(A[i]+" ");

}

}

二十二.打印出楊輝三角形(要求打印出10行如下圖)

程序分析:

?????1

??? 1 1

????1 2 1

1 3 3 1

?1 4 6 4 1

1 5 10 10 5 1

public class Prog33{

public static void main(String[] args){

int[][] n = new int[10][21];

n[0][10] = 1;

for(int i=1;i<10;i++)

??for(int j=10-i;j<10+i+1;j++)

????n[i][j] = n[i-1][j-1]+n[i-1][j+1];

for(int i=0;i<10;i++){

for(int j=0;j<21;j++){

if(n[i][j]==0)

??System.out.print(" ??");

else{

????if(n[i][j]<10)

??????System.out.print(" ?"+n[i][j]);//空格為了美觀需要

????else if(n[i][j]<100)

??????System.out.print(" "+n[i][j]);

??????else

????????System.out.print(n[i][j]);

??}

}

System.out.println();

}

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    葉總韓閱讀 5,230評論 0 41
  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一...
    阿里高級軟件架構(gòu)師閱讀 3,399評論 0 19
  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,514評論 0 6
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 2,087評論 0 2
  • 可能是習(xí)慣了大大咧咧,也可能是在成長的道路上。我相信一切也都漸入軌道變得越來越好,曾經(jīng)笑過也哭過,都在盡自己的努力...
    陳略略略閱讀 782評論 0 0

友情鏈接更多精彩內(nèi)容