64,678
社区成员
发帖
与我相关
我的任务
分享
# f(x, y, z) = ax+by+cz
def get_steps(result, coints = []):
max_cnt = []
steps = []
for coint in coints:
max_cnt.append((result+coint) / coint)
min_step = []
step = max(max_cnt)
a = coints[0]
b = coints[1]
c = coints[2]
for x in range(max_cnt[0]):
for y in range(max_cnt[1]):
for z in range(max_cnt[2]):
sum = a*x+b*y+c*z
if (sum == result):
steps.append((x,y,z))
if (x+y+z) < step:
min_step = [x, y, z]
step = x+y+z
return min_step
if __name__ == '__main__':
print get_steps(200, [12, 7, 5])
// MoneyCount.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;
vector<int> MoneyKind;
int Total=0;
vector<int> bestSolution; //最优解
int flag=0; //用来判断是否第一次找到可行解
//solution 存有问题的当前解
//total 目前还是需要多少
//x 为增加哪一个硬币
void FindSolution( vector<int> & solution, int total, int x )
{
if( total == x ) //找到了一个可行解
{
if( flag == 0 )
{
bestSolution = solution;
flag = 1;
}
else
{
if( solution.size() < bestSolution.size() )
bestSolution = solution;
}
return;
}
//搜索每一个子节点
total = total - x;
for( int i=0; i<(int)MoneyKind.size(); i++ )
{
solution.push_back( MoneyKind[i] );
if( MoneyKind[i]<=total ) //可行性约束
{
if( flag==1 && solution.size() <= bestSolution.size() )
FindSolution( solution, total ,MoneyKind[i] );
if( flag==0) //没有找到可行解
FindSolution( solution, total ,MoneyKind[i] );
}
solution.pop_back();
}
}
void input()
{
int numKinds=0;
cout << "How many kinds of coins?\n";
cin >> numKinds;
//MoneyKind.resize( numKinds );
cout << "Input all kinds of coins:\n";
for( int i=0; i<numKinds; i++)
{
int m=0;
cin >> m;
MoneyKind.push_back(m);
}
// sort(MoneyKind.begin(), MoneyKind.end());
cout << "Input the total money of coins:\n";
cin >> Total;
}
int _tmain(int argc, _TCHAR* argv[])
{
input();
clock_t begin = clock();
vector<int> solution;
for( int i=0; i<(int)MoneyKind.size(); i++ )
{
solution.push_back( MoneyKind[i] );
if( MoneyKind[i]<=Total ) //可行性约束
{
if( flag==1 && solution.size() <= bestSolution.size() )
FindSolution( solution, Total ,MoneyKind[i] );
if( flag==0 ) //没有找到可行解
FindSolution( solution, Total ,MoneyKind[i] );
}
solution.pop_back();
}
if( flag == 0 )
cout << "NO SOLUTION!" <<endl;
else
{
cout << "SOLUTION IS:\n" ;
sort( bestSolution.begin(),bestSolution.end() );
for( int i=0; i<(int)bestSolution.size(); i++)
cout << bestSolution[i] << " ";
cout <<"\nTime used "<< (clock()-begin)/1000.0 << "s" <<endl;
}
return 0;
}
// MoneyCount.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
vector<int> MoneyKind;
int Total=0;
vector<int> bestSolution; //最优解
int flag=0; //用来判断是否第一次找到可行解
//solution 存有问题的当前解
//total 目前还是需要多少
//x 为增加哪一个硬币
void FindSolution( vector<int> & solution, int total, int x )
{
if( total == x ) //找到了一个可行解
{
if( flag == 0 )
{
bestSolution = solution;
flag = 1;
}
else
{
if( solution.size() < bestSolution.size() )
bestSolution = solution;
}
return;
}
//搜索每一个子节点
total = total - x;
if( total > 0 )
{
for( int i=0; i<MoneyKind.size(); i++ )
{
solution.push_back( MoneyKind[i] );
FindSolution( solution, total ,MoneyKind[i] );
solution.pop_back();
}
}
}
void input()
{
int numKinds=0;
cout << "How many kinds of coins?\n";
cin >> numKinds;
//MoneyKind.resize( numKinds );
cout << "Input all kinds of coins:\n";
for( int i=0; i<numKinds; i++)
{
int m=0;
cin >> m;
MoneyKind.push_back(m);
}
cout << "Input the total money of coins:\n";
cin >> Total;
}
int _tmain(int argc, _TCHAR* argv[])
{
input();
vector<int> solution;
for( int i=0; i<MoneyKind.size(); i++ )
{
solution.push_back( MoneyKind[i] );
FindSolution( solution, Total ,MoneyKind[i] );
solution.pop_back();
}
for( int i=0; i<bestSolution.size(); i++)
cout << bestSolution[i] << " ";
return 0;
}
def calc_min_step(coins, expectresult):
calc_steps = []
my_steps = []
calc_steps.append(coins[0])
result = calc_steps[0]
index = 0;
while (index < len(coins)):
result = result + coins[index]
if (result < expectresult):
calc_steps.append(coins[index])
elif(result > expectresult):
result = result - coins[index]
index = index + 1
elif(result == expectresult):
calc_steps.append(coins[index])
if (len(my_steps) == 0):
my_steps = calc_steps[0:]
elif (len(my_steps) > len(calc_steps)):
my_steps = calc_steps[0:]
calc_steps.pop()
result = result - coins[index]
index = index + 1
return my_steps
def getsteps(const_coins, expectresult):
steps=[]
coins = []
for i in range(len(const_coins)):
if (i == 0):
coins = const_coins[0:]
else:
coins.append(coins.pop(0))
temp = calc_min_step(coins,expectresult);
if (len(steps) == 0):
steps = temp
if (len(temp)<len(steps)):
steps = temp
return steps
if __name__ == '__main__':
print getsteps([5, 4, 1], 8)