40道java经典算法(二)(11-20)

/*【程序11】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
*/
/*算法:3个for循环加一个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 + "个三位数");
}
}

/*【程序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,求应发放奖金总数?
1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
*/
/*注意: 要精确到小数点后多少位,用 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 = new DecimalFormat("#0.00000");
 
   System.out.println("应该提取的奖金是 " + df.format(award) + "万");
}
}

/*【程序13】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,
如果开方后的结果满足如下条件,即是结果。请看具体分析:
*/
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是一个完全平方数,再加168又是一个完全平方数");
     }
    }
   }
}
}

*【程序14】
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,
然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
*/
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 = new FourteenthYearMonthDay();
 
   System.out.print("Input the year:");
   year =fymd.input();
   System.out.print("Input the month:");
   month = fymd.input();
   System.out.print("Input The Day:");
   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 <month; 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】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x> y则将x与y的值进行交换,然后再用x与z进行比较,如果x> z则将x与z的值进行交换,这样能使x最小。
*/
package cn.com.flywater.FiftyAlgorthm;
import java.util.*;
public class FifteenthNumberCompare {
public static void main(String[] args) {
   FifteenthNumberCompare fnc = new FifteenthNumberCompare();
   int a, b, c;
 
   System.out.println("Input 3 numbers:");
   a = fnc.input();
   b = fnc.input();
   c = fnc.input();
//
//   fnc.compare(a, b);//方法调用不能通过改变形参的值来改变实参的值
//   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】
//题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,
//又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
//以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,
//见只剩下一个桃子了。求第一天共摘了多少。
//1.程序分析:采取逆向思维的方法,从后往前推断。

package cn.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】
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。
已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
*/
/*
* 这个程序写得很不好,是知道结果后拼凑起来的,还不如直接写输出语句加上结果来的好。
*/
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.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,
第一层控制行,第二层控制列。
*/
/*上半部分循环变量的控制方法是
* for(int i=0; i<(HEIGHT+1) / 2; i++) {
   for(int j=1; j<WIDTH/2-i; j++) {
   for(int k=1; k<(i+1)*2; k++) {
 
    下半部分循环变量的控制方法是
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<WIDTH/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<=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();
   }
}
}
上半部分第二重循环应改为:    for(int j=0; j<WIDTH/2-i; j++)
上半部分第三重循环应改为:    for(int k=1; k<=WIDTH-2*i; k++)
/*【程序20】
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
1.程序分析:请抓住分子与分母的变化规律。
*/

package cn.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 = new DecimalFormat("#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));
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值