編程合集: https://www.cnblogs.com/jssj/p/12002760.html
Java總結:https://www.cnblogs.com/jssj/p/11146205.html
【程序11】
題目:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?
/** * 【程序11】 * 題目:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少? */ public class Subject11 { public static void main(String[] args) { FormThreeNum(); } public static void FormThreeNum(){ for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { for (int k = 1; k <= 4; k++) { if(i != j && j!= k && k != i){ System.out.println("1,2,3,4可以組成的三位數:"+i+j+k); } } } } } }
運行結果:
。。。。。。。
【程序12】
題目:企業發放的獎金根據利潤提成。利潤(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%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?
/** * 【程序12】 * 題目:企業發放的獎金根據利潤提成。利潤(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%提成,從鍵盤輸入當月利潤I,求應發放獎金總數? */ public class Subject12 { public static void main(String[] args) { System.out.println("請輸入您的業績:"); Scanner scanner = new Scanner(System.in); double profit= scanner.nextDouble(); System.out.println("您得到的提成為:"+premium(profit)); } public static double premium(double profit){ double premium = 0; if(profit <= 100000){ premium = profit*0.1; }else if(profit > 100000 && profit <= 200000){ premium = (profit - 100000) * 0.075 + premium(100000); }else if(profit > 200000 && profit <= 400000){ premium = (profit - 200000) * 0.05 + premium(200000); }else if(profit > 400000 && profit <= 600000){ premium = (profit - 400000) * 0.03 + premium(400000); }else if(profit > 600000 && profit <= 1000000){ premium = (profit - 600000) * 0.015 + premium(600000); }else{ premium = (profit - 1000000) * 0.01 + premium(1000000); } return premium; } }
運行結果:
【程序13】
題目:一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?
/** * 【程序13】 * 題目:一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少? */ public class Subject13 { public static void main(String[] args) { squareNum(); } public static void squareNum(){ int i = 1; while(true){ int tmp = i*i; int z = 1; while(true){ if(z*z - tmp >=168){ break; } z++; } if(z*z - i*i == 168 && (z*z - 168-100)>=0){ System.out.println("一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,則該數為:"+(z*z - 168-100)); break; }else{ i++; } } } }
運行結果:
【程序14】
題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
/** * 【程序14】 * 題目:輸入某年某月某日,判斷這一天是這一年的第幾天? */ public class Subject14 { public static void main(String[] args) { dayNum("2018-03-08"); dayNum("1900-03-08"); dayNum("2000-03-08"); dayNum("2020-03-08"); dayNum("2018-02-08"); dayNum("1900-02-08"); dayNum("2000-02-08"); dayNum("2020-02-08"); } public static void dayNum(String date){ String[] dateStr = date.split("-"); int dayNum = 0; if(leapYear(dateStr[0])){ dayNum = month(dateStr[1] ,"1") + Integer.parseInt(dateStr[2]); }else{ dayNum = month(dateStr[1] ,"0") + Integer.parseInt(dateStr[2]); } System.out.println("輸入日期為該("+dateStr[0]+")年的第"+dayNum+"天"); } /** * 根據年份判斷閏年還是普通年 * @param year * @return */ public static boolean leapYear(String year){ int years = Integer.parseInt(year); if((years%4 ==0 && years%100!=0) || years%400 == 0){ return true; } return false; } /** * 根據月份獲取天數 * @param month * @param flag * @return */ public static int month(String month,String flag){ int dayNum = 0; switch (month){ case "01": dayNum = 0; break; case "02": dayNum = month("01", flag) +31; break; case "03": if("0".equals(flag)) { dayNum = month("02", flag) + 28; }else { dayNum = month("02", flag) + 29; } break; case "04": dayNum = month("03", flag) + 31; break; case "05": dayNum = month("04", flag) + 30; break; case "06": dayNum = month("05", flag) + 31; break; case "07": dayNum = month("06", flag) + 30; break; case "08": dayNum = month("07", flag) + 31; break; case "09": dayNum = month("08", flag) + 31; break; case "10": dayNum = month("09", flag) + 30; break; case "11": dayNum = month("10", flag) + 31; break; case "12": dayNum = month("11", flag) + 30; break; } return dayNum; } }
運行結果:
【程序15】
題目:輸入三個整數x,y,z,請把這三個數由小到大輸出
/** * 【程序15】 * 題目:輸入三個整數x,y,z,請把這三個數由小到大輸出 */ public class Subject15 { public static void main(String[] args) { minNum(7,3,5); } /** * 獲取最小數 * @param a * @param b * @param c */ public static void minNum(int a,int b,int c){ int tmp = 0; int[] nums= {a,b,c}; int[] numTmp = new int[3]; for(int i=0 ; i<2;i++){ for (int j = i+1; j <= 2; j++) { if(nums[j] < nums[i]){ tmp = nums[j]; nums[j] = nums[i]; nums[i] = tmp; } } } for (int i = 0; i < nums.length ; i++) { System.out.println(nums[i]); } } }
運行結果:
【程序16】
題目:輸出9*9口訣。
/** * 【程序16】 * 題目:輸出9*9口訣。 */ public class Subject16 { public static void main(String[] args) { for (int i = 1; i <= 9 ; i++) { for (int j = 1; j <= i ; j++) { System.out.print(i+"*"+j+"="+i*j+" "); } System.out.println(""); } } }
運行結果:
【程序17】
題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。
/** * 【程序17】 * 題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個第二天早上又將剩下的桃子吃掉一半, * 又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。 */ public class Subject17 { public static void main(String[] args) { System.out.println(peachNum(10)); } public static int peachNum(int days){ if(days == 1){ return 1; }else if(days > 1){ return (peachNum(days-1) + 1) * 2; } return 0; } }
運行結果:
【程序18】
題目:兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。
除了題目本身還擴展實現了,總共可以出現多少種組合
/** * 【程序18】 * 題目:兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。 * a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。 * * 除了題目本身還擴展實現了,總共可以出現多少種組合 */ public class Subject18 { public static void main(String[] args) { List<Map> list = new ArrayList<>(); Map<Character, Character> map = new HashMap<>(); List<Character> a = new ArrayList<>(); a.add('a'); a.add('b'); a.add('c'); List<Character> b = new ArrayList<>(); b.add('x'); b.add('y'); b.add('z'); group(a,b,map,list); for(Map<Character, Character> maptmp:list){ boolean flag = true; for (Map.Entry<Character, Character> entry : maptmp.entrySet()) { /***將不滿足條件的情況去除掉*****/ if(((entry.getKey() =='a' && 'x' == entry.getValue()) || ((entry.getKey() =='c' && 'x' == entry.getValue()) || (entry.getKey() =='c' && 'z' == entry.getValue())))){ flag = false; break; } } if(flag){ System.out.println("對決名單為:"); for (Map.Entry<Character, Character> entry : maptmp.entrySet()) { System.out.println("選手:"+entry.getKey() + " ----------VS------------ " + "選手:"+ entry.getValue()); } } } } /** * 將全部分組情況展示出來 * @param a * @param b * @param map * @param list */ public static void group(List<Character> a,List<Character> b,Map<Character, Character> map,List<Map> list){ for (int i = 0; i < a.size(); i++) { if (i == 1) { break; } for (int j = 0; j < a.size(); j++) { map.put(a.get(i), b.get(j)); //System.out.println(a.get(i) + "----" + b.get(j)); if(a.size() == 1){ Map<Character, Character> mapTrue = new HashMap<>(); mapCopy(map,mapTrue); list.add(mapTrue); //System.out.println(mapTrue); } List<Character> tmp1 = new ArrayList<>(); List<Character> tmp2 = new ArrayList<>(); listCopy(a,tmp1); listCopy(b,tmp2); tmp1.remove(a.get(i)); tmp2.remove(b.get(j)); group(tmp1,tmp2,map,list); } } } /** * 將paramsMap內容復制到resultMap中 * @param paramsMap * @param resultMap */ public static void mapCopy(Map paramsMap, Map resultMap) { if (resultMap == null) resultMap = new HashMap(); if (paramsMap == null) return; Iterator it = paramsMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); resultMap.put(key, paramsMap.get(key) != null ? paramsMap.get(key) : ""); } } /** * 將paramsList內容復制到resultList中 * @param paramsList * @param resultList */ public static void listCopy(List paramsList,List resultList){ for (int k = 0; k < paramsList.size(); k++) { resultList.add(paramsList.get(k)); } } }
運行結果:
【程序19】
題目:打印出如下圖案(菱形)
/** * 【程序19】 * 題目:打印出如下圖案(菱形) */ public class Subject19 { public static void main(String[] args) { diamond(6); } /** * 打印菱形 */ public static void diamond(int a){ for (int i = a; i > 0; i--) { for (int j = 0; j < i; j++) { System.out.print(" "); } for (int j = 0; j < a-i; j++) { System.out.print("* "); } System.out.println(""); } for (int i = 0; i < a ; i++) { for (int j = 0; j < i; j++) { System.out.print(" "); } for (int j = 0; j < a-i; j++) { System.out.print("* "); } System.out.println(""); } } }
運行結果:
【程序20】
題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13…求出這個數列的前20項之和。
/** * 【程序20】 * 題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13…求出這個數列的前20項之和。 */ public class Subject20 { public static void main(String[] args) { double sumNum = sum(20); System.out.println("前20規律數之和為:"+sumNum); } /** * 前num的數的和 * @param num */ public static double sum(int num){ if(num == 1){ return 2.0/1.0; }else{ double a = getMolecule(num); double b = getMolecule(num-1); return sum(num-1)+ a/b; } } /** * 按照規律獲取分子 * @param num * @return */ public static double getMolecule(int num){ if(num == 1){ return 2.0; }else if(num == 2){ return 3.0; }else{ return getMolecule(num-1) + getMolecule(num-2); } } }
運行結果:
參考:https://blog.csdn.net/zzzzzzzhu/article/details/82355001