М
Молодежь
К
Компьютеры-и-электроника
Д
Дом-и-сад
С
Стиль-и-уход-за-собой
П
Праздники-и-традиции
Т
Транспорт
П
Путешествия
С
Семейная-жизнь
Ф
Философия-и-религия
Б
Без категории
М
Мир-работы
Х
Хобби-и-рукоделие
И
Искусство-и-развлечения
В
Взаимоотношения
З
Здоровье
К
Кулинария-и-гостеприимство
Ф
Финансы-и-бизнес
П
Питомцы-и-животные
О
Образование
О
Образование-и-коммуникации
bondarsofia
bondarsofia
05.04.2020 03:57 •  Информатика

Переписать программу с java на pascal, , 50 import java.util.*; import java.io.*; public class sol_ik { private final int unit = 1000; private final int discount_per_unit = 500; private final double max_discount = 0.2; private double gettotalcost(long firstcost, long secondcost, long fullunits) { long couponsum = fullunits * discount_per_unit; double secondcostwithdiscount = secondcost - math.min(max_discount * secondcost, couponsum); return firstcost + secondcostwithdiscount; } long[] solveknapsack(long[] weights, long totalweight) { int maxunits = (int) (totalweight / unit + 1); long[] old = new long[maxunits + 1]; arrays.fill(old, totalweight); old[0] = 0; long[] cur = new long[maxunits + 1]; int n = weights.length; for (int pos = 0; pos < n; pos++) { arrays.fill(cur, totalweight); for (int units = 0; units < = maxunits; units++) { cur[units] = math.min(cur[units], old[units]); int add = (int) weights[pos] / unit; if (units - add > = 0) { cur[units] = math.min(cur[units], old[units - add] + weights[pos]); } } system.arraycopy(cur, 0, old, 0, cur.length); } return old; } public double getsolution(long[] costs) { int n = costs.length; long totalcost = 0; for (int i = 0; i < n; i++) { totalcost += costs[i]; } long[] minforunits = solveknapsack(costs, totalcost); double res = totalcost; long maxunits = totalcost / unit + 1; for (int units = 0; units < = maxunits; units++) { double cur = minforunits[units]; res = math.min(res, gettotalcost(minforunits[units], totalcost - minforunits[units], units)); } return res; } public void solve(scanner in, printwriter out) { int n = in.nextint(); long[] costs = new long[n]; for (int i = 0; i < n; i++) { costs[i] = in.nextint(); } out.printf("%.2f%n", getsolution(costs)); } public static void main(string[] args) { scanner in = new scanner(system.in); printwriter out = new printwriter(system.out); new (in, out); out.flush(); } }

👇
Ответ:
Red4521
Red4521
05.04.2020
// PascalABC.NET 3.3, сборка 1607 от 31.12.2017
// Внимание! Если программа не работает, обновите версию!

const
  cunit=1000;
  DISCOUNT_PER_UNIT=500;
  MAX_DISCOUNT=0.2;

function getTotalCost(firstCost,secondCost,fullUnits:real):real;
begin
  var couponSum:=fullUnits*DISCOUNT_PER_UNIT;
    var secondCostWithDiscount:=
        secondCost-Min(MAX_DISCOUNT*secondCost,couponSum);
    Result:=firstCost+secondCostWithDiscount
end;

function solveKnapsack(weights:array of integer; totalWeight:integer):
    array of integer;
begin
    var maxUnits:=Trunc(totalWeight/cunit+1);
    var old:=ArrFill(maxUnits+1,totalWeight);
    old[0]:=0;
    var cur:=new integer[maxUnits+1];
    var n:=weights.Length;
    for var pos:=0 to n-1 do begin
        cur.Fill(t->totalWeight);
        for var units:=0 to maxUnits do begin
            cur[units]:=Min(cur[units],old[units]);
            var add:=Trunc(weights[pos]/cunit);
            if units-add >= 0 then
                cur[units]:=Min(cur[units],old[units-add]+weights[pos])
            end;
            cur.CopyTo(old,0);
        end;
    Result:=old;    
end;

function getSolution(costs:array of integer):real;
begin
  var n:=costs.Length;
    var totalCost:=0;
    for var i:=0 to n-1 do totalCost+=costs[i];
    var minForUnits:=solveKnapsack(costs,totalCost);
    Result:=totalCost;
    var maxUnits:=Trunc(totalCost/cunit+1);
    for var units:=0 to maxUnits do begin
      var cur:real:=minForUnits[units];
        Result:=Min(Result,getTotalCost(minForUnits[units],totalCost-cur,units))
      end
end;

begin
  Writeln(getSolution(ReadArrInteger(ReadInteger)):0:2)
end.

Пример
15
1131 2764 1249 3885 4971 2526 1506 1919 520 3094 2183 2503 277 2293 4477
30415.40
4,5(94 оценок)
Ответ:
Ilay64
Ilay64
05.04.2020
Скачай нужный файл.Потом перекинь на папку.
Переписать программу с java на pascal, , 50 import java.util.*; import java.io.*; public class sol_i
4,4(34 оценок)
Открыть все ответы
Ответ:
Hamrod
Hamrod
05.04.2020
1. В приведенном коде ошибка. Не хватает ";" в третьей строке снизу.
2. Немного изменим ваш код и получим искомое значение x
Искомое число х = 16293

var
  x, y, a, b, k: integer;

begin
  k:=10000;
  repeat
    x:=k;
    a := 0; b := 0; y := 1;
    while x > 0 do
    begin
      if (x mod 10) mod 2 = 0
        then
        a := a * 10 + x mod 10
      else begin
        y := y * 10;
        b := b * 10 + x mod 10
      end;
      x := x div 10
    end;
    a := a * y + b;
    k := k + 1;
    until a = 26391;
    writeln(a:8, k-1:8); 
end.

 
4,4(66 оценок)
Ответ:
DarKerThanBlaCK34
DarKerThanBlaCK34
05.04.2020

55:5=11

365:11≈33(34) берём "34", чтобы перехлестнуть 365.

Так как 11-ый член является замыкающим (перехлёстывающим 365), тогда смотрим по ближайшим:

34*10=340 (340+34=374) - подходит, но это не максимум!

365 mod 10= 36, а это значит, что последующий член будет больше 365, а именно 395.

В задании сказано, что d должно быть максимальным, а "34" - это допустимое, но не максимально число, удовлетворяющее условию, даже минимальное (по результату).

Проверка:

34*11=374, значит, когда на экране выведется s:=340, n:=50, то, зайдя по следующему циклу, получится, что s:= 374, n:=55. На следующий цикл программа не пойдёт.

ответ: d=34. 

4,5(52 оценок)
Это интересно:
Новые ответы от MOGZ: Информатика
logo
Вход Регистрация
Что ты хочешь узнать?
Спроси Mozg
Открыть лучший ответ