小美正在设计美团外卖的定价信息。已知外卖定价的规则如下:
1. 每道菜有折扣价和原价。折扣价不能超过原价。
2. 订单有满元减元的优惠。当购买的菜的价格总和不小于元时,总价格可以减元。“减”的价格不能超过“满”的价格。
3. 满减优惠和折扣价是互斥的,当且仅当每个菜都选择了原价才可以触发满减。
4. 系统会自动为客户计算最低价格的方案。
请使用等价划分法设计测试用例,来测试该系统的功能。
第一行输入一个正整数,代表菜的总数。
接下来的行,每行输入两个实数和,代表每道菜的原价是,折扣价是。
最后一行输入两个实数和,代表满元可以减元。
数据中所有实数的绝对值不超过1000。
如果数据有误,则输出一行字符串"error"。
否则输出一个小数,小数点后保留2位即可。该小数代表顾客购买了全部菜各一份时,订单的总价格。
2 10 5.5 10 6.5 15 3
12.00
虽然触发了满15元减3元,但使用折扣只需要花12元,低于使用满减的价格(20-3=17),因此最终系统会为客户推荐折扣价。
2 10 5.5 10 6.5 20 10
10.00
触发满20元减10元即可。满减价优于折扣价。
2 10 10.25 10 3.5 20 4.5
error
折扣价高于原价,数据错误。
import java.util.Scanner; public class Main { public static void main(String[] args) { double x = 0; // 原价总数 double y = 0; // 折扣总数 Scanner in = new Scanner(System.in); int menuCount = in.nextInt(); while (menuCount-- >= 1) { double a = in.nextDouble(); // 原价 double b = in.nextDouble(); // 折扣 if (a < b || a <= 0 || b <= 0) { System.out.println("error"); return; } x += a; y += b; } double o = in.nextDouble(); // 满多少 double d = in.nextDouble(); // 减多少 if (o < d || o <= 0 || d <= 0) { System.out.println("error"); return; } double t = 0; t = x >= o ? x - d : x; if (t < y) { System.out.printf("%.2f", t); } else { System.out.printf("%.2f", y); } } }
import sys a = input() n = int(a) t=[] for i in range(n): tt = list(map(float,input().strip().split())) t.append(tt) c = list(map(float,input().strip().split())) def solution(n,t:list, c:list): result = 0 sum_cost1 = 0 sum_cost2 = 0 sum_src = 0 for i in range(n): if t[i][0] <=0&nbs***bsp;t[i][1] <= 0&nbs***bsp;t[i][0] < t[i][1] : return -1 sum_src += t[i][0] sum_cost1 += t[i][1] if c[1] <= 0&nbs***bsp;c[0] < c[1]: return -1 elif sum_src >= c[0]: sum_src -= c[1] sum_cost2 = sum_src return min(sum_cost1, sum_cost2) result = solution(n,t,c) if result == -1: print("error") else: print("%.2f"% result)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); double[] a = new double[n]; double[] b = new double[n]; double sum1 = 0.0; double sum2 = 0.0; boolean flag = true; for(int i=0;i<n;i++){ a[i] = in.nextDouble(); sum1 += a[i]; b[i] = in.nextDouble(); sum2 += b[i]; if(a[i]<b[i] || a[i]<=0 || b[i]<=0){ flag = false; } } double x = in.nextDouble(); double y = in.nextDouble(); if(x<y || x<=0 || y<=0){ flag = false; } if(sum1 >= x){ sum1 -= y; } if(flag){ System.out.println(String.format("%.2f",sum1>sum2?sum2:sum1)); }else{ System.out.println("error"); } } }
#include <iostream> #include <vector> using namespace std; class solution { public: double sumprice(vector<double>&src,vector<double>&cot,int n,double x,double y) { if (x<y||x<=0||y<=0) return -1; double sumsrc=0; double sumcot=0; for (int i=0;i<n;++i) { if (src[i]<cot[i]||src[i]<=0||cot[i]<=0) return -1; sumsrc+=src[i]; sumcot+=cot[i]; } if (sumsrc>=x) sumsrc-=y; return min(sumsrc,sumcot); } }; int main() { int n; cin>>n; vector<double>src(n,0.0); vector<double>cot(n,0.0); for(int i=0;i<n;++i) { cin>>src[i]>>cot[i]; } double x,y; cin>>x>>y; solution solve; double ans; ans=solve.sumprice(src, cot, n, x, y); if (ans<=0) cout<<"error"<<endl; else printf("%.2f",ans); return 0; } // 64 位输出请用 printf("%lld")
def sol(ori,dis,x,y): sumDis = sum(dis) sumOri = sum(ori) if sumOri>=x: return min(sumDis,sumOri-y) else: return sumDis while 1: try: n = int(input()) arr,ori,dis = [],[],[] isContinue = True for i in range(n): temp = list(map(float,input().split())) if temp[0]<temp[1]&nbs***bsp;temp[0]<=0&nbs***bsp;temp[1]<=0: print('error') isContinue = False break arr.append(temp) ori.append(temp[0]) dis.append(temp[1]) x,y = map(float,input().split()) if x<y&nbs***bsp;x<=0&nbs***bsp;y<=0: print('error') isContinue = False break if isContinue: ans = sol(ori,dis,x,y) print("%.2f" % ans) except: break添加一个python代码版本
n = int(input()) price, discount = [], [] for _ in range(n): p, d = map(float, input().split()) if d > p or d <= 0 or p <= 0: print("error") exit() price.append(p) discount.append(d) full, minus = map(float, input().split()) if full <= 0 or minus <= 0 or minus > full: print("error") exit() if sum(price) < full: print("{:.2f}".format(sum(discount))) else: ans = min(sum(price) - minus, sum(discount)) print("{:.2f}".format(ans))
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { const total = await readline(); let discount = []; let priceTotal = 0; //原价和 let discountTotal = 0; // 折扣价和 let manjianTotal = 0; // 满减和 for (let i = 0; i < total; i++) { discount.push((await readline()).split(" ")); if (+discount[i][0] < +discount[i][1] || +discount[i][0] <= 0 || +discount[i][1] <= 0) { console.log("error"); return; } } let manjian = (await readline()).split(" "); if (+manjian[0] < +manjian[1] || +manjian[0] <= 0 || +manjian[1] <= 0) { console.log("error"); return; } if(await readline()) { console.log("error"); return; } discount.forEach(item => { priceTotal += +item[0] discountTotal += +item[1] }) if ((priceTotal - discountTotal > +manjian[1]) || (priceTotal < +manjian[0])) { console.log(getFloat2(discountTotal)) } else { console.log(getFloat2(priceTotal - +manjian[1])) } })(); function getFloat2(x){ if (x != '.'){ var f = Math.round(x * 100) / 100; var s = f.toString(); var rs = s.indexOf('.'); if (rs <= 0) { rs = s.length; s += '.'; } while (s.length <= rs + 2) { s += '0'; } return s; }else{ return '0.00'; } }
#include #include #include using namespace std; class Solution { public: double fun(vector& a, vector& b, int n, float x, float y) { double result = 0, sum_cost1 = 0, sum_cost2 = 0, sum_src = 0; if (x x) return -1; for (int i = 0; i < n; i++) { if (a[i] <= 0 || b[i] <= 0 || a[i] < b[i]) return -1; sum_src += a[i]; sum_cost1 += b[i]; } if (sum_src >= x) sum_src -= y; sum_cost2 = sum_src; return min(sum_cost1, sum_cost2); } }; int main() { int n; scanf("%d", &n); vector a(n, 0), b(n, 0); for (int i = 0; i < n; i++) { scanf("%f %f", &a[i], &b[i]); } float x, y; scanf("%f %f", &x, &y); Solution solution; double result = solution.fun(a, b, n, x, y); if (result == -1) printf("error"); else printf("%.2f", result); return 0; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { // 原价,折扣价 double yuan = 0, zekou = 0; Scanner in = new Scanner(System.in); int num = in.nextInt(); int size = num; double [][]arr = new double[num][2]; while (num * 2 > 0) { double a = in.nextDouble(); double b = in.nextDouble(); // 输入错误处理 if (b > a || b <= 0) { System.out.println("error"); return; } arr[num - 1][0] = a; arr[num - 1][1] = b; num--; } // 计算原价和折扣价 for (int i = 0; i < size; i++) { yuan = yuan + arr[i][0]; zekou = zekou + arr[i][1]; } // 满多少 double man = in.nextDouble(); // 减多少 double jian = in.nextDouble(); // 判断输入是否正确 if (man < 0 || jian <= 0 || jian > man) { System.out.println("error"); return; } if (yuan > man) { yuan = yuan - jian; } System.out.printf("%.2f", yuan > zekou ? zekou : yuan); } }
import sys price = [] count_price = [] number = 0 for i, line in enumerate(sys.stdin): a = line.split() if i == 0: number = int(a[0]) else: if float(a[0]) < float(a[1])&nbs***bsp;float(a[0])<=0&nbs***bsp;float(a[1]) <= 0: # 折扣价大于原价 print("error") exit() price.append(float(a[0])) count_price.append(float(a[1])) total_price1 = sum(price[:-1]) if total_price1 >= price[-1]: total_price1 -= count_price[-1] total_price2 = sum(count_price[:-1]) print("%.2f"%min(total_price1, total_price2))
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { let total = 0; // 记录菜的总价格 let countPrimce = 0; // 读取第一行,获取菜的数量 let numDishes = parseInt(await readline()); for (let i = 0; i < numDishes; i++) { // 读取每一道菜的原价和折扣价 let line = await readline(); let tokens = line.split(" "); let originalPrice = parseFloat(tokens[0]); let discountPrice = parseFloat(tokens[1]); if( originalPrice <= 0 || discountPrice <= 0 ) return console.log('error'); if( originalPrice < discountPrice ) return console.log('error'); // 根据规则计算实际价格 total+=originalPrice;//原价 countPrimce+=discountPrice;// 折扣价 } // 读取满减条件和金额 let line = await readline() const tokens = line.split(" "); let discountThreshold = parseFloat(tokens[0]);//满x let discountAmount = parseFloat(tokens[1]);// 减y if( discountThreshold <= 0 || discountAmount <= 0 ) return console.log('error'); if( discountAmount > discountThreshold ) return console.log('error'); // 处理满减优惠 if (total >= discountThreshold) { total -= discountAmount; // 如果总价格达到满减条件,减去相应的金额 } console.log(Math.min(total,countPrimce).toFixed(2)); })();
void async function () { // Write your code here const n = await readline() let arr = [] for (let i = 0; i < n; i++) { let [a, b] = (await readline()).split(' ').map(Number) if (a < b || b <= 0) { console.log("error") return } arr.push([a, b]) } const[x, y] = (await readline()).split(' ').map(Number) if (x < 0 || y <= 0 || x < y) { console.log("error") return } let m = 0, old = 0 for (let i = 0; i < n; i++) { m += arr[i][1] old += arr[i][0] } old -= old > x ? y : 0 console.log(Math.min(m, old).toFixed(2)) }()