问题描述
在此代码中,我要求用户输入两个整数(Index
,Mindex
),然后显示1..Index
和1..Mindex
之间的所有整数。我的问题是在这里,我不知道如何将Integers
中的Index
和{Mindex中的Integers
的值相乘,然后将这两个乘积相加
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Add is
Index,Mindex : Integer;
procedure calc (Item : in Integer) is
New_Value : Integer;
begin
Put ("The value of the index is Now");
Put (Item);
New_Line;
New_Value := (Item - 1);
if New_Value > 0 then
calc (New_Value);
end if;
end calc;
begin
Get (Index);
Get (Mindex);
calc (Index);
New_Line;
calc (Mindex);
end Add;
解决方法
阶乘保持链乘以每个递减值:5! = 5 * 4 * 3 * 2 * 1 =120。为了进行递归,您需要在递归函数中包含两种情况:如果您的值大于1,则将该值乘以下一个最小值。这是递归部分,您将在其中调用Factorial(N)内部的Factorial(N-1)。否则,仅返回1(因式0在数学上为1,因此1!和0!都等于1)。
在Ada中的工作方式是:
<script>
function increment_quantity(product_id,price) {
var inputQuantityElement = $("#quantity-"+product_id);
var newQuantity = parseInt($(inputQuantityElement).val())+1;
var newPrice = newQuantity * price;
save_to_db(product_id,newQuantity,newPrice);
}
function decrement_quantity(product_id,price) {
var inputQuantityElement = $("#quantity-"+product_id);
if($(inputQuantityElement).val() > 1)
{
var newQuantity = parseInt($(inputQuantityElement).val()) - 1;
var newPrice = newQuantity * price;
save_to_db(product_id,newPrice);
}
}
function numberWithCommas(number) {
var parts = number.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g,",");
return parts.join(".");
}
function save_to_db(product_id,new_quantity,newPrice) {
var inputQuantityElement = $("#quantity-"+product_id);
var priceElement = $("#cart-price-"+product_id);
var form_data = new FormData();
form_data.append('action','cart-qty');
form_data.append('product_id',product_id);
form_data.append('new_quantity',new_quantity);
form_data.append('new_price',newPrice);
$.ajax({
url: 'load.php',dataType: 'json',processData: false,contentType: false,data: form_data,type: 'post',success : function(response) {
$(inputQuantityElement).val(new_quantity);
$(priceElement).attr('data-price',newPrice);
$(priceElement).text("$ "+numberWithCommas(Number(newPrice.toFixed(2))));
var totalQuantity = 0;
$("input[id*='quantity-']").each(function() {
var cart_quantity = $(this).val();
totalQuantity = parseInt(totalQuantity) + parseInt(cart_quantity);
});
$("#ttl-qty").text(totalQuantity);
var totalItemPrice = 0;
$("p[id*='cart-price-']").each(function() {
var cart_price = $(this).attr('data-price').replace("$","");
totalItemPrice = Number(totalItemPrice) + Number(cart_price);
});
$("#sttlprc").text("$ "+numberWithCommas(totalItemPrice.toFixed(2)));
$("#ttlprc").text("$ "+numberWithCommas(totalItemPrice.toFixed(2)));
}
});
}
</script>
然后您可以在每个数字上调用该阶乘函数并添加结果。