




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假設兔子都不死,問每個月的兔子總數為多少? /這是一個菲波拉契數列問題public class lianxi01 public static void main(String args) System.out.println("第1個月的兔子對數: 1");System.out.println("第2個月的兔子對數:
2、60; 1");int f1 = 1, f2 = 1, f, M=24; for(int i=3; i<=M; i+) f = f2; f2 = f1 + f2; f1 = f; System.out.println("第" + i +"個月的兔子對數: &quo
3、t;+f2); 【程序2】 題目:判斷101-200之間有多少個素數,并輸出所有素數。 程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除, 那么說明此數不是素數,反之是素數。 public class lianxi02 public static void main(String args) int count = 0; for(
4、int i=101; i<200; i+=2) boolean b = false; for(int j=2; j<=Math.sqrt(i); j+) if(i % j = 0) b = false; break; else
5、60; b = true; if(b = true) count +;System.out.println(i );
6、 System.out.println( "素數個數是: " + count);【程序3】 題目:打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等于該數本身。例如:153是一個 "水仙花數 ",因為153=1的三次方5的三次方3的三次方。public class lianxi03
7、 public static void main(String args) int b1, b2, b3; for(int m=101; m<1000; m+) b3 = m / 100; b2 = m % 100 / 10; b1 = m % 10;
8、 if(b3*b3*b3 + b2*b2*b2 + b1*b1*b1) = m) System.out.println(m+"是一個水仙花數"); 【程序4】 題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。 程序分析:對n進行分解質因數,應先找到一個最小的質數k,然后按下述步驟完成:
9、160; (1)如果這個質數恰等于n,那么說明分解質因數的過程已經結束,打印出即可。 (2)如果n <> k,但n能被k整除,那么應打印出k的值,并用n除以k的商,作為新的正整數你n,重復執(zhí)行第一步。 (3)如果n不能被k整除,那么用k+1作為k的值,重復執(zhí)行第一步。 import java.util.*;public class lianxi04 p
10、ublic static void main(String args) Scanner s = new Scanner(System.in); System.out.print( "請鍵入一個正整數: "); int n
11、0; = s.nextInt(); int k=2; System.out.print(n + "=" ); while(k <= n) if(k = n) System.out.println(n
12、);break; else if( n % k = 0) System.out.print(k + "*");n = n / k; else k+;
13、; 【程序5】 題目:利用條件運算符的嵌套來完成此題:學習成績> =90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。 import java.util.*;public class lianxi05 public static voi
14、d main(String args) int x; char grade; Scanner s = new Scanner(System.in); System.out.print( "請輸入一個成績: "); x = s.nextInt(); grade =
15、x >= 90 ? 'A' : x >= 60 ? 'B' :'C' System.out.println("等級為:"+grade); 【程序6】 題目:輸入兩個正整數m和n,求其最大
16、公約數和最小公倍數。 /*在循環(huán)中,只要除數不等于0,用較大數除以較小的數,將小的一個數作為下一輪循環(huán)的大數,取得的余數作為下一輪循環(huán)的較小的數,如此循環(huán)直到較小的數的值為0,返回較大的數,此數即為最大公約數,最小公倍數為兩數之積除以最大公約數。* /import java.util.*;public class lianxi06 public static void main(String args) int
17、0; a ,b,m;Scanner s = new Scanner(System.in);System.out.print( "鍵入一個整數: "); a = s.nextInt();System.out.print( "再鍵入一個整數: "); b = s.nextInt(); deff cd = new deff(); m = cd.deff(a,b);
18、0; int n = a * b / m; System.out.println("最大公約數: " + m); System.out.println("最小公倍數: " + n); class deffpublic int deff(int x, int y) int t; if(x < y) &
19、#160; t = x; x = y; y = t; while(y != 0) if(x = y) return x; else int k = x
20、 % y; x = y; y = k; return x; 【程序7】 題目:輸入一行字符,分別統(tǒng)計出其中英文字母、空格、數字和其它字符的個數。 import java.util.*;public class lianxi07
21、public static void main(String args) int digital = 0;int character = 0;int other = 0;int blank = 0; char ch = null; Scanner sc = new Scanner(System.in); String s = sc.nextLine(); ch = s.toCharArray();
22、0; for(int i=0; i<ch.length; i+) if(ch >= '0' && ch <= '9') digital +; else if(ch >= 'a' && ch <= 'z') | ch > 'A
23、39; && ch <= 'Z') character +; else if(ch = ' ') blank +; else other +;
24、 System.out.println("數字個數: " + digital); System.out.println("英文字母個數: " + character); System.out.println("空格個數: " + blank); System.out.println(&
25、quot;其他字符個數:" + other );個人收集整理 勿做商業(yè)用途【程序8】 題目:求s=a+aa+aaa+aaaa+aa.a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。 import java.util.*;public class lianxi08 public static void main(String args) long a , b = 0, sum = 0;
26、0; Scanner s = new Scanner(System.in); System.out.print("輸入數字a的值: "); a = s.nextInt(); System.out.print("輸入相加的項數:"); int n = s.nextInt(); int i = 0;
27、 while(i < n) b = b + a; sum = sum + b; a = a * 10; + i; System.out.println(sum); 【程序9】
28、0; 題目:一個數如果恰好等于它的因子之和,這個數就稱為 "完數 "。例如6=123.編程 找出1000以內的所有完數。 public class lianxi09 public static void main(String args) System.out.println("1到1000的完數有: "); for(int i=1; i<1000; i+) &
29、#160; int t = 0; for(int j=1; j<= i/2; j+) if(i % j = 0) t = t + j;
30、0; if(t = i) System.out.print(i + " "); 個人收集整理 勿做商業(yè)用途【程序10】 題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高? public c
31、lass lianxi10 public static void main(String args) double h = 100,s = 100; for(int i=1; i<10; i+) s = s + h; h = h / 2; System
32、.out.println("經過路程:" + s); System.out.println("反彈高度:" + h / 2); 【程序11】 題目:有1、2、3、4四個數字,能組成多少個互不相同且無重復數字的三位數?都是多少? public class lianxi11 public static void main(String args) int count = 0;
33、0; for(int x=1; x<5; x+) for(int y=1; y<5; y+) for(int z=1; z<5; z+) if(x != y && y != z && x != z) cou
34、nt +; System.out.println(x*100 + y*10 + z ); System.out.println("共有" + count + &qu
35、ot;個三位數"); 【程序12】 題目:企業(yè)發(fā)放的獎金根據利潤提成。利潤(I)低于或等于10萬元時,獎金可提10%;利潤高于10萬元,低于20萬元時,低于10萬元的局部按10%提成,高于10萬元的局部,可可提成7.5%;20萬到40萬之間時,高于20萬元的局部,可提成5%;40萬到60萬之間時高于40萬元的局部,可提成3%;60萬到100萬之間時,高于60萬元的局部,可提成1.5%,高于100萬元時,超過100萬元的局部按1%提成,從鍵盤輸入當月利潤,求應發(fā)放獎金總數? import java.util.*;pub
36、lic class lianxi12 public static void main(String args) double x = 0,y = 0; System.out.print("輸入當月利潤萬:"); Scanner s = new Scanner(System.in); x = s.nextInt(); if(x >
37、0 && x <= 10) y = x * 0.1; else if(x > 10 && x <= 20) y = 10 * 0.1 + (x - 10) * 0.075; else if(x > 20 && x <= 40) y = 10 * 0.1
38、 + 10 * 0.075 + (x - 20) * 0.05; else if(x > 40 && x <= 60) y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03; else if(x > 60 && x <= 100) y = 20 * 0.175 +
39、20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015; else if(x > 100) y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01; System.out.println("應該提取的獎金是 " + y + "萬");文檔收集自網絡,僅用于
40、個人學習【程序13】 題目:一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少? public class lianxi13 public static void main(String args) for(int x =1; x<100000; x+) if(Math.sqrt(x+100) % 1 = 0) &
41、#160; if(Math.sqrt(x+268) % 1 = 0) System.out.println(x + "加100是一個完全平方數,再加168又是一個完全平方數"); /*按題意循環(huán)應該從-100開始整數包括正整數、負整數、零,這樣會多一個滿足條件的數-99。但是我看到大局部人解這道題目時都把題中
42、的“整數理解成正整數,我也就隨大流了。*/【程序14】 題目:輸入某年某月某日,判斷這一天是這一年的第幾天? import java.util.*;public class lianxi14 public static void main(String args) int year, month, day; int days = 0; int d = 0;
43、160; int e; input fymd = new input(); do e = 0; System.out.print("輸入年:"); year =fymd.input(); System.out.print("輸入月:");
44、0; month = fymd.input(); System.out.print("輸入天:"); day = fymd.input(); if (year < 0 | month < 0 | month > 12 | day < 0 | day > 31) System.out.println("輸入錯誤,請重新輸入!"
45、;); e=1 ; while( e=1); for (int i=1; i <month; i+) switch (i) case 1: case 3:
46、 case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4:
47、0; case 6: case 9: case 11: days = 30; break; case 2: if (year % 400 = 0) | (year % 4 = 0
48、&& year % 100 != 0) days = 29; else days = 28; break; &
49、#160; d += days; System.out.println(year + "-" + month + "-" + day + "是這年的第" + (d+day) + "天。");class inputpublic int input() int value = 0; Scan
50、ner s = new Scanner(System.in); value = s.nextInt(); return value;個人收集整理 勿做商業(yè)用途【程序15】 題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。 import java.util.*;public class lianxi15 public static void main(String args)
51、input fnc = new input(); int x=0, y=0, z=0; System.out.print("輸入第一個數字:"); x = fnc.input(); System.out.print("輸入第二個數字:"); y = fnc.input();
52、; System.out.print("輸入第三個數字:"); z = fnc.input(); if(x > y) int t = x; x = y; y = t;
53、0; if(x > z) int t = x; x = z; z = t; if(y > z) int t = y; y = z;
54、0; z = t; System.out.println( "三個數字由小到大排列為: "+x + " " + y + " " + z);class inputpublic int input() int value = 0; Scanner s = new Scanner(System.in); valu
55、e = s.nextInt(); return value; 【程序16】題目:輸出9*9口訣。 public class lianxi16 public static void main(String args) for(int i=1; i<10; i+) for(int j=1; j<=i; j+) &
56、#160; System.out.print(j + "*" + i + "=" + j*i + " " ); if(j*i<10)System.out.print(" "); System.out.println(); 【程
57、序17】 題目:猴子吃桃問題:猴子第一天摘下假設干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下 的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。 public class lianxi17 public static void main(String args) int x =
58、1; for(int i=2; i<=10; i+) x = (x+1)*2; System.out.println("猴子第一天摘了 " + x + " 個桃子");個人收集整理 勿做商業(yè)用途【程序18】 題目:兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。有
59、人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。 public class lianxi18 static char m = 'a', 'b', 'c' ;static char n = 'x', 'y', 'z' ;public static void main(String args) for (int i = 0; i < m.length; i+)
60、for (int j = 0; j < n.length; j+) if (mi = 'a' && nj = 'x') continue; else if (mi = 'a' && nj = 'y') continue; else if (mi = 'c' &
61、amp;& nj = 'x') | (mi = 'c' && nj = 'z') continue; else if (mi = 'b' && nj = 'z') | (mi = 'b' &&
62、nj = 'y') continue; else System.out.println(mi + " vs " + nj); 【程序19】 題目:打印出如以下圖案菱形 *
63、0; * * * * * * public class lianxi19 public static void main(String args) int H = 7, W = 7;/高和寬必須是相等的奇數 for(int i=0; i<
64、;(H+1) / 2; i+) for(int j=0; j<W/2-i; j+) System.out.print(" "); for(int k=1; k<(i+1)*2; k+) System.out.print('*');
65、 System.out.println(); for(int i=1; i<=H/2; i+) for(int j=1; j<=i; j+) System.out.print(" "); for(int k=1; k<=W-2*i
66、; k+) System.out.print('*'); System.out.println(); 【程序20】 題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13.求出這個數列的前20項之和。 public class lianxi20 public static void main(String args)
67、60; int x = 2, y = 1, t; double sum = 0; for(int i=1; i<=20; i+) sum = sum + (double)x / y; t = y; y = x; x = y + t; System.out.
68、println("前20項相加之和是: " + sum);【程序21】 題目:求1+2!+3!+.+20!的和 public class lianxi21 public static void main(String args) long sum = 0; long fac = 1; for(int i=1; i<=20; i+) fac =
69、 fac * i; sum += fac; System.out.println(sum);【程序22】 題目:利用遞歸方法求5!。 public class lianxi22 public static void main(String args) int n = 5; rec fr = new
70、rec(); System.out.println(n+"! = "+fr.rec(n);class recpublic long rec(int n) long value = 0 ; if(n =1 ) value = 1; else value = n * rec(n-1);
71、160; return value; 【程序23】 題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最后問第一個人,他說是10歲。請問第五個人多大? public class lianxi23 public static void main(String args) int age = 10;
72、0; for(int i=2; i<=5; i+) age =age+2; System.out.println(age); 【程序24】 題目:給一個不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。 /使用了長整型最多輸入18位import java.util.*;public class lianxi24 public static void main(String args
73、) Scanner s = new Scanner(System.in); System.out.print("請輸入一個正整數:"); long a = s.nextLong(); String ss = Long.toString(a); char ch = ss.toCharArray(); int j=ch.length; System.out.println(a
74、 + "是一個"+ j +"位數。"); System.out.print("按逆序輸出是:"); for(int i=j-1; i>=0; i-) System.out.print(chi); 【程序25】 題目:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同。
75、 import java.util.*;public class lianxi25 public static void main(String args) Scanner s = new Scanner(System.in); int a; do System.out.print("請輸入一個5位正整數:"); a = s.nextIn
76、t(); while(a<10000|a>99999); String ss =String.valueOf(a); char ch = ss.toCharArray(); if(ch0=ch4&&ch1=ch3) System.out.println("這是一個回文數");
77、 else System.out.println("這不是一個回文數"); /這個更好,不限位數import java.util.*;public class lianxi25a public static void main(String args) Scanner s = new Scanner(System.in); boolean is =true; System.out.print("
78、請輸入一個正整數:"); long a = s.nextLong(); String ss = Long.toString(a); char ch = ss.toCharArray(); int j=ch.length; for(int i=0; i<j/2; i+) if(chi!=chj-i-1)is=false; if(is=true)System.out.println(&qu
79、ot;這是一個回文數"); else System.out.println("這不是一個回文數"); 【程序26】 題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,那么繼續(xù) 判斷第二個字母。 import java.util.*;public class lianxi26 public static void main(String args)
80、 getChar tw = new getChar(); System.out.println("請輸入星期的第一個大寫字母:"); char ch = tw.getChar(); switch(ch) case 'M': System.out.println("Monday");
81、160; break; case 'W': System.out.println("Wednesday"); break; case 'F': System.out.println("Friday");
82、 break; case 'T': System.out.println("請輸入星期的第二個字母:"); char ch2 = tw.getChar(); if(ch2 = 'U') System.out.println("Tuesday&quo
83、t;); else if(ch2 = 'H') System.out.println("Thursday"); else System.out.println("無此寫法!"); ; break;
84、160; case 'S': System.out.println("請輸入星期的第二個字母:"); char ch2 = tw.getChar(); if(ch2 = 'U') System.out.println("Sunday"); else if(ch
85、2 = 'A') System.out.println("Saturday"); else System.out.println("無此寫法!"); ; break;default:System.out.println("無此寫法!"); cl
86、ass getCharpublic char getChar() Scanner s = new Scanner(System.in); String str = s.nextLine(); char ch = str.charAt(0); if(ch<'A' | ch>'Z') System.out.println("輸入錯誤,請重新輸入&quo
87、t;); ch=getChar(); return ch; 【程序27】 題目:求100之內的素數 /使用除sqrt(n)的方法求出的素數不包括2和3public class lianxi27 public static void main(String args) boolean b =false; Syste
88、m.out.print(2 + " "); System.out.print(3 + " "); for(int i=3; i<100; i+=2) for(int j=2; j<=Math.sqrt(i); j+) if(i % j = 0) b = false; &
89、#160; break; elseb = true; if(b = true) System.out.print(i + " "); /該程序使用除1位素數得2位方法,運行效率高通用性差。public class lianxi2
90、7a public static void main(String args) int a = new int2, 3, 5, 7; for(int j=0; j<4; j+)System.out.print(aj + " "); boolean b =false; for(int i=11; i<100; i+=2) for(int j=0; j<4; j+)
91、 if(i % aj = 0) b = false; break; elseb = true; if(b = true) System.out.print
92、(i + " "); 【程序28】 題目:對10個數進行排序 import java.util.*;public class lianxi28 public static void main(String args) Scanner s = new Scanner(System.in); int a = new int10; System.out.println("請輸入10個整數:&qu
93、ot;); for(int i=0; i<10; i+) ai = s.nextInt(); for(int i=0; i<10; i+) for(int j=i+1; j<10; j+) if(ai > aj) int t = ai; ai = aj;
94、 aj = t; for(int i=0; i<10; i+) System.out.print(ai + " "); 【程序29】 題目:求一個3*3矩陣對角線元素之和 import java.util.*;public
95、 class lianxi29 public static void main(String args) Scanner s = new Scanner(System.in); int a = new int33;System.out.println("請輸入9個整數:"); for(int i=0; i<3; i+) for(int j=0; j<3; j+) aij = s.nextInt();
96、160; System.out.println("輸入的3 * 3 矩陣是:"); for(int i=0; i<3; i+) for(int j=0; j<3; j+) System.out.print(aij + " "); System.out.println(); &
97、#160; int sum = 0; for(int i=0; i<3; i+) for(int j=0; j<3; j+) if(i = j) sum += aij; System.out.println("對角線之和是:" + sum);【程序30】 題目:有一個已經排好序的數組?,F輸入一個數,要求按原來的規(guī)律將它插入數組中。 /此程序不好,沒有使用折半
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度診所執(zhí)業(yè)醫(yī)師醫(yī)療風險防控聘用合同
- 二零二五年度手車轉讓與綠色出行推廣合同
- 二零二五年度投資分紅股收益分配協(xié)議
- 二零二五年度汽車展覽會參展商展位電力合同
- 2025年度道路破碎修復與再生利用合同
- 2025年度青貯收割作業(yè)與農業(yè)物聯網平臺合作協(xié)議
- 二零二五年度口腔診所醫(yī)生培訓與薪酬管理合同
- 二零二五年度商業(yè)秘密保護與員工保密義務合同
- 二零二五年度多功能辦公場所租賃服務協(xié)議
- 2025年度蔬菜大棚承包與品牌授權合作協(xié)議
- 《企業(yè)員工培訓國內外文獻綜述》4800字
- 麻醉藥品與精神藥品不良反應的防治 (1) - 副本課件
- 車輛保險登記臺賬參考模板范本
- 三年級下冊數學教案-速度、時間和路程 滬教版
- 礦山道路施工組織設計方案
- 正弦函數的圖像與性質優(yōu)秀課件
- 山東省任氏宗親分布村落
- 北師大版小學數學五年級下冊《有趣的折疊》說課稿
- 陜西省建設工程長安杯獎省優(yōu)質工程結構備案和復查的要求
- 典型示功圖分析(全)
- 水生觀賞動物鑒賞與維護課程
評論
0/150
提交評論