110,533
社区成员
发帖
与我相关
我的任务
分享
class Program
{
private static readonly int _maxLoad = 1000; //单次运载上限
private static readonly int _totals = 3000; //需要被运载总量
private static readonly int _unitDeplete = 1; //运输单位距离消耗食品量
private static readonly int _road = 1000; //全路长度
/// <summary>
/// 求本次实际运载量
/// </summary>
/// <param name="quantity">本次剩余需要运载的量</param>
/// <returns>本次实际运载量</returns>
private static int GetLoad(int quantity)
{
return Math.Min(quantity, _maxLoad);
}
/// <summary>
/// 计算运载过程需要消耗的食品量
/// </summary>
/// <param name="x">运载距离</param>
/// <returns>总消耗量</returns>
private static int GetDepletion(int x)
{
return x * _unitDeplete;
}
/// <summary>
/// 单段路程运载结果
/// </summary>
/// <param name="x">运载的路程</param>
/// <param name="quantity">需要被运载的量</param>
/// <returns>最后运到目的地的量</returns>
private static int PartLoad(int x, int quantity)
{
int target = 0; //运到目的地的量
int origin = quantity; //剩余需被运载量
target = GetLoad(origin) - GetDepletion(x);
origin = origin - GetLoad(origin);
while (GetLoad(origin) > GetDepletion(2 * x)) //只要剩余可运量大于来回路程消耗量,就回头去运输
{
target += (GetLoad(origin) - GetDepletion(2 * x));
origin -= (GetLoad(origin));
}
return target;
}
/// <summary>
/// 计算是指定分段路程下,最终运到目的地的量
/// </summary>
/// <param name="x">分段路程长度</param>
/// <returns>最终到达目的地量</returns>
private static int FinalLoad(int x)
{
int road = _road; //剩余路程
int target = _totals; //运到的量
while (road > x) //只要剩余路程大于单位路程,就继续运输
{
road -= x;
target = PartLoad(x, target);
if (target <= 0) //如果把所有食品都消耗完了,运输失败,没意义继续下去了。
{
break;
}
}
if (target > 0)
{
target = PartLoad(road, target); //运输余下最后一段路程
}
return target > 0 ? target : 0;
}
static void Main(string[] args)
{
int unitRoad = 0; //单位路程
int quantity = 0; //最终运量
for (int i = 1; i <= 1000; i++)
{
int target = FinalLoad(i);
if (target > quantity)
{
quantity = target;
unitRoad = i;
}
}
Console.WriteLine(string.Format("当单位路程为{0}时,最后运载量最大,可达到{1}。", unitRoad, quantity));
Console.ReadKey();
}
}
open System
let maxLoad = 1000
let total = 3000
let unitDeplete = 1
let totalRoad = 1000
let getLoad quantity = Math.Min(quantity, maxLoad)
let getDepletion x = x * unitDeplete
let partLoad x quantity =
let target = ref 0
let origin = ref quantity
target := getLoad !origin - getDepletion x
origin := !origin - getLoad !origin
while getLoad !origin > getDepletion 2 * x do
target := !target + (getLoad !origin - getDepletion 2 * x )
origin := !origin - getLoad !origin
!target
let finalLoad x =
let road = ref totalRoad
let target = ref total
while !road > x && !target > 0 do
road := !road - x
target := partLoad x !target
if !target > 0 then target := partLoad !road !target
Math.Max(!target , 0)
let roadList = [1..1000]
let road = List.maxBy (fun i -> finalLoad(i)) roadList
let quantityList = List.map (fun i -> finalLoad(i)) roadList
let quantity = List.max quantityList
printfn "当单位路程为%A时,运到目的地量最多,为%A。" road quantity
ignore(Console.ReadKey())
using System;
using System.Collections.Generic;
namespace acmtimusru
{
class Program
{
static void Main(string[] args)
{
double[] timesTable = new double[4000];
timesTable[0] = 1;
for (int i = 1; i < timesTable.Length; i++)
timesTable[i] = timesTable[i - 1] + 1.0 / (double)(i * 2 + 1);
string[] inputString = Console.ReadLine().Split(' ');
int total = int.Parse(inputString[0]);
int perTime = int.Parse(inputString[1]);
int indexNum = Array.BinarySearch(timesTable, (double)total / perTime);
if (indexNum < 0)
indexNum = -indexNum - 1;
int spend = (int)((timesTable[indexNum] * perTime - total) * (indexNum * 2 + 1));
Console.WriteLine(perTime * (indexNum + 1) - spend);
}
}
}