Home > Net >  A recursive algorithm
A recursive algorithm

Time:11-17

Suppose you have data
Order A:
Smoke: RMB 1200
Wine: RMB 800
The order B:
Daily: 200
Snacks: 900

Given a number n=1000

Results:
Orders need to split A:
A1=order & gt; Smoke: RMB 1000
A2=order & gt; Smoke: RMB 200
Wine: RMB 800
Orders need to split into B:
Order B1=& gt; Daily: 200
=> Snacks: RMB 800

Orders for the rest of the order for 100 yuan shall be transferred to the C B, which is the amount of each order must be less than or equal to 1000 yuan, the computation to the last order, order is less than 1000 is the output

Thought for a few days also don't know how to do, only these points, and bosses solve, thank you very much!

CodePudding user response:

Don't panic, you put your mind draw a pseudo code chart, then tell the cup beside, do you think you understand, basic is 80% complete, then I believe you must can be done

CodePudding user response:

Recursion is not no, but the problem with the simplest for ok, cycle number=all commodities, divided by the units in the order number and take up a maximum of the whole,

CodePudding user response:

Well, SAO operation, true when industrial and commercial, taxation, discipline inspection silly

You explain this to me, why you 9 orders are just equal to 1000

CodePudding user response:

First take the mantissa,

1999299=999999={999, 1}, {998}

The rest had no difficulty
{1000200}={1000}, {1000}, {1000}

CodePudding user response:

Public class Program
{
Public static void Main (string [] args)
{
Var resOrder=new Order ();
Var listOrders=SetOrderData (5);
If (listOrders Count!=0)
{
The foreach (var item in listOrders)
{
ResOrder=Fun (item);
Console. WriteLine ($" final results: [smoke: ${resOrder. Cigarette}, wine: {resOrder. Alcohol}] ");
}
}
}
///& lt; Summary>
///the output
///& lt;/summary>
///& lt; Param name="list" & gt;
///& lt; Returns>
Public static Order Fun (Order Order)
{
Int tempC;
Int tempA;
The Order resOrder=new Order ();
Int no=1;
If (order. Cigarette & gt;=1000 & amp; & Order. Alcohol & gt;
=1000){
for (int i=0; i {
Console. WriteLine (${no} "order/smoke: {1000}");
No +=1;
}
for (int i=0; i {
Console. WriteLine (${no} "order/wine: {1000}");
No +=1;
}
TempC=order. Cigarette % 1000;
TempA=order. Alcohol % 1000;
If (tempC + tempA & gt; 1000)
{
ResOrder=new Order
{
Cigarette=tempC,
Alcohol=tempA
};
ResOrder=Fun (resOrder);
}
The else
{
ResOrder=new Order
{
Cigarette=tempC,
Alcohol=tempA
};
}
}
The else
{
Int x=1000 - order. Cigarette;
If (x & lt; Order. Alcohol)
{
TempC=0;
TempA=order. Alcohol - x;
ResOrder=new Order
{
Cigarette=tempC,
Alcohol=tempA
};
}
}
Return resOrder;
}
///& lt; Summary>
///random order data
///& lt;/summary>
///& lt; Param name="count" & gt; Article number & lt;/param>
///& lt; Returns>
Public static List SetOrderData (int count)
{
Try
{
List The orders=new List (a);
The Random ran=new Random ();
for (int i=0; i {
The orders. The Add (new Order
{
Cigarette=ran. Next (1200, 5000),
Alcohol=ran. Next (1500, 2000)
});
}
Return the orders;
}
The catch (Exception e)
{
String error=e.M essage.
return null;
}
}
}
///& lt; Summary>
///order
///& lt;/summary>
Public class Order
{
///& lt; Summary>
///smoke
///& lt;/summary>
Public int Cigarette {get; The set; }
///& lt; Summary>
///wine
///& lt;/summary>
Public int Alcohol {get; The set; }
}
  •  Tags:  
  • C#
  • Related