完成任务 我需要实现函数Change,它将取值并将其分成硬币列表中的可能组合(随机列表) 例如:
DatePickerDialog.OnDateSetListener beginDate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,monthOfYear);
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
setBeginDateOnView();
}
};
public void beginDateOnClick(View view) {
new DatePickerDialog(CreateEventActivity.this,beginDate,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
}
public void setBeginDateOnView() {
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
edtBeginDate.setText(sdf.format(c.getTime()));
这就是我所做的:
coins = [2,3,7]
GHCi> change 7
[[2,2,3],[2,3,2],[3,2,2],[7]]
但这段代码很慢。帮助更快地完成此程序。作为工作的一部分,据说这项任务很容易在生成器列表和递归的帮助下完成。提前感谢您的帮助。
答案 0 :(得分:1)
import Data.List
change :: [Int] -> Int -> [[Int]]
change _ 0 = []
change coins n = do
x <- [c | c <- coins, c <= n]
if x == n
then return [x]
else do
xs <- change coins (n - x)
-- if (null xs)
-- then return [x]
-- else if x < (head xs)
-- then []
-- else return (x:xs)
return (x:xs)
change' :: Int -> [[Int]]
change' = change [2,3,7]
test7 = change' 7
test6 = change' 6
test5 = change' 5
test4 = change' 4
答案 1 :(得分:0)
您正在进行大量过滤,elem
等等,并对数据类型施加了很多限制。
将此视为一个动态问题,您经常需要弄清楚有多少种方式可以返回total
金额的变化。
找到特定硬币的可能性后,您可以将其从列表中删除。
这是我提出的解决方案,包含在一个功能中。
在列表理解中,请注意我为remaining
变量赋值,并且这些值的范围为[0,total],每x
跳转,其中x
是面额。
例如,如果您必须计算$ 0.25总计$ 2的次数,那么该列表理解最终会:
[countChange 2, countChange 1.75,countChange 1.5, countChange 1.25,...]
,但countChange
的下一次迭代也不包括0.25硬币 - 因为我们只是测试了#34;这一点。
-- Amount to return -> List of Coin denominations available
countChange :: Integer -> [Integer] -> Integer
countChange _ [] = 0 -- No coins at all, so no change can be given
countChange 0 _ = 1 -- Only one way to return 0 change
countChange total (x:xs) = sum [countChange (total-remaining) xs | remaining <- [0,x..total]]
答案 2 :(得分:0)
使用MemoCombinators。这很快! PLS。尝试改变100
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string1 = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string1];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string1];
UILabel *label1 = (UILabel*)[cell.contentView viewWithTag:401];
CGFloat widthLabelHeader = CGRectGetWidth(orderTable.frame)/7;
if(!label1)
{
label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 0, widthLabelHeader, 45);
label1.layer.borderWidth = 0.5;
label1.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label1];
}
label1.textAlignment = NSTextAlignmentCenter;
UILabel *label2 = (UILabel*)[cell.contentView viewWithTag:402];
if(!label2)
{
label2 = [[UILabel alloc]init];
label2.frame = CGRectMake(CGRectGetMaxX(label1.frame), 0, widthLabelHeader, 45);
label2.layer.borderWidth = 0.5;
label2.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label2];
}
label2.textAlignment = NSTextAlignmentCenter;
UILabel *label3 = (UILabel*)[cell.contentView viewWithTag:403];
if(!label3)
{
label3 = [[UILabel alloc]init];
label3.frame = CGRectMake(CGRectGetMaxX(label2.frame), 0, widthLabelHeader, 45);
label3.layer.borderWidth = 0.5;
label3.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label3];
}
label3.textAlignment = NSTextAlignmentCenter;
UILabel *label4 = (UILabel*)[cell.contentView viewWithTag:404];
if(!label4)
{
label4 = [[UILabel alloc]init];
label4.frame = CGRectMake(CGRectGetMaxX(label3.frame), 0, widthLabelHeader, 45);
label4.layer.borderWidth = 0.5;
label4.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label4];
}
label4.textAlignment = NSTextAlignmentCenter;
UILabel *label5 = (UILabel*)[cell.contentView viewWithTag:405];
if(!label5)
{
label5 = [[UILabel alloc]init];
label5.frame = CGRectMake(CGRectGetMaxX(label4.frame), 0, widthLabelHeader, 45);
label5.layer.borderWidth = 0.5;
label5.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label5];
}
label5.textAlignment = NSTextAlignmentCenter;
UILabel *label6 = (UILabel*)[cell.contentView viewWithTag:406];
if(!label6)
{
label6 = [[UILabel alloc]init];
label6.frame = CGRectMake(CGRectGetMaxX(label5.frame), 0, widthLabelHeader, 45);
label6.layer.borderWidth = 0.5;
label6.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label6];
}
label6.textAlignment = NSTextAlignmentCenter;
UILabel *label7 = (UILabel*)[cell.contentView viewWithTag:407];
if(!label7)
{
label7 = [[UILabel alloc]init];
label7.frame = CGRectMake(CGRectGetMaxX(label6.frame), 0, widthLabelHeader, 45);
label7.layer.borderWidth = 0.5;
label7.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label7];
}
label7.textAlignment = NSTextAlignmentCenter;
dict1 = [array1 objectAtIndex:indexPath.row];
if( dict1)
{
label1.text = [dict1 objectForKey:@"key1" ];
label1.backgroundColor = [UIColor lightGrayColor];
label2.text = [dict1 objectForKey:@"key2" ];
label2.backgroundColor = [UIColor lightGrayColor];
label3.text = [dict1 objectForKey:@"key3" ];
label3.backgroundColor = [UIColor lightGrayColor];
label4.text = [dict1 objectForKey:@"key4" ];
label4.backgroundColor = [UIColor lightGrayColor];
label5.text = [dict1 objectForKey:@"key5" ];
label5.backgroundColor = [UIColor lightGrayColor];
label6.text = [dict1 objectForKey:@"key6" ];
label6.backgroundColor = [UIColor lightGrayColor];
label7.text = [dict1 objectForKey:@"key7" ];
label7.backgroundColor = [UIColor lightGrayColor];
}}
return cell;
}