TopCoder 350 points 22-SRM 154 DIV 2 246.20/300 82.07%

Problem Statement

 Margin is defined as the percentage of the selling price of an item or group of items which is profit. For example, if an item costs $80 and is sold for $100, then there is $20 profit, or 20% margin.

You will be given a String[], items, which is all of the items sold in a single transaction. Each String in items will be formatted as follows: "nnn.nn nnn.nn" (quotes for clarity), where each n is a digit between '0' and '9' inclusive. Each String will be exactly 13 characters in length. The first number listed is the price the customer paid for the item. The second number is what the cost to the store of the item was.

You will create a class ProfitCalculator with a method percent which will calculate the percentage of margin on the transaction and return it as an int, rounded down to the greatest integer less than the actual value.

For example, let's say you were given the following String[]:
   { "012.99 008.73",
     "099.99 050.00",
     "123.45 101.07" }
The total cost is $159.80. The total price is $236.43. That means $76.63 was made on this sale. This would be a 32.41128% margin. Since we are rounding down, you would return 32.

Definition

 
Class:ProfitCalculator
Method:percent
Parameters:String[]
Returns:int
Method signature:int percent(String[] items)
(be sure your method is public)
 
 

Notes

-The cost for some items may be greater than the price they was sold for. However, the sum of the prices of all the items in the transaction will be greater than the sum of the costs of all items.

Constraints

-items will contain between 1 and 50 elements, inclusive.
-Every String in items will be exactly 13 characters in length.
-Every String in items will be formatted as "nnn.nn nnn.nn" where each n is a digit between '0' and '9' inclusive, thus the range for each price will be between "000.00" and "999.99" inclusive (all quotes for clarity).
-The total price of all items will be greater than the total cost of all items.
-The total price of all items will be greater than 0.
-The percent of margin, prior to rounding, will not be within 1e-5 of an integer.

Examples

0) 
 
{"012.99 008.73","099.99 050.00","123.45 101.07"}
Returns: 32
This is the example from above.
1) 
 
{"000.00 049.99","999.99 936.22","033.99 025.64","249.99 211.87"}
Returns: 4
The total price is $1283.97. The total cost is $1223.72. The margin prior to rounding is 4.6924772%.
2) 
 
{"822.77 704.86","829.42 355.45","887.18 949.38"}
Returns: 20
The margin prior to rounding is 20.8587169%.
3) 
 
{"612.72 941.34","576.46 182.66","787.41 524.70","637.96 333.23","345.01 219.69",
 "567.22 104.77","673.02 885.77"}
Returns: 23
The margin prior to rounding is 23.9925711%.
4) 
 
{"811.22 275.32","433.89 006.48","141.28 967.41","344.47 786.23","897.47 860.61",
 "007.42 559.29","255.72 460.00","419.35 931.19","419.25 490.52","199.78 114.44",
 "505.63 276.58","720.96 735.00","719.90 824.46","816.58 195.94","498.68 453.05",
 "399.48 921.39","930.88 017.63","422.20 075.39","380.22 917.27","630.83 995.87",
 "821.07 126.87","715.73 985.62","246.23 134.64","168.28 550.33","707.28 046.72",
 "117.76 281.87","595.43 410.45","345.28 532.42","554.24 264.34","195.73 814.87",
 "131.98 041.28","595.13 939.47","576.04 107.70","716.00 404.75","524.24 029.96",
 "673.49 070.97","288.09 849.43","616.34 236.34","401.96 316.33","371.18 014.27",
 "809.63 508.33","375.68 290.84","334.66 477.89","689.54 526.35","084.77 316.51",
 "304.76 015.91","460.63 636.56","357.84 436.20","752.24 047.64","922.10 573.12"}
Returns: 10
The margin prior to rounding is 10.4737522%.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


public class ProfitCalculator {



	public  int percent(String[] items) {
		int len = items.length;
		int n;
		double price = 0;
		double cost = 0;
		String t[];

		for (int i = 0; i < len; i++) {
			t = items[i].split(" ");
			price += Double.parseDouble(t[0]);
			cost += Double.parseDouble(t[1]);
		}

		n = (int) ((price - cost) * 100 / price);
		return n;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值