编程经典问题及其Java求解(二)
编程经典问题的第一部分贴出来后,一些朋友指出我的一些程序把问题复杂化了,而且给出了更简单的解法。非常感谢这些留言,使我学习到了东西。现在我再贴出今天做的几个题
package com.sailor.game;
import java.util.Scanner;
/**
* 题目:企业发放的奖金根据利润提成。利润(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,求应发放奖金总数?
*
* @author Sailor
*
*/
public class Prize_Problem {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入这个月的利润(万元):");
double profit = in.nextDouble();
System.out.println("奖金为(万元):" + getPrize(profit));
}
public static double getPrize(double profit) {
double temp;
double prize = 0;
if (profit > 100) {
temp = profit - 100;
prize += temp * 0.01;
profit -= temp;
}
if (profit > 60) {
temp = profit - 60;
prize += temp * 0.015;
profit -= temp;
}
if (profit > 40) {
temp = profit - 40;
prize += temp * 0.03;
profit -= temp;
}
if (profit > 20) {
temp = profit - 20;
prize += temp * 0.05;
profit -= temp;
}
if (profit > 10) {
temp = profit - 10;
prize += temp * 0.075;
profit -= temp;
}
if (profit <= 10) {
prize += profit * 0.1;
}
return prize;
}
}
package com.sailor.game;
import java.util.Scanner;
/**
* 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
*
* @author Sailor
*/
public class Reverse_Order_Output {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个不多于五位的整数:");
int n = in.nextInt();
if (n > 99999 || n < 0) {
System.out.println("输入的数不符合要求");
System.exit(1);
}
System.out.println(getDigitS(n));
System.out.println(reverse_order(n));
}
// 将一个不大于5位的整数的反序后的数返回
public static int reverse_order(int n) {
int result = 0;
int exp = 0;// 从最高位开始累加
for (int i = 4; i >= 0; i--) {
int temp = (int) (n / Math.pow(10, i));
if (temp > 0 || exp > 0) {
result += (int) (temp * Math.pow(10, exp));
exp++;
n = (int) (n % Math.pow(10, i));
}
}
return result;
}
// 获得一个不大于5位数的位数
public static String getDigitS(int n) {
String digits = "";
if (n / 10000 > 0) {
digits = "五位数";
} else if (n / 1000 > 0) {
digits = "四位数";
} else if (n / 100 > 0) {
digits = "三位数";
} else if (n / 10 > 0) {
digits = "二位数";
} else {
digits = "一位数";
}
return digits;
}
}
package com.sailor.game;
import java.util.Scanner;
/**
* @author Sailor
*
* 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
*
*/
public class StringCaculate {
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner in = new Scanner(System.in);
String str = in.nextLine();
System.out.println(getResult(str));
}
@SuppressWarnings("static-access")
public static String getResult(String str) {
int digitNum = 0;// 数字个数
int letterNum = 0;// 字母个数
int blankNum = 0;// 空格个数
int otherNum = 0;// 其他符号个数
for (int i = 0; i < str.length(); i++) {
Character ch = new Character(str.charAt(i));
if (ch.isDigit(ch)) {
digitNum++;
} else if (ch.isLetter(ch)) {
letterNum++;
} else if (ch.isSpaceChar(ch)) {
blankNum++;
} else {
otherNum++;
}
}
return str + "中\n数字个数为:" + digitNum + "\n字母个数为:" + letterNum
+ "\n空格个数为:" + blankNum + "\n其他字符个数为: " + otherNum;
}
}
package com.sailor.game;
/**
* 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。 程序分析:请抓住分子与分母的变化规律。
*
* @author Sailor
*
*/
public class Sum_Problem {
public static void main(String[] args) {
double sum = 0;
String formula = "";
for (int i = 1; i <= 20; i++) {
formula += (getFibonacci(i + 2) + "/" + getFibonacci(i + 1));
formula += (i == 20 ? "" : "+");
sum += getFibonacci(i + 2) / getFibonacci(i + 1);
}
System.out.println(formula + "=" + sum);
}
/**
* 获得斐波拉契数列的项
*
* @param n
* @return
*/
public static int getFibonacci(int n) {
if (n == 1 || n == 2)
return 1;
else
return getFibonacci(n - 1) + getFibonacci(n - 2);
}
}
package com.sailor.game;
import java.util.Scanner;
/**
*
* @author Sailor
* 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),
* 几个数相加有键盘控制。
*
*/
public class Superposition {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
long result = 0;
Scanner in = new Scanner(System.in);
System.out.println("请依次输入相加次数、数字");
num1 = in.nextInt();
num2 = in.nextInt();
String resultStr = "";
for (int i = 1; i <= num1; i++) {
for(int j=0;j<i;j++){
resultStr+=num2;
}
if(i!=num1){
resultStr+="+";
}
result += (num2 * i * (Math.pow(10, num1-i)));
}
System.out.println(resultStr+"="+result);
}
}
广告打到这儿来了~~ :o
恩,实现方式很多,而且我感觉在做Java web开发的时候接触算法的机会比较少,而且一般也不是在算法上做效率优化,而是在数据库方面做优化。不过这些算法题在面试的时候还是需要用到的。
说下最后一个题吧,可以通过递归来实现.
呵呵,算法复杂度这个度量方法我不是很清楚啊,当时学算法课的时候这个就让我很头痛~~
恩,这样挺好。等待楼主的系列作品。最好把负责度的分析也写上。 哪个大牛可以出一贴,专门讨论如何计算算法复杂度的,评价算法优劣的帖子。 估计很多同学都不了解,这些时间负责度O(nlogn)类似的是如何计算出来的。 投个良好,鼓励下。