每30头母牛,zakat就是一岁的母牛,每40头牛,zakat就是两岁的母牛。
例子:
30-39头牛-> zakat: 1头一岁母牛
40-49头牛-> zakat: 1头两岁母牛
50-59头牛-> zakat: 1头两岁母牛
60-69头牛-> zakat: 2头一岁的奶牛
-> zakat: 3头两岁母牛或4头一岁母牛
说明:
30-39类: 30组合: 30
30-49类: 40人合并: 40人
50-59类: 50合并: 40
60-69类: 60组合: 30 + 30
120-129类:120个组合: 40+40+40或30+30+30+30
类别很清楚。组合只能使用数字30或40。在此基础上,计算了zakat的组合。
#include <iostream>
#include <cmath>
int main()
{
int cows=67;
int category=cows-cows%10;
if(30==category)
std::cout<<"1 cow of one years old";
if(40==category)
std::cout<<"1 female cow of two years old";
if(50==category)
std::cout<<"1 female cow of two years old";
if(30+30==category)
std::cout<<"2 cows of one years old";
if(30+30+30+30==category||40+40+40==category)
std::cout<<"4 cows of one years old OR 3 female cows of two years old";
return 0;
}你知不知道如何做算法,使组合,将工作的巨大数字?程序应该根据组合打印zakat。
发布于 2022-04-30 12:33:48
考虑一些牛,N。
两年生母牛(简称V2)的最大数量是地板(N/40)。因此,您的zakat可能有一些从0到地板(N/40)的V2,这使得N-40*V2母牛不穿衣服。这显然需要地板((N-40*V2)/30) V1来完成zakat。
伪码:
N := number of cows
MV2 := floor(N/40)
out 'Possible zakat due:'
loop i from 0 to MV2:
V1 := floor((N-40*i)/30)
out i, ' female cows of 2 years, ', V1, ' cows of 1 year'
end当有多个组合时,您可以将算法复杂化,以确保,只有最小的不带头饰的奶牛才能接受。
例如,如果您有60头奶牛,基本算法将从0到1头两年的奶牛产量:
V2=0, 60 cows remain, V1=2, untithed = 0
V2=1, 20 cows remain, V1=0, untithed = 20在这种情况下,(1,0)解决方案是一个欺骗,必须丢弃。有112头牛:
V2=0, 112 cows remain, V1=3, untithed = 22
V2=1, 72 cows remain, V1=2, untithed = 12
V2=2, 32 cows remain, V1=1, untithed = 2这里,最接近的解决方案是2年的2头母牛,1头1年的母牛。
在C中:
#include <math.h>
#include <stdio.h>
int main() {
int cows;
printf("Enter number of cows: ");
scanf("%d", &cows);
int category = cows - cows % 10;
int N, v2, mx, b1, b2, un, i;
for (N = 0; N <= cows; N += 10) {
v2 = floor(N / 40);
mx = 40;
b2 = 0;
b1 = 0;
for (i = 0; i <= v2; i++) {
int j = floor((N - 40 * i) / 30);
un = N - 40 * i - 30 * j;
if (un <= mx) {
b2 = i;
b1 = j;
mx = un;
}
}
un = N - 40 * b2 - 30 * b1;
if (N == category) {
printf("For %d cows, the zakat is:\n", cows);
if (cows < 30)
printf("0 cows");
if (b1)
printf("%d one years old cows\n", b1);
if (b2)
printf("%d two years old female cows\n", b2);
}
}
}产出:
Enter number of cows: 140
For 140 cows, the zakat is:
2 one years old cows
2 two years old female cowshttps://stackoverflow.com/questions/72065710
复制相似问题