当前位置:网站首页>C # equipment synthesis
C # equipment synthesis
2022-06-30 04:48:00 【Your wig classmate Z】
Is there a problem with the composition of items in the multi tree atlas written below ? Everybody, let's see what's wrong
using System.Collections.Generic;
namespace Test
{
public class MaterialData
{
public ItemData item; // Items required for synthesis
public int count; // The quantity of the item required for synthesis
}
public class ItemData
{
public int id; // goods ID
public int count; // Number of items currently owned
public int costGold; // The gold coin needed to synthesize this item
public List<MaterialData> materialList; // The material needed to synthesize the article
}
public class Exam
{
private static Exam instance = new Exam();
public static Exam Instance
{
get
{
return instance;
}
set
{
value = instance;
}
}
/// <summary>
/// For calculation totalGold Gold coins can be synthesized at most item Quantity of equipment
/// </summary>
/// <param name="item"> Equipment to be synthesized </param>
/// <param name="totalGold"> Gold coins owned </param>
/// <returns> Synthetic item Maximum number of equipment </returns>
public int Run(ItemData item, int totalGold)
{
// Because there is a limit on the number of items you currently own , First of all, we can find out according to the quantity of other items , At present, the maximum number of required items can be synthesized
// And MaterialData Hold... In the library ItemData,ItemData hold MaterialData, So use the return to complete the current number of items
//Todo Get the quantity recursively
int Count = GetMaxCount(item);
// After obtaining the maximum number of items that can be synthesized , To calculate the gold coins needed to synthesize this item
int singlePrice = GetSinglePrice(item);
// After obtaining the price required for synthesis , Use the total number of gold coins to calculate , You can synthesize several items at most
int num = GetMaxNumByPrice(singlePrice, totalGold);
// After calculating the number of items that can be synthesized by the current gold coin , It needs to be compared with the maximum number of items that can be generated in the current inventory , Get the maximum number of items that can be synthesized
int maxNum = GetMaxNum(Count, num);
return maxNum;
}
/// <summary>
/// Get the inventory quantity of this item
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public int GetMaxCount(ItemData item)
{
// The first step is to obtain the amount of material required for the synthesis of the object, i.e ,materialList The number of , According to the amount of materials required for synthesis , To determine how many items can be synthesized at present
// If the currently synthesized item does not need synthetic materials , Directly return the number of items you currently own
if (item.materialList.Count == 0) return item.count;
// If the quantity of synthetic materials required is not equal to 0, It means that the article has one or more synthetic materials
// At this point, you need to traverse the list of required materials to confirm the quantity of synthesis
int materialNum, canSynthesisNum = 0;
foreach (var material in item.materialList)
{
// because materialList Are there in ItemData, So get ItemData The number of
materialNum = GetMaxCount(material.item);
// To calculate the ItemData After the quantity of , What is needed to remove synthetic materials ItemData The number of , You can calculate Material The number of
canSynthesisNum = materialNum / material.count;
}
return canSynthesisNum;
}
/// <summary>
/// Obtaining a single item consumes gold coins
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public int GetSinglePrice(ItemData item)
{
// When calculating the price of a single object , First, determine whether there are objects in the required material list , If there is no object, the unit price of the object will be returned directly costGold
if (item.materialList.Count == 0) return item.costGold;
// If there are synthetic materials , When calculating the price of synthetic items, you need to include the gold coins consumed by synthetic materials , So we still need to calculate , The number of objects that can be synthesized
// because ItemData and materiallist There is interdependence , Yes materiallist Traversal , Recursively calculate the gold coins required for each synthetic material during traversal
int materialPrice = 0;
foreach (var material in item.materialList)
{
// Here you synthesize the required materials , The quantity may be more than one , So here we have to multiply the number of compounds
materialPrice += GetSinglePrice(material.item) * material.count;
}
// When the program runs here , The gold coins consumed by the materials required for the composite object are calculated , But it also costs gold coins to synthesize objects , Here you can add the gold coins consumed by the composite object
materialPrice += item.costGold;
return materialPrice;
}
/// <summary>
/// Get the number of items that can be purchased through the current gold coin , Do not consider the problem of inventory
/// </summary>
/// <param name="price"> Unit price </param>
/// <param name="totalGold"> Total gold </param>
/// <returns></returns>
public int GetMaxNumByPrice(int price, int totalGold)
{
// Here, you also need to judge whether the purchase conditions are met , That is, the total gold coins are greater than 0, Or the unit price of the item should be lower than the total gold coins
if (totalGold <= 0 || price > totalGold) return 0;
return totalGold / price;
}
/// <summary>
/// Get the maximum number of synthetic items
/// </summary>
/// <param name="count"> Inventory quantity </param>
/// <param name="num"> The maximum composite number of gold coins you hold </param>
/// <returns></returns>
public int GetMaxNum(int count, int num)
{
return count <= num ? count : num;
}
}
}
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
ItemData A = new ItemData()
{
id = 1,
count = 2,
costGold = 10,
materialList = new List<MaterialData>()
};
ItemData B = new ItemData()
{
id = 2,
count = 10,
costGold = 20,
materialList = new List<MaterialData>()
};
MaterialData C = new MaterialData
{
item = new ItemData
{
id = 1,
count = 10,
costGold = 10,
materialList = new List<MaterialData>()
},
count = 2
};
B.materialList.Add(C);
int result = Exam.Instance.Run(B, 100);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
边栏推荐
- 破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
- 力扣周赛293题解
- Array of small C
- Autowired注解警告的解决办法
- Qos(Quality of Service)
- Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
- What is SQL injection and how to avoid it?
- [fpga] implementation of IIC read / write EEPROM
- Arrays class
- 【Paper】2019_ Distributed Cooperative Control of a High-speed Train
猜你喜欢

【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter

Threejs实现模拟河流,水面水流,水管水流,海面

Qos(Quality of Service)

File and IO

Singapore parent-child tour, these popular attractions must be arranged

【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV

Network high concurrency

A virtual reality secret room escape adventure, let you see Technology Singapore

Connect to the database and run node JS running database shows that the database is missing

Process architecture and process management
随机推荐
Cheap SSL certificate abroad
Have a heart beating Valentine's day in Singapore
[FPGA] IIC读写EEPROM 的实现
Directory operations and virtual file systems
What is multimodal interaction?
SSL universal domain name certificate
The subsystem implementing transaction persistence in DBMS is ()
Collective system
Using the command line to convert JSON to dart file in fluent
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
Threadlocal
[control] multi agent system summary. 5. system consolidation.
Bean创建流程 与 lazy-init 延迟加载机制原理
Meet in Bangkok for a romantic trip on Valentine's Day
【Paper】2017_ Distributed control for high-speed trains movements
A collection of errors encountered in machine learning with unity
Connect to the database and run node JS running database shows that the database is missing
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
File and IO
Approaching history, introduction to the London Guard Museum