cvte most money cost just 0

来源:互联网 发布:js遍历set集合对象 编辑:程序博客网 时间:2024/06/07 07:12

整数发常规两个for循环解释
#include <iostream>  
#include<vector>
using namespace std;


struct v
{


int  p;
int  t;
};
int c(int m, const vector<v>& v)
{
int i, j;
int a[65535];
int k = 0;
if (m - v[0].t*v[0].p - v[1].t*v[1].p >= 0)
return v[0].t + v[1].t;
for (i = 0; i <= v[0].t; i++)
for (j = 0; j <= v[1].t; j++)


{
if (abs(m - i*v[0].p - j*v[1].p) < 0.0001)
{
a[k++]= i + j;


}
}
int max = a[0];
for (int i=0; i <= k;i++)
{
if (a[i] > max)
max = a[i];
}
return max;
}
int main()
{
vector<v>  v = { { 3, 56 }, { 6, 1 } };
cout << c(96, v);
}

//适合恰好花完  一点也不剩







//第二题  第一版改进


#include <iostream>  
#include<vector>
using namespace std;


struct v
{


int  p;
int  t;
};
int c(int m, const vector<v>& v)
{
int i, j;
for (i = 0; i <=v[0].t;i++)
for (j = 0; j <=v[1].t; j++)


{
if (abs(m - i*v[0].p - j*v[1].p) < 0.0001)
{
return i + j;
}
}
}
int main()
{
vector<v>  v = { { 3,3},{ 6,1 }};
cout << c(12, v);
}












//第二版改进
#include <iostream>  
#include<vector>
using namespace std;


struct v
{


int  p;
int  t;
};
int c(int m, const vector<v>& v)
{
int i, j;
int a[65535];
int k = 0;
if (m - v[0].t*v[0].p - v[1].t*v[1].p >= 0)
return v[0].t + v[1].t;
for (i = 0; i <= v[0].t; i++)
for (j = 0; j <= v[1].t; j++)


{
if (abs(m - i*v[0].p - j*v[1].p) < 0.0001)
{
a[k++]= i + j;


}
}
int max = a[0];
for (int i=0; i <= k;i++)
{
if (a[i] > max)
max = a[i];
}
return max;
}
int main()
{
vector<v>  v = { { 3, 56 }, { 6, 1 } };
cout << c(96, v);
}
//适合恰好花完  一点也不剩






第三版改进:
//浮点数版本
#include <iostream>  
#include<vector>
using namespace std;


struct v
{


int  p;
int  t;
};
float c(int m, const vector<v>& v)
{
float i, j;
float a[65535];
int k = 0;
if (m - v[0].t*v[0].p - v[1].t*v[1].p >= 0)
return v[0].t + v[1].t;


for (i = 0.0; i <= v[0].t; i = i + 0.01)
for (j = 0.0; j <= v[1].t; j = j + 0.01)


{
if (abs(m - i*v[0].p - j*v[1].p) <= 0.01)
{
a[k++] = i + j;


}
}
float max = a[0];
for (int i = 0; i <= k; i++)
{
if (a[i] > max)
max = a[i];
}
float x = max, y;
int z;
y = x * 100;
z = y;
y = z; y /= 100;
x = y;//这样x=3.14


return x;
}
int main()
{
vector<v>  v = { { 4, 7 }, { 3, 200 } };
printf("%g", c(20, v));


}
//适合恰好花完  一点也不剩


//一定的钱数买最多的货物

0 0