这里是示例:
我们有总工作时间由一名员工负责。小时数是10。
正常限制为8小时。超出此金额的称为超时。
正常工作时间 = 8,加班时间 =2。因此,我们将数字10分为8和2。
这是我的实现方式,请原谅我的posts.filter(({ cat }) => catKey.filter(key => cat.includes(key)).length > 0);
:
<kendo-grid-column field="EffectiveDate" title="Effective Date" filter="date" format="{0:dd/MM/yyyy HH:mm:ss }" [style]="{'text-align': 'left'}"
width="130">
<ng-template kendoGridCellTemplate let-dataItem>
{{ getdateformat(dataItem.EffectiveDate.$date | date:'dd/MM/yyyy HH:mm:ss'}}
</ng-template>
请注意,如果我们想将一个数字分成两个以上的数字-我们最终将拥有多个错综复杂的IF块,并且代码将变得晦涩。
有没有更优雅的方式来处理这个问题?
答案 0 :(得分:4)
您可以通过使用Math.Min
方法来选择一对数字中的较低者,并使用Math.Max
来选择较高者来避免if / else。
set
{
// The actual value or the configured limit, whichever is lower.
dailyStatements[index].Normal = Math.Min(value, _config.NormalLimit);
// The hours over the limit, or 0 if it comes out negative);
dailyStatements[index].Overtime = Math.Max(value - _config.NormalLimit, 0)
}
答案 1 :(得分:1)
如评论中所讨论的,我们可以制作一个工作小时数数组,并为该小时中每个小时的支付率创建另一个数组。请注意,您也可以为此设置一个班级,以在工作时间和付款率之间建立更紧密的联系。
class Solution {
static void Main() {
int[] working_hours = new int[]{8,2,15};
int[] payment_rate = {100,150,200};
int payment_per_hour = 500;
for(int i=1;i<=15;++i){
System.Console.WriteLine("Payment for " + i + " hour(s) worked: $"+ totalPayment(i,working_hours,payment_rate,payment_per_hour));
}
}
private static int totalPayment(int hours_worked,int []working_hours,int []payment_rate,int payment_per_hour){
int amount = 0;
for(int i=0;i<working_hours.Length;++i){
int curr_slab = Math.Min(hours_worked,working_hours[i]);
amount += curr_slab * (payment_per_hour * payment_rate[i] / 100);
hours_worked -= curr_slab;
if(hours_worked == 0) break;
}
return amount;
}
}
输出:
Payment for 1 hour(s) worked: $500
Payment for 2 hour(s) worked: $1000
Payment for 3 hour(s) worked: $1500
Payment for 4 hour(s) worked: $2000
Payment for 5 hour(s) worked: $2500
Payment for 6 hour(s) worked: $3000
Payment for 7 hour(s) worked: $3500
Payment for 8 hour(s) worked: $4000
Payment for 9 hour(s) worked: $4750
Payment for 10 hour(s) worked: $5500
Payment for 11 hour(s) worked: $6500
Payment for 12 hour(s) worked: $7500
Payment for 13 hour(s) worked: $8500
Payment for 14 hour(s) worked: $9500
Payment for 15 hour(s) worked: $10500
我们采用候选人的工作小时数,然后继续从工作板中减去该小时数,同时还要增加应支付的费用,以尊重特定工作板的工时率。