经典的Java面试题【多测师_王sir】
package duoceshi.test; public class Duoceshi { public static float sum(float p, float q, int m, int n) { float sum = 0f; //如果购买的商品数量n小于套装里面的数量那么就买单支 if (n < m) { sum = p * n; } //拿商品的单价和套装里面的单个商品价格比较,如果单价的价格更便宜则直接用单价购买 else if(p<(m/q)) { sum = p * n; } else{ //先买套装,剩余数量不足套装的买单个 int t = n / m; int d = n % m; sum = t * q + d * d; } System.out.println("总共花费"+sum); return sum; } public static void main(String[] args) { Duoceshi.sum(2,30,40,178); } }
运行结果:
总共花费444.0