一个算法
7X+5Y=一个整数(传入的值)我要得到最优化的X与Y,举例说明
例如:如果传入的30,
30 7 7 7 5 5 最终就是X=3 Y=2
传入的是20 ,
20 7 7 7 最终X=3 Y=0
9 5 5 X=0,Y=2
11 5 7 X=1,Y=1
可以这样理解,一个箱子能放7个物件,一个箱子能放5个物件,现在我要根据物件数来分配箱子,
得到最优的分配方式
不知道这样描述,大家能否了解我的意思
算法
[解决办法]
意思就是找满足7X+5Y的值比传入的N大,但是超过量最小的X和Y?
[解决办法]
class Program
{
static void Main(string[] args)
{
int X , Y ;
int t1;
int t2;
int t1_0 = 0;
int t2_0 = 0;
int z = Int32.Parse(Console.ReadLine());
if (z > 14)
{
for (X = 0; (X-1) * 7 <= z; X++)
{
for (Y = 0; (Y-1) * 5 <= z; Y++)
{
if (z <= X * 7 + Y * 5 - 1)
{
t1 = X + Y;
t2 = 7 * X + 5 * Y;
if (t1_0 == 0) { t1_0 = t1; }
else if (t1 < t1_0) { t1_0 = t1; }
if (t2_0 == 0) { t2_0 = t2; }
else if (t2 < t2_0) { t2_0 = t2; }
}
}
}
Console.Out.WriteLine(t1_0);
Console.Out.WriteLine(t2_0);
X = (t2_0 - 5 * t1_0) / 2;
Y = t1_0 - X;
Console.Out.WriteLine("X = " + X + "Y =" + Y);
}
else if(z<=14)
{
X = 0; Y = 0;
if (z <= 5) { X = 0; Y = 1; }
else if (z >= 6 && z <= 7) { X = 1; Y = 0; }
else if (z >= 8 && z <= 10) { X = 0; Y = 2; }
else if (z >= 11 && z <= 12) { X = 1; Y = 1; }
else if (z >= 13 && z <= 14) { X = 2; Y = 0; }
Console.Out.WriteLine("X = " + X + "Y =" + Y);
}
Console.ReadLine();
}
}
class Program
{
static int x=0,y=0;
static void Main(string[] args)
{
string stra = Console.ReadLine();
int n = Convert.ToInt32(stra);
function(n);
Console.WriteLine("{0},{1}", x, y);
Console.ReadLine();
}
public static void function(int temp)
{
x = temp / 7; //保证x+y最小
temp = temp - (7 * x);
if (temp > 5) { x++; }
else if (temp > 3) { y++; }
else if (temp > 0) //保证y最大
{
y++;
if (x > 0) { y++; x--; }
}
}
}
total-30: box1(7)-4; box2(5)-1
30: 7 7 7 7 5
total-30: box1(7)-4; box2(6)-1
30: 7 7 7 7 6
total-20: box1(7)-3; box2(5)-0
20: 7 7 7
total-20: box1(6)-3; box2(5)-1
20: 6 6 6 5
Press any key to continue
int x=Math.Ceil(n/7);
int m=0;
for(int i=x;i>=0;i--)
{
if(7*x+5*m>=n)
{
m++;
}
else
{
return;
}
}