用Java实现以下问题 最好用算法来实现?

用java实现以下场景,有100条商品,每个商品的价格0到1000元不等
商品数据:

{ id: 1, productName: '特价商品1', price: 200 },

{ id: 2, productName: '特价商品2', price: 400 },

{ id: 3, productName: '特价商品3', price: 600 },

{ id: 4, productName: '特价商品4', price: 800 },

例如200元 加价之后就是240元 利润是40元

首先把0到300元的商品的加价30%,301到500的加价20%,501到1000元商品的加价10%,
然后0到300元的商品订单有300条,301-500的商品订单有400条,501-1000的商品订单有300条,
最后计算他的总利润有多少,最好写出算法


回答:

只计算总利润,那么关注每个元素的 price 即可。以下是一段伪代码示意:

CALC(A)

R = 0

N = A.length

for i in 0..N

P = A[i].price

if P <= 300

R += P * 0.3

else if P <= 500

R += P * 0.2

else

R += P * 0.1

return R

若是计算的同时又需要修改,则可以加上这些:

CALC(A)

R = 0

N = A.length

for i in 0..N

P = A[i].price

if P <= 300

R += P * 0.3

+ A[i].price *= 1.3

else if P <= 500

R += P * 0.2

+ A[i].price *= 1.2

else

R += P * 0.1

+ A[i].price *= 1.1;

return R


回答:

你确定这不是什么JAVA课的编程小作业吗..

import java.util.ArrayList;

import java.util.List;

class Product {

private int id;

private String productName;

private double price;

public Product(int id, String productName, double price) {

this.id = id;

this.productName = productName;

this.price = price;

}

public double calculateProfit() {

double profit = 0;

if (price <= 300) {

profit = price * 0.3;

} else if (price <= 500) {

profit = price * 0.2;

} else if (price <= 1000) {

profit = price * 0.1;

}

return profit;

}

}

public class Main {

public static void main(String[] args) {

List<Product> products = new ArrayList<>();

products.add(new Product(1, "特价商品1", 200));

products.add(new Product(2, "特价商品2", 400));

products.add(new Product(3, "特价商品3", 600));

products.add(new Product(4, "特价商品4", 800));

int orderCount1 = 300;

int orderCount2 = 400;

int orderCount3 = 300;

double totalProfit = 0;

for (Product product : products) {

double profit = product.calculateProfit();

if (product.getPrice() <= 300) {

totalProfit += profit * orderCount1;

} else if (product.getPrice() <= 500) {

totalProfit += profit * orderCount2;

} else if (product.getPrice() <= 1000) {

totalProfit += profit * orderCount3;

}

}

System.out.println("总利润:" + totalProfit);

}

}


回答:

大概思路是,在 calculateTotalProfit 中,遍历所有商品,并根据价格范围确定对应的利润百分比(30%、20%、或10%)。然后,根据商品的价格和利润百分比计算单个商品的利润,并累加到 totalProfit 变量中。运行最后的时候,返回计算得到的总利润。代码比较简单,注释差不多都解释了,所以不做过多赘述。

import java.util.ArrayList;

import java.util.List;

class Product {

int id;

String productName;

int price;

public Product(int id, String productName, int price) {

this.id = id;

this.productName = productName;

this.price = price;

}

}

public class ProfitCalculator {

public static void main(String[] args) {

List<Product> products = new ArrayList<>();

// 添加100条商品数据

// 例如:products.add(new Product(1, "特价商品1", 200));

// products.add(new Product(2, "特价商品2", 400));

// ...

int totalProfit = calculateTotalProfit(products);

System.out.println("总利润为:" + totalProfit + "元");

}

public static int calculateTotalProfit(List<Product> products) {

int totalProfit = 0;

for (Product product : products) {

int price = product.price;

int profitPercentage;

// 根据价格范围确定利润百分比

if (price <= 300) {

profitPercentage = 30;

} else if (price <= 500) {

profitPercentage = 20;

} else {

profitPercentage = 10;

}

// 计算单个商品的利润

int profit = price * profitPercentage / 100;

totalProfit += profit;

}

return totalProfit;

}

}


回答:

import java.util.ArrayList;

import java.util.List;

class Product {

int id;

String productName;

double price;

double profit;

public Product(int id, String productName, double price) {

this.id = id;

this.productName = productName;

this.price = price;

this.profit = 0;

}

public void applyMarkup(double percentage) {

double markup = this.price * percentage;

this.profit = markup;

this.price += markup;

}

}

public class ProfitCalculator {

public static void main(String[] args) {

List<Product> products = new ArrayList<>();

// 假设这里已经填充了100条商品数据

for (int i = 1; i <= 100; i++) {

products.add(new Product(i, "特价商品" + i, (i * 10) % 1001));

}

int orderCount1 = 300; // 0-300元的订单数量

int orderCount2 = 400; // 301-500元的订单数量

int orderCount3 = 300; // 501-1000元的订单数量

double totalProfit = 0;

for (Product product : products) {

if (product.price <= 300) {

product.applyMarkup(0.3);

totalProfit += product.profit * orderCount1;

} else if (product.price <= 500) {

product.applyMarkup(0.2);

totalProfit += product.profit * orderCount2;

} else if (product.price <= 1000) {

product.applyMarkup(0.1);

totalProfit += product.profit * orderCount3;

}

}

System.out.println("总利润为: " + totalProfit + "元");

}

}


回答:

import java.util.ArrayList;

import java.util.List;

class Product {

private int id;

private String productName;

private int price;

public Product(int id, String productName, int price) {

this.id = id;

this.productName = productName;

this.price = price;

}

public int getId() {

return id;

}

public String getProductName() {

return productName;

}

public int getPrice() {

return price;

}

}

public class Main {

public static void main(String[] args) {

List<Product> products = new ArrayList<>();

products.add(new Product(1, "特价商品1", 200));

products.add(new Product(2, "特价商品2", 400));

products.add(new Product(3, "特价商品3", 600));

products.add(new Product(4, "特价商品4", 800));

int totalProfit = calculateTotalProfit(products);

System.out.println("总利润:" + totalProfit + "元");

}

public static int calculateTotalProfit(List<Product> products) {

int totalProfit = 0;

for (Product product : products) {

int price = product.getPrice();

int profit;

if (price <= 300) {

profit = (int) (price * 0.3);

} else if (price <= 500) {

profit = (int) (price * 0.2);

} else {

profit = (int) (price * 0.1);

}

totalProfit += profit;

}

return totalProfit;

}

}

用java写的。

以上是 用Java实现以下问题 最好用算法来实现? 的全部内容, 来源链接: utcz.com/p/945290.html

回到顶部