+ Нов въпрос

Hello France , Mid Exam

Здравейте,

Ако може да ми помогнете със задачата... двата zero tests в judje са верни но всичко друго ме е грешно. Разгледах две други решения.. логиката ми е вярна и са почти същите, но все пак... не мога да намеря грешка.

https://pastebin.com/ANvNzzZ6

Create a program that calculates the profit after buying some items and selling them on a higher price. In order to fulfil that, you are going to need certain data - you will receive a collection of items and a budget in the following format:

{type->price|type->price|type->price……|type->price}

{budget}

The prices for each of the types cannot exceed a certain price, which is given bellow:

 Type Maximum Price Clothes 50.00 Shoes 35.00 Accessories 20.50

If a price for a certain item is higher than the maximum price, don’t buy it. Every time you buy an item, you have to reduce the budget with the value of its price. If you don’t have enough money for it, you can’t buy it. Buy as much items as you can.

You have to increase the price of each of the items you have successfully bought with 40%. Print the list with the new prices and the profit you will gain from selling the items. They need exactly 150\$ for tickets for the train, so if their budget after selling the products is enough – print – "Hello, France!" and if not – "Time to go."

Input / Constraints

• On the 1st line you are going to receive the items with their prices in the format described above – real numbers in the range [0.00……1000.00]
• On the 2nd line, you are going to be given the budgeta real number in the range [0.0….1000.0]

Output

• Print the list with the bought item’s new prices, rounded 2 digits after the decimal separator in the following format:

"{price1} {price2} {price3} {price5}………{priceN}"

• Print the profit, rounded 2 digits after the decimal separator in the following format:

"Profit: {profit}"

• If the money for tickets are enough, print: "Hello, France!" and if not – "Time to go."
Тагове:
0
nickwork 17 Точки

Твоят код преправен дава 100/100, дебъгни го няколко пъти и виж разликите.

1.Когато работиш с пари използвай decimal, а не double.

2. Имаше малко излишен код с Math.Round() - в условието никъде не пише да се закръгля.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

{
class Program
{
static void Main(string[] args)
{

List<decimal> newPrices = new List<decimal>();
decimal profit = 0;

for (int i = 0; i < data.Length; i++)
{
string[] itemData = data[i].Split("->");
string type = itemData[0];
decimal price = decimal.Parse(itemData[1]);
decimal maxPrice = 0;

switch (type)
{
case "Clothes":
maxPrice = 50.00M;
break;
case "Shoes":
maxPrice = 35.00M;
break;
case "Accessories":
maxPrice = 20.50M;
break;
}

if (price <= maxPrice && budget >= price)
{
budget -= price;

profit += price * 0.4M;
}
}

budget += newPrices.Sum();
//Console.WriteLine(string.Join(" ", newPrices));

StringBuilder sb = new StringBuilder();
foreach (var price in newPrices)
{
sb.Append(\$"{price:f2}" + " ");
}

Console.WriteLine(sb.ToString().TrimEnd());

Console.WriteLine(\$"Profit: {profit:F2}");
if (budget >= 150)
{
Console.WriteLine("Hello, France!");
}
else
{
Console.WriteLine("Time to go.");
}
}
}
}

0