50道基礎(chǔ)編程題練習(xí)你的邏輯思維(二)
【程序11】
題目:有1、2、3、4個(gè)數(shù)字,能組成多少個(gè)互不相同且無重復(fù)數(shù)字的三位數(shù)?都是多少?
程序分析:可填在百位、十位、個(gè)位的數(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+"個(gè)");
????}
}
【程序12】
題目:企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤(rùn)提成。利潤(rùn)(I)低于或等于10萬元時(shí),獎(jiǎng)金可提10%;利潤(rùn)高于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)月利潤(rùn)I,求應(yīng)發(fā)放獎(jiǎng)金總數(shù)?
程序分析:請(qǐng)利用數(shù)軸來分界,定位。注意定義時(shí)需把獎(jiǎng)金定義成長(zhǎng)整型。
import java.io.*;
public class Prog12{
????public static void main(String[] args){
????????System.out.print("請(qǐng)輸入當(dāng)前利潤(rùn):");
????????long profit = Long.parseLong(key_Input());
????????System.out.println("應(yīng)發(fā)獎(jiǎng)金:"+bonus(profit));
????}
????//接受從鍵盤輸入的內(nèi)容
????private static String key_Input(){
????????????String str = null;
????????????BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));
????????????try{
????????????????str = bufIn.readLine();
????????????}catch(IOException e){
????????????????e.printStackTrace();
????????????}finally{
????????????try{
????????????????bufIn.close();
????????????}catch(IOException e){
????????????????e.printStackTrace();
????????????}
????????}
????????return str;
????}
????//計(jì)算獎(jiǎng)金
????private static long bonus(long profit){
????????long prize = 0;
????????long profit_sub = profit;
????????if(profit>1000000){
????????????profit = profit_sub-1000000;
????????????profit_sub = 1000000;
????????????prize += profit*0.01;
????????}
????????if(profit>600000){
????????????profit = profit_sub-600000;
????????????profit_sub = 600000;
????????????prize += profit*0.015;
????????}
????????if(profit>400000){
????????????profit = profit_sub-400000;
????????????profit_sub = 400000;
????????????prize += profit*0.03;
????????}
????????if(profit>200000){
????????????profit = profit_sub-200000;
????????????profit_sub = 200000;
????????????prize += prize*0.05;
????????}
????????if(profit>100000){
????????????profit = profit_sub-100000;
????????????profit_sub = 100000;
????????????prize += profit*0.075;
????????}
????????prize += profit_sub*0.1;
????????return prize;
????}
}
?
【程序13】
題目:一個(gè)整數(shù),它加上100后是一個(gè)完全平方數(shù),再加上168又是一個(gè)完全平方數(shù),請(qǐng)問該數(shù)是多少?
程序分析:在10萬以內(nèi)判斷,先將該數(shù)加上100后再開方,再將該數(shù)加上268后再開方,如果開方后的結(jié)果滿足如下條件,即是結(jié)果。
public class Prog13{
public static void main(String[] args){
????int n=0;
????for(int i=0;i<100001;i++){
????????if(isCompSqrt(i+100) && isCompSqrt(i+268)){
????????????n = i;
????????????break;
????????}
????}
????System.out.println("所求的數(shù)是:"+n);
}
//判斷完全平方數(shù)
private static boolean isCompSqrt(int n){
????boolean isComp = false;
????for(int i=1;i<Math.sqrt(n)+1;i++){
????????if(n==Math.pow(i,2)){
????????????isComp = true;
????????????break;
????????????}
????????}
????????return isComp;
????}
}
【程序14】
題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
程序分析:以3月5日為例,應(yīng)該先把前兩個(gè)月的加起來,然后再加上5天即本年的第幾天,特殊情況,閏年且輸入月份大于3時(shí)需考慮多加一天。
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("請(qǐng)輸入當(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<month;i++)
??????????n += month_date[i];
????????return n+date;
????}
}
【程序15】
題目:輸入三個(gè)整數(shù)x,y,z,請(qǐng)把這三個(gè)數(shù)由小到大輸出。
程序分析:我們想辦法把最小的數(shù)放到x上,先將x與y進(jìn)行比較,如果x>y則將x與y的值進(jìn)行交換,然后再用x與z進(jìn)行比較,如果x>z則將x與z的值進(jìn)行交換,這樣能使x最小。
import java.util.Scanner;
public class Prog15{
????public static void main(String[] args){
????Scanner scan = new Scanner(System.in).useDelimiter("\\D");
????System.out.print("請(qǐng)輸入三個(gè)數(shù):");
????int x = scan.nextInt();
????int y = scan.nextInt();
????int z = scan.nextInt();
????scan.close();
????System.out.println("排序結(jié)果:"+sort(x,y,z));
}
//比較兩個(gè)數(shù)的大小
private static String sort(int x,int y,int z){
????String s = null;
????if(x>y){
????????int t = x;
????????x = y;
????????y = t;
????}
????if(x>z){
????????int t = x;
????????x = z;
????????z = t;
????}
????if(y>z){
????????int t = z;
????????z = y;
????????y = t;
????}
????s = x+" "+y+" "+z;
????return s;
????}
}
【程序16】
題目:輸出9*9口訣。
程序分析:分行與列考慮,共9行9列,i控制行,j控制列。
public class Prog16{
????public static void main(String[] args){
????????for(int i=1;i<10;i++){
????????????for(int j=1;j<i+1;j++)
????????????System.out.print(j+"*"+i+"="+(j*i)+" ");
????????????System.out.println();
????????}
????}
}
【程序17】
題目:猴子吃桃問題:猴子第一天摘下若干個(gè)桃子,當(dāng)即吃了一半,還不癮,又多吃了一個(gè) 第二天早上又將剩下的桃子吃掉一半,又多吃了一個(gè)。以后每天早上都吃了前一天剩下的一半零一個(gè)。到第10天早上想再吃時(shí),見只剩下一個(gè)桃子了。求第一天共摘了多少。
程序分析:采取逆向思維的方法,從后往前推斷。
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+"桃子");
????}
}
【程序18】
題目:兩個(gè)乒乓球隊(duì)進(jìn)行比賽,各出三人。甲隊(duì)為a,b,c三人,乙隊(duì)為x,y,z三人。已抽簽決定比賽名單。有人向隊(duì)員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請(qǐng)編程序找出三隊(duì)賽手的名單。
import java.util.ArrayList;
public class Prog18{
????String a,b,c;//甲隊(duì)成員
????public static void main(String[] args){
????????String[] racer = {"x","y","z"};//乙隊(duì)成員
????????ArrayList<Prog18> arrayList = new ArrayList<Prog18>();
????????for(int i=0;i<3;i++)
??????????for(int j=0;j<3;j++)
????????????for(int k=0;k<3;k++){
???????????? Prog18 prog18 = new Prog18(racer[i],racer[j],racer[k]);
???????????? if(!prog18.a.equals(prog18.b) && !prog18.a.equals(prog18.c) && !prog18.b.equals(prog18.c) &&
???????????? ???!prog18.a.equals("x") && !prog18.c.equals("x") && !prog18.c.equals("z"))
???????????? ???arrayList.add(prog18);
????????????}
??????????for(Object obj:arrayList)
????????????System.out.println(obj);
????????}
????????//構(gòu)造方法
????????private Prog18(String a,String b,String c){
????????????this.a = a;
????????????this.b = b ;
????????????this.c = c;
????????????}
????????????public String toString(){
????????????return "a的對(duì)手是"+a+" ?"+"b的對(duì)手是"+b+" ?"+"c的對(duì)手是"+c;
????}
}
【程序19】
題目:打印出如下圖案(菱形)
??? *
?? ***
?******
********
?******
? ***
?? *
程序分析:先把圖形分成兩部分來看待,前四行一個(gè)規(guī)律,后三行一個(gè)規(guī)律,利用雙重 for循環(huán),第一層控制行,第二層控制列。
public class Prog19{
????public static void main(String[] args){
????????int n = 5;
????????printStar(n);
????}
????//打印星星
????private static void printStar(int n){
????????//打印上半部分
????????for(int i=0;i<n;i++){
????????????for(int j=0;j<2*n;j++){
?????????????? if(j<n-i)
?????????????? ??System.out.print(" ");
?????????????? if(j>=n-i && j<=n+i)
?????????????? ??System.out.print("*");
??????????????}
??????????System.out.println();
????}
????//打印下半部分
????for(int i=1;i<n;i++){
????????System.out.print(" ");
????????????for(int j=0;j<2*n-i;j++){
????????????if(j<i)
?????????????? ??System.out.print(" ");
?????????????? if(j>=i && j<2*n-i-1)
?????????????? ??System.out.print("*");
????????????}
????????????System.out.println();
????????}
????}
}
【程序20】
題目:有一分?jǐn)?shù)序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個(gè)數(shù)列的前20項(xiàng)之和。
程序分析:請(qǐng)抓住分子與分母的變化規(guī)律。
public class Prog20{
????public static void main(String[] args){
????????double n1 = 1;
????????double n2 = 1;
????????double fraction = n1/n2;
????????double Sn = 0;
????????for(int i=0;i<20;i++){
??????????double t1 = n1;
??????????double t2 = n2;
??????????n1 = t1+t2;
??????????n2 = t1;
??????????fraction = n1/n2;
??????????Sn += fraction;
????????}
????????System.out.print(Sn);
}
}