/*
【程序1】題目:古典問題:有一對兔子锨能,從出生后第3個月起每個月都生一對兔子荒适,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死突想,問每個月的兔子總數(shù)為多少?
1.
程序分析: 兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21....
*/
package cn.com.flywater.FiftyAlgorthm;
public class FirstRabbit {
public static final int MONTH = 15;
public static void main(String[] args) {
?? long f1 = 1L, f2 = 1L;
?? long f;
?? for(int i=3; i
??? f = f2;
??? f2 = f1 + f2;
??? f1 = f;
??? System.out.print("
第"
+ i +"個月的兔子對數(shù):");
??? System.out.println(" " +f2);
?? }
}
}
/*【程序2】
*
作者 南楓題目:判斷101-200之間有多少個素?cái)?shù)究抓,并輸出所有素?cái)?shù)猾担。
1.
程序分析:判斷素?cái)?shù)的方法:用一個數(shù)分別去除2到sqrt(這個數(shù)),如果能被整除刺下,則表明此數(shù)不是素?cái)?shù)绑嘹,反之是素?cái)?shù)。*/
package cn.com.flywater.FiftyAlgorthm;
public class SecondPrimeNumber {
public static int count = 0;
?public static void main(String[] args) {
???? for(int i=101;i<200;i++){
??? ?????boolean b = true;//
默認(rèn)此數(shù)就素?cái)?shù)
???????? for(intj=2;j<=Math.sqrt(i);j++){
???????????? if(i%j==0){
???????????????? b = false; //
此數(shù)不是素?cái)?shù)
???????????????? break;
???????????? }
???????? }
???????? if(b){
???????????? count++;
???????????? System.out.print(i+"");
???????? }
???? }
???? System.out.println("\n
素?cái)?shù)的個數(shù):"+count);
??? }
}
/*
【程序3】作者 南楓題目:打印出所有的"水仙花數(shù)(narcissus
number)"橘茉,所謂"水仙花數(shù)"是指一個三位數(shù)工腋,其各位數(shù)字立方和等于該數(shù)本身姨丈。例如:153是一個"水仙花數(shù)",因?yàn)?53=1的三次方+5的三次方+3的三次方擅腰。
1.
程序分析:利用for循環(huán)控制100-999個數(shù)蟋恬,每個數(shù)分解出個位,十位趁冈,百位歼争。*/
package cn.com.flywater.FiftyAlgorthm;
public class ThirdNarcissusNum {
static int b, bb, bbb;
public static void main(String[] args) {
?? for(int num=101; num<1000; num++) {
??? ThirdNarcissusNum tnn = newThirdNarcissusNum();
??? tnn.f(num);
?? }
}
public void f(int m) {
?? bbb = m / 100;
?? bb = (m % 100) / 10;
?? b = (m % 100) % 10;
?? if((bbb * bbb * bbb + bb * bb * bb + b* b * b) == m) {
??? System.out.println(m);
?? }
}
}
/*
【程序4】作者 南楓題目:將一個正整數(shù)分解質(zhì)因數(shù)。例如:輸入90,打印出90=2*3*3*5渗勘。程序分析:對n進(jìn)行分解質(zhì)因數(shù)沐绒,應(yīng)先找到一個最小的質(zhì)數(shù)k,然后按下述步驟完成:
(1)
如果這個質(zhì)數(shù)恰等于n旺坠,則說明分解質(zhì)因數(shù)的過程已經(jīng)結(jié)束乔遮,打印出即可。
(2)
如果n>k取刃,但n能被k整除申眼,則應(yīng)打印出k的值,并用n除以k的商,作為新的正整數(shù)你n,重復(fù)執(zhí)行第一步蝉衣。
(3)
如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步巷蚪。*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class FourthPrimeFactor {
static int n, k = 2;
public static void main(String[] args) {
?? Scanner s = new Scanner(System.in);
?? n = s.nextInt();
?? System.out.print(n + "=" );
?? FourthPrimeFactor fpf = newFourthPrimeFactor();
?? fpf.f(n);
}
public void f(int n) {
?? while(k <= n) {
??? if(k == n) {
??? ?System.out.println(n);
???? break;
??? } else if(n > k && n % k== 0) {
???? System.out.print(k + "*");
???? n = n / k;
???? f(n);
???? break;
??? } else if(n > k && n % k!= 0) {
???? k++;
???? f(n);
???? break;
??? }
?? }
}
}
/*
【程序5】作者 南楓題目:利用條件運(yùn)算符的嵌套來完成此題:學(xué)習(xí)成績>=90分的同學(xué)用A表示病毡,60-89分之間的用B表示,60分以下的用C表示屁柏。
1.
程序分析:(a>b)?a:b這是條件運(yùn)算符的基本例子啦膜。*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class FifthCondition {
//public static final int S1 = 90;
//public static final int S2 = 60;
static int grade;
public static void main(String[] args) {
?? Scanner str = new Scanner(System.in);
?? int s = str.nextInt();
?? FifthCondition fc = newFifthCondition();
?? grade = fc.compare(s);
?? if(grade == 1) {
??? System.out.print('A');
?? } else if(grade == 2) {
??? System.out.print('B');
?? } else {
??? System.out.println('C');
?? }
}
public int compare(int s) {
?? return s > 90 ? 1
???? : s > 60 ? 2
???? :3;
}
}
/*
【程序6】作者 南楓題目:輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)淌喻。
1.
程序分析:利用輾除法僧家。*/
/*
*
在循環(huán)中,只要除數(shù)不等于0裸删,用較大數(shù)除以較小的數(shù)八拱,將小的一個數(shù)作為下一輪循環(huán)的大數(shù),取得的余數(shù)作為下一輪循環(huán)的較小的數(shù)涯塔,如此循環(huán)直到較小的數(shù)的值為0肌稻,返回
*
較大的數(shù),此數(shù)即為最小公約數(shù)匕荸,最小公倍數(shù)為兩數(shù)之積除以最小公倍數(shù)爹谭。
* */
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class SixthCommonDiviser {
public static void main(String[] args) {
?? int a, b;
?? Scanner s1 = new Scanner(System.in);
?? Scanner s2 = new Scanner(System.in);
?? a = s1.nextInt();
?? b = s2.nextInt();
?? SixthCommonDiviser scd = newSixthCommonDiviser();
?? int m = scd.division(a, b);
?? int n = a * b / m;
?? System.out.println("
最大公約數(shù):" + m);
?? System.out.println("
最小公倍數(shù):" + n);
}
publicint division(int x, int y) {
?? int t;
?? if(x < y) {
??? t = x;
??? x = y;
??? y = t;
?? }
?? while(y != 0) {
??? if(x == y) return 1;
??? else {
???? int k = x % y;
???? x = y;
???? y = k;
??? }
?? }
? ?return x;
}
}
/*【程序7】作者 南楓題目:輸入一行字符,分別統(tǒng)計(jì)出其中英文字母榛搔、空格诺凡、數(shù)字和其它字符的個數(shù)东揣。
1.
程序分析:利用while語句,條件為輸入的字符不為'\n '. */
packagecn.com.flywater.FiftyAlgorthm;
import java.util.*;
public class SeventhCharacterStatistics {
static int digital = 0;
static int character = 0;
static int other = 0;
static int blank = 0;
public static void main(String[] args) {
?? char[] ch = null;
?? Scanner sc = new Scanner(System.in);
?? String s = sc.nextLine();
?? ch = s.toCharArray();
?? for(int i=0; i
??? if(ch[i] >= '0' && ch[i]<= '9') {
???? digital ++;
??? } else if((ch[i] >= 'a' &&ch[i] <= 'z') || ch[i] > 'A' && ch[i] <= 'Z') {
???? character ++;
??? } else if(ch[i] == ' ') {
???? blank ++;
??? } else {
???? other ++;
??? }
?? }
?? System.out.println("數(shù)字個數(shù):" + digital);
?? System.out.println("
英文字母個數(shù):" + character);
?? System.out.println("
空格個數(shù):" + blank);
?? System.out.println("
其他字符個數(shù):"+ other );
}
}
/*【程序8】作者 南楓題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字腹泌。例如2+22+222+2222+22222(此時(shí)共有5個數(shù)相加)嘶卧,幾個數(shù)相加有鍵盤控制。
*/
/*
*
算法: 定義一個變量b真屯,賦初值為0脸候;定義一變量sum,賦初值為0绑蔫,
*
進(jìn)入循環(huán)后运沦,將a + b 的值賦給b,將sum
+ b 的值賦給sum配深;
*
同時(shí)携添,將a 增加十倍,++ i篓叶; 繼續(xù)循環(huán)烈掠;
*
循環(huán)結(jié)束后,輸出sum 的值缸托。
*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
public class EightPlus {
static long a = 2, b = 0;
public static void main(String[] args) {
?? Scanner s = new Scanner(System.in);
?? int n = s.nextInt();
?? int i = 0;
?? long sum = 0;
?? while(i < n) {
??? b = b + a;
??? sum = sum + b;
??? a = a * 10;
??? ++ i;
?? }
?? System.out.println("input number:" + n);
?? System.out.println(sum);
}
}
/*【程序9】題目:一個數(shù)如果恰好等于它的因子之和左敌,這個數(shù)就稱為"完數(shù)"。例如6=1+2+3.編程找出1000以內(nèi)的所有完數(shù)俐镐。
*/
package cn.com.flywater.FiftyAlgorthm;
public class NinthWanshu {
publicstatic void main(String[] args) {
?? System.out.println("1
到1000的完數(shù)有:");
?? for(int i=1; i<1000; 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 + " ");
??? }
?? }
}
}
/*【程序10】作者 南楓題目:一球從100米高度自由落下矫限,每次落地后反跳回原高度的一半;再落下佩抹,求它在 第10次落地時(shí)叼风,共經(jīng)過多少米?第10次反彈多高棍苹?
*/
package cn.com.flywater.FiftyAlgorthm;
public class TenthTreeFall {
static double height = 100;
static double distance = 100;
public static void main(String[] args) {
?? for(int i=1; i<10; i++) {
??? distance = distance + height;
??? height = height / 2;
?? }
?? System.out.println("
路程:"+ distance);
?? System.out.println("
高度:"+ height / 2);
}
}
/*
【程序11】
*
作者 南楓題目:有1无宿、2、3枢里、4個數(shù)字孽鸡,能組成多少個互不相同且無重復(fù)數(shù)字的三位數(shù)?都是多少栏豺?
1.
程序分析:可填在百位梭灿、十位、個位的數(shù)字都是1冰悠、2堡妒、3、4溉卓。組成所有的排列后再去掉不滿足條件的排列皮迟。
*/
/*
算法:3個for循環(huán)加一個if語句搬泥;
*
*/
package cn.com.flywater.FiftyAlgorthm;
public class EleventhNumberRange {
public static void main(String[] args) {
?? int count = 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) {
????? ?count ++;
?????? System.out.print(x*100 + y*10 + z+ "?? ");
?????? if(count % 4 == 0) {
??????? System.out.println();
?????? }
????? }
???? }
??? }
?? }
?? System.out.println("
共有"
+ count + "個三位數(shù)");
}
}
/*【程序12】
*
作者 南楓題目:企業(yè)發(fā)放的獎金根據(jù)利潤提成。利潤(I)低于或等于10萬元時(shí)伏尼,獎金可提10%忿檩;利潤高于10萬元,低于20萬元時(shí)爆阶,低于10萬元的部分按10%提成燥透,高于10萬元的部分,可可提成7.5%辨图;20萬到40萬之間時(shí)班套,高于20萬元的部分,可提成5%故河;40萬到60萬之間時(shí)高于40萬元的部分吱韭,可提成3%;
60
萬到100萬之間時(shí)鱼的,高于60萬元的部分理盆,可提成1.5%,高于100萬元時(shí)凑阶,超過100萬元的部分按1%提成猿规,從鍵盤輸入當(dāng)月利潤I,求應(yīng)發(fā)放獎金總數(shù)宙橱?
1.
程序分析:請利用數(shù)軸來分界姨俩,定位。注意定義時(shí)需把獎金定義成長整型养匈。
*/
/*
注意: 要精確到小數(shù)點(diǎn)后多少位,用DecimalFormat df = new DecimalFormat("#0.0000");
*
*/
package cn.com.flywater.FiftyAlgorthm;
import java.text.DecimalFormat;
import java.util.*;
public class TwelfthProfitAward {
static double profit = 0;
static double award = 0;
public static void main(String[] args) {
?? Scanner s = new Scanner(System.in);
?? profit = s.nextInt();
?? System.out.println("
輸入的利潤是"
+ profit + "萬");
?? if(profit > 0 && profit<= 10) {
??? award = profit * 0.1;
?? } else if(profit > 10 &&profit <= 20) {
??? award = 10 * 0.1 + (profit - 10) *0.075;
?? } else if(profit > 20 &&profit <= 40) {
??? award = 10 * 0.1 + 10 * 0.075 +(profit - 20) * 0.05;
?? } else if(profit > 40 &&profit <= 60) {
??? award = 10 * 0.1 + 10 * 0.075 + 20 *0.05 + (profit - 40) * 0.03;
?? } else if(profit > 60 &&profit <= 100) {
??? award = 20 * 0.175 + 20 * 0.05 + 20 *0.03 + (profit - 60) * 0.015;
?? } else if(profit > 100) {
??? award = 20 * 0.175 + 40 * 0.08 + 40 *0.015 + (profit - 100) * 0.01;
?? }
?? DecimalFormat df = newDecimalFormat("#0.00000");
?? System.out.println("
應(yīng)該提取的獎金是" + df.format(award) + "萬");
}
}
/*【程序13】
*
作者 南楓題目:一個整數(shù)都伪,它加上100后是一個完全平方數(shù)呕乎,再加上168又是一個完全平方數(shù),請問該數(shù)是多少陨晶?
1.
程序分析:在10萬以內(nèi)判斷猬仁,先將該數(shù)加上100后再開方,再將該數(shù)加上268后再開方先誉,如果開方后的結(jié)果滿足如下條件湿刽,即是結(jié)果。請看具體分析:
*/
package cn.com.flywater.FiftyAlgorthm;
public class ThirteenthTwiceSqrt {
public static void main(String[] args) {
?? for(long l=1L; l<100000; l++) {
??? if(Math.sqrt((long)(l+100)) % 1 == 0){
???? if(Math.sqrt((long)(l+268)) % 1 ==0) {
????? System.out.println(l + "
加100是一個完全平方數(shù)褐耳,再加168又是一個完全平方數(shù)");
???? }
??? }
?? }
}
}
*【程序14】
*
作者 南楓題目:輸入某年某月某日诈闺,判斷這一天是這一年的第幾天?
1.
程序分析:以3月5日為例铃芦,應(yīng)該先把前兩個月的加起來雅镊,然后再加上5天即本年的第幾天襟雷,特殊情況,閏年且輸入月份大于3時(shí)需考慮多加一天仁烹。
*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.Scanner;
import java.io.*;
public class FourteenthYearMonthDay {
public static void main(String[] args) {
?? int year, month, day;
?? int days = 0;
?? int d = 0;
?? FourteenthYearMonthDay fymd = newFourteenthYearMonthDay();
?? System.out.print("Input theyear:");
?? year =fymd.input();
?? System.out.print("Input themonth:");
?? month = fymd.input();
?? System.out.print("Input TheDay:");
?? day = fymd.input();
?? if (year < 0 || month < 0 ||month > 12 || day < 0 || day > 31) {
??? System.out.println("Input error,please run this program again!");
??? System.exit(0);
?? }
?? for (int i=1; i
??? switch (i) {
??? case 1:
??? case 3:
??? case 5:
??? case 7:
??? case 8:
??? case 10:
??? case 12:
???? days = 31;
???? //d += days;
???? break;
??? case 4:
??? case 6:
??? case 9:
??? case 11:
???? days = 30;
???? //d += days;
???? break;
??? case 2:
???? if ((year % 400 == 0) || (year % 4== 0 && year % 100 != 0)) {
????? days = 29;
???? } else {
????? days = 28;
???? }
???? //d += days;
???? break;
??? }
??? d += days;
?? }
?? System.out.println(year +":" + month + ":" + day + "
是今年的第"
+ (d+day) + "天耸弄。");
}
public int input() {
?? int value = 0;
?? Scanner s = new Scanner(System.in);
?? value = s.nextInt();
?? return value;
}
}
/*【程序15】
*
作者??南楓題目:輸入三個整數(shù)x,y,z,請把這三個數(shù)由小到大輸出卓缰。
1.
程序分析:我們想辦法把最小的數(shù)放到x上计呈,先將x與y進(jìn)行比較,如果x>
y則將x與y的值進(jìn)行交換征唬,然后再用x與z進(jìn)行比較捌显,如果x>
z則將x與z的值進(jìn)行交換,這樣能使x最小鳍鸵。
*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.*;
public class FifteenthNumberCompare {
public static void main(String[] args) {
?? FifteenthNumberCompare fnc = newFifteenthNumberCompare();
?? int a, b, c;
?? System.out.println("Input 3numbers:");
?? a = fnc.input();
?? b = fnc.input();
?? c = fnc.input();
//
//?? fnc.compare(a, b);//
方法調(diào)用不能通過改變形參的值來改變實(shí)參的值
//?? fnc.compare(b, c);//
這種做法是錯的
//?? fnc.compare(a, c);
?? //System.out.println("result:"+ a +" " + b + " " + c);//
沒有改變
?? if(a > b) {
??? int t = a;
??? a = b;
??? b = t;
?? }
?? if(a > c) {
??? int t = a;
??? a = c;
??? c = t;
?? }
?? if(b > c) {
??? int t = b;
??? b = c;
??? c = t;
?? }
?? System.out.println( a + " "+ b + " " + c);
}
public int input() {
?? int value = 0;
?? Scanner s = new Scanner(System.in);
?? value = s.nextInt();
?? return value;
}
public void compare(int x, int y) {//
此方法沒用
?? if(x > y) {
??? int t = x;
??? x = y;
??? y = t;
?? }
}
}
/*【程序16】
*
作者 南楓
*
題目:輸出9*9口訣苇瓣。
*1.
程序分析:分行與列考慮,共9行9列偿乖,i控制行击罪,j控制列。
**/
package cn.com.flywater.FiftyAlgorthm;
public class SixteenthMultiplicationTable {
public static void main(String[] args) {
?? for(int i=1; i<10; i++) {
??? for(int j=1; j<=i; j++) {
???? System.out.print(j + "*" +i + "=" + j*i + " " );
??? }
??? System.out.println();
?? }
}
}
//
【程序17】
//
作者 南楓
//
題目:猴子吃桃問題:猴子第一天摘下若干個桃子贪薪,當(dāng)即吃了一半媳禁,還不癮,
//
又多吃了一個 第二天早上又將剩下的桃子吃掉一半画切,又多吃了一個竣稽。
//
以后每天早上都吃了前一天剩下 的一半零一個。到第10天早上想再吃時(shí)霍弹,
//
見只剩下一個桃子了毫别。求第一天共摘了多少。
//1.
程序分析:采取逆向思維的方法典格,從后往前推斷岛宦。
packagecn.com.flywater.FiftyAlgorthm;
public class SeventeenthMonkeyPeach {
public static void main(String[] args) {
?? int lastdayNum = 1;
?? for(int i=2; i<=10; i++) {
??? lastdayNum = (lastdayNum+1) * 2;
?? }
?? System.out.println("
猴子第一天摘了" + lastdayNum + " 個桃子");
}
}
/*
【程序18】
*
作者 南楓題目:兩個乒乓球隊(duì)進(jìn)行比賽,各出三人耍缴。甲隊(duì)為a,b,c三人砾肺,乙隊(duì)為x,y,z三人。已抽簽決定比賽名單防嗡。有人向隊(duì)員打聽比賽的名單变汪。a說他不和x比,c說他不和x,z比蚁趁,請編程序找出三隊(duì)賽手的名單裙盾。
*/
/*
*
這個程序?qū)懙煤懿缓茫侵澜Y(jié)果后拼湊起來的,還不如直接寫輸出語句加上結(jié)果來的好闷煤。
*/
package cn.com.flywater.FiftyAlgorthm;
public class EighteenthPingpong {
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[i] == 'a' && n[j] =='x') {
????? continue;
???? } else if (m[i] == 'a' &&n[j] == 'y') {
????? continue;
???? } else if ((m[i] == 'c' &&n[j] == 'x')
?????? || (m[i] == 'c' && n[j] =='z')) {
????? continue;
???? } else if ((m[i] == 'b' &&n[j] == 'z')
?????? || (m[i] == 'b' && n[j] =='y')) {
????? continue;
???? } else
????? System.out.println(m[i] + " vs" + n[j]);
??? }
?? }
}
}
題目:打印出如下圖案(菱形)
?? *
? ***
?*****
*******
?*****
? ***
?? *
1.
程序分析:先把圖形分成兩部分來看待童芹,前四行一個規(guī)律,后三行一個規(guī)律鲤拿,利用雙重for循環(huán)假褪,第一層控制行,第二層控制列近顷。
*/
/*
上半部分循環(huán)變量的控制方法是
* for(int i=0; i<(HEIGHT+1) / 2; i++) {
?? for(int j=1; j
?? for(int k=1; k<(i+1)*2; k++) {
下半部分循環(huán)變量的控制方法是
for(int i=1; i<=HEIGHT/2; i++) {
?? for(int j=1; j<=i; j++) {
*??? for(int k=1; k<=WIDTH-2*i-1; k++){
*/
package cn.com.flywater.FiftyAlgorthm;
public class NineteenthPrintRhombic {
static final int HEIGHT = 7;
static final int WIDTH = 8;
public static void main(String[] args) {
?? for(int i=0; i<(HEIGHT+1) / 2; i++){
??? for(int j=1; j
???? System.out.print(" ");
??? }
??? for(int k=1; k<(i+1)*2; k++) {
???? System.out.print('*');
??? }
??? System.out.println();
?? }
?? for(int i=1; i<=HEIGHT/2; i++) {
??? for(int j=1; j<=i; j++) {
???? System.out.print(" ");
??? }
??? for(int k=1; k<=WIDTH-2*i-1; k++){
???? System.out.print('*');
??? }
??? System.out.println();
?? }
}
}
上半部分第二重循環(huán)應(yīng)改為:??? for(int j=0; j
上半部分第三重循環(huán)應(yīng)改為:??? for(int k=1; k<=WIDTH-2*i; k++)
/*
【程序20】
*
作者 南楓題目:有一分?jǐn)?shù)序列:2/1生音,3/2,5/3窒升,8/5缀遍,13/8,21/13...求出這個數(shù)列的前20項(xiàng)之和饱须。
1.
程序分析:請抓住分子與分母的變化規(guī)律域醇。
*/
packagecn.com.flywater.FiftyAlgorthm;
import java.text.DecimalFormat;
public class TwentiethFractionSum {
public static void main(String[] args) {
?? int x = 2, y = 1, t;
?? double sum = 0;
?? DecimalFormat df = newDecimalFormat("#0.0000");
?? for(int i=1; i<=20; i++) {
??? sum += (double)x / y;
??? t = y;
??? y = x;
??? x = y + t;
??? System.out.println("
第" + i + " 次相加,和是" + df.format(sum));
?? }
}