




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、.【程序 1】題目:古典問題:有一對兔子,從出生后第3 個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?/這是一個菲波拉契數列問題public class lianxi01 public static void main(String args) System.out.println( 第 1 個月的兔子對數:1);System.out.println( 第 2 個月的兔子對數:1);int f1 = 1, f2 = 1, f, M=24;for(int i=3; i=M; i+) f = f2;f2 = f1 + f2;f1 = f
2、;System.out.println( 第 + i + 個月的兔子對數: +f2);【程序 2】題目:判斷101-200 之間有多少個素數,并輸出所有素數。程序分析:判斷素數的方法:用一個數分別去除2 到 sqrt(這個數 ) ,如果能被整除,則表明此數不是素數,反之是素數。public class lianxi02 public static void main(String args) int count = 0;for(int i=101; i200; i+=2) boolean b = false;for(int j=2; j=Math.sqrt(i); j+)if(i % j =
3、0) b = false; break; else b = true; if(b = true) count +;System.out.println(i );System.out.println( 素數個數是 : + count);1/38.【程序3】題目:打印出所有的 水仙花數 ,所謂 水仙花數是指一個三位數,其各位數字立方和等于該數本身。例如:153 是一個 水仙花數,因為 153=1 的三次方 5 的三次方3 的三次方。public class lianxi03 public static void main(String args) int b1, b2, b3;for(int m=
4、101; m1000; m+) b3= m / 100;b2= m % 100 / 10;b1= m % 10;if(b3*b3*b3 + b2*b2*b2 + b1*b1*b1) = m) System.out.println(m+ 是一個水仙花數); 【程序 4】題目:將一個正整數分解質因數。例如:輸入90,打印出 90=2*3*3*5 。程序分析:對n 進行分解質因數,應先找到一個最小的質數k,然后按下述步驟完成:(1) 如果這個質數恰等于 n,則說明分解質因數的過程已經結束,打印出即可。(2) 如果 n k,但 n 能被 k 整除,則應打印出 k 的值,并用 n 除以 k 的商 ,作為
5、新的正整數你 n,重復執行第一步。(3) 如果 n 不能被 k 整除,則用 k+1 作為 k 的值 ,重復執行第一步。import java.util.*;publicclasslianxi04public static void main(String args) Scanner s = new Scanner(System.in);System.out.print( 請鍵入一個正整數:);intn= s.nextInt();int k=2;System.out.print(n + = );while(k =90 分的同學用A 表示, 60-89 分之間的用 B 表示, 60 分以下的用C
6、表示。import java.util.*;public class lianxi05 public static void main(String args) int x;char grade;Scanner s = new Scanner(System.in);System.out.print( 請輸入一個成績: );x = s.nextInt();grade = x = 90 ? A: x = 60 ? B:C;System.out.println( 等級為: +grade);3/38.【程序 6】題目:輸入兩個正整數m 和 n,求其最大公約數和最小公倍數。/* 在循環中, 只要除數不等
7、于0,用較大數除以較小的數,將小的一個數作為下一輪循環的大數,取得的余數作為下一輪循環的較小的數,如此循環直到較小的數的值為0,返回較大的數,此數即為最大公約數,最小公倍數為兩數之積除以最大公約數。* /import java.util.*;publicclasslianxi06public static void main(String args) inta ,b,m;Scanner s = new Scanner(System.in);System.out.print( 鍵入一個整數:);a = s.nextInt();System.out.print( 再鍵入一個整數:);b = s.n
8、extInt();deff cd = new deff();m = cd.deff(a,b);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) t = x;x = y;y = t;while(y != 0) if(x = y) return x;else int k = x % y;x = y;y = k;return x;4/38.【程序 7】題目:輸入一行字符,分別統
9、計出其中英文字母、空格、數字和其它字符的個數。import java.util.*;public class lianxi07 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();for(int i=0; i= 0 & ch = a & ch A & ch =
10、 Z) character +; else if(ch = ) blank +; else other +;System.out.println( 數字個數 : + digital);System.out.println( 英文字母個數: + character);System.out.println( 空格個數 : + blank);System.out.println( 其他字符個數: + other );5/38.【程序 8】題目:求 s=a+aa+aaa+aaaa+aa.a的值,其中 a 是一個數字。例如2+22+222+2222+22222( 此時共有 5 個數相加 ),幾個數相加有
11、鍵盤控制。import java.util.*;public class lianxi08 public static void main(String args) long a , b = 0, sum = 0;Scanner s = new Scanner(System.in);System.out.print( 輸入數字a 的值:);a = s.nextInt();System.out.print( 輸入相加的項數:);int n = s.nextInt();int i = 0;while(i n) b = b + a;sum = sum + b;a = a * 10;+ i;Syste
12、m.out.println(sum);【程序 9】題目:一個數如果恰好等于它的因子之和,這個數就稱為完數。例如6=1 2 3.編程找出 1000 以內的所有完數。public class lianxi09 public static void main(String args) System.out.println(1 到 1000 的完數有:);for(int i=1; i1000; i+) int t = 0;for(int j=1; j= i/2; j+) if(i % j = 0) t = t + j;if(t = i) System.out.print(i + );6/38.【程序
13、10】題目:一球從 100 米高度自由落下, 每次落地后反跳回原高度的一半;再落下,求它在第10 次落地時,共經過多少米?第10 次反彈多高?public class lianxi10 public static void main(String args) double h = 100,s = 100;for(int i=1; i10; i+) s = s + h;h = h / 2;System.out.println( 經過路程: + s);System.out.println( 反彈高度: + h / 2);【程序 11】題目:有 1、2、3、4 四個數字,能組成多少個互不相同且無重復
14、數字的三位數?都是多少?public class lianxi11 public static void main(String args) int count = 0;for(int x=1; x5; x+) for(int y=1; y5; y+) for(int z=1; z 0 & x 10 & x 20 & x 40 & x 60 & x 100) y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;System.out.println( 應該提取的獎金是 + y + 萬 );8/38.【程序 13】題目: 一個整數,
15、它加上 100 后是一個完全平方數, 再加上 168 又是一個完全平方數,請問該數是多少?public class lianxi13 public static void main(String args) for(int x =1; x100000; x+) if(Math.sqrt(x+100) % 1 = 0) if(Math.sqrt(x+268) % 1 = 0) System.out.println(x + 加 100 是一個完全平方數,再加168 又是一個完全平方數);/* 按題意循環應該從-100 開始(整數包括正整數、負整數、零),這樣會多一個滿足條件的數-99。但是我看到大
16、部分人解這道題目時都把題中的“整數 ”理解成正整數,我也就隨大流了。*/9/38.【程序 14】題目:輸入某年某月某日,判斷這一天是這一年的第幾天?import java.util.*;public class lianxi14 public static void main(String args) int year, month, day;int days = 0;int d = 0;int e;input fymd = new input();do e = 0;System.out.print( 輸入年: );year =fymd.input();System.out.print( 輸入
17、月: );month = fymd.input();System.out.print( 輸入天: );day = fymd.input();if (year 0 | month 12 | day 31) System.out.println( 輸入錯誤,請重新輸入!);e=1 ;while( e=1);for (int i=1; i y) int t = x;x = y;y = t;if(x z) int t = x;x = z;z = t;if(y z) int t = y;y = z;z = t;System.out.println( 三個數字由小到大排列為:+x + + y + + z)
18、;class inputpublic int input() int value = 0;Scanner s = new Scanner(System.in);value = s.nextInt();return value;12/38.【程序 16】題目:輸出9*9 口訣。public class lianxi16 public static void main(String args) for(int i=1; i10; i+) for(int j=1; j=i; j+) System.out.print(j + * + i + = + j*i + );if(j*i10)System.ou
19、t.print( );System.out.println();【程序 17】題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。 到第 10 天早上想再吃時, 見只剩下一個桃子了。 求第一天共摘了多少。public class lianxi17 public static void main(String args) int x = 1;for(int i=2; i=10; i+) x = (x+1)*2;System.out.println( 猴子第一天摘了 + x +
20、個桃子 );13/38.【程序 18】題目:兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c 三人,乙隊為x,y,z 三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。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+) for (int j = 0; j n.length; j+) if (m
21、i = a & nj = x) continue; else if (mi = a & nj = y) continue; else if (mi = c & nj = x)| (mi = c & nj = z) continue; else if (mi = b & nj = z)| (mi = b & nj = y) continue; elseSystem.out.println(mi + vs + nj);14/38.【程序 19】題目:打印出如下圖案(菱形)*public class lianxi19 public static void main(String args) int
22、H = 7, W = 7;/ 高和寬必須是相等的奇數for(int i=0; i(H+1) / 2; i+) for(int j=0; jW/2-i; j+) System.out.print( );for(int k=1; k(i+1)*2; k+) System.out.print(*);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; k+) System.out.print(*);System.out.println()
23、;15/38.【程序 20】題目:有一分數序列:2/1, 3/2, 5/3, 8/5, 13/8,21/13. 求出這個數列的前20 項之和。public class lianxi20 public static void main(String args) 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.println( 前 20 項相加之和是: + sum);【程序 21】題目:求1+2!+3!+.+20!
24、 的和public class lianxi21 public static void main(String args) long sum = 0;long fac = 1;for(int i=1; i=20; i+) fac = fac * i;sum += fac;System.out.println(sum);16/38.【程序 22】題目:利用遞歸方法求5!。public class lianxi22 public static void main(String args) int n = 5;rec fr = new rec();System.out.println(n+! = +
25、fr.rec(n);class recpublic long rec(int n) long value = 0 ;if(n =1 ) value = 1; elsevalue = n * rec(n-1);return value;【程序 23】題目:有5個人坐在一起,問第五個人多少歲?他說比第4 個人大 2 歲。問第 4 個人歲數,他說比第3個人大 2 歲。問第三個人,又說比第 2 人大兩歲。 問第 2 個人, 說比第一個人大兩歲。最后問第一個人,他說是10 歲。請問第五個人多大?public class lianxi23 public static void main(String ar
26、gs) int age = 10;for(int i=2; i=0; i-) System.out.print(chi);18/38.【程序 25】題目:一個 5 位數,判斷它是不是回文數。即 12321 是回文數,個位與萬位相同,十位與千位相同。import java.util.*;public class lianxi25 public static void main(String args) Scanner s = new Scanner(System.in);int a;doSystem.out.print(請輸入一個5 位正整數: );a = s.nextInt();while(a
27、99999);String ss =String.valueOf(a);char ch = ss.toCharArray();if(ch0=ch4&ch1=ch3)System.out.println(這是一個回文數);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.pri
28、nt(請輸入一個正整數:);long a = s.nextLong();String ss = Long.toString(a);char ch = ss.toCharArray();int j=ch.length;for(int i=0; ij/2; i+) if(chi!=chj-i-1)is=false;if(is=true)System.out.println(這是一個回文數);else System.out.println(這不是一個回文數);19/38.【程序 26】題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續判斷第二個字母。import java.
29、util.*;public class lianxi26 public static void main(String args) getChar tw = new getChar();System.out.println(請輸入星期的第一個大寫字母:);char ch = tw.getChar();switch(ch) case M:System.out.println(Monday);break;case W:System.out.println(Wednesday);break;case F:System.out.println(Friday);break;case T: System.
30、out.println(請輸入星期的第二個字母:);char ch2 = tw.getChar();if(ch2 = U) System.out.println(Tuesday); else if(ch2 = H) System.out.println(Thursday); else System.out.println(無此寫法! );break;case S: System.out.println(請輸入星期的第二個字母:);char ch2 = tw.getChar();if(ch2 = U) System.out.println(Sunday); else if(ch2 = A) Sy
31、stem.out.println(Saturday); else System.out.println(無此寫法! );break;default:System.out.println(無此寫法! );class getCharpublic char getChar() 20/38.Scanner s = new Scanner(System.in);String str = s.nextLine();char ch = str.charAt(0);if(chZ) System.out.println(輸入錯誤,請重新輸入);ch=getChar();return ch;【程序 27】題目:求
32、 100 之內的素數/ 使用除 sqrt(n)的方法求出的素數不包括2 和 3public class lianxi27 public static void main(String args) boolean b =false;System.out.print(2 + );System.out.print(3 + );for(int i=3; i100; i+=2) for(int j=2; j=Math.sqrt(i); j+) if(i % j = 0) b = false;break; elseb = true;if(b = true) System.out.print(i + );/ 該程序使用除 1 位素數得 2 位方法,運行效率高通用性差。public class lianxi27a public static void main(String args) int a = new int2, 3, 5, 7;for(int j=0; j4; j+)System.out.print(aj + );boolean b =false;for(int i=11; i100; i+=2) for(int j=0; j4; j+) if(i % aj = 0) b = false;break; elseb = true;if(b = true) System
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T/CNFA 9-2019中式家具常用木材識別
- T/CIMA 0032-2022單相智能電能表設計方案多目標穩健性評價通則
- T/CHASA 001-2018凈水上門服務工程師資質認證規范
- T/CECS 10349-2023綠色校園用裝飾裝修材料抗菌、抗病毒性能要求
- T/CECS 10109-2020耐腐蝕預制混凝土樁
- T/CCMA 0168-2023土方機械電控手柄技術要求及試驗方法
- T/CCMA 0095-2020非公路自卸車操作使用規程
- T/CCAS 021-2021水泥生料助磨劑
- T/CCAS 004-2018錳(礦)渣化學分析方法
- T/CAQI 86-2019家用和類似用途新風機空氣清新度技術要求及試驗方法
- 現場照相技術課件
- 抖音帶貨主播勞動合同范本
- 廣東省高等學校“千百十工程”第六批繼續培養對象和第
- 人教版三年級數學上冊口算題卡
- 綠色施工與環境管理
- 小數乘整數的教學設計 小數乘整數教學設計一等獎(十四篇)
- 畢業設計基于單片機的發動機轉速電控系統程序設計及仿真
- 統借統還資金分撥合同
- 地鐵運營施工負責人考試題庫
- GB/T 708-2006冷軋鋼板和鋼帶的尺寸、外形、重量及允許偏差
- 故宮的資料簡介(標準版)
評論
0/150
提交評論