如何正确计算分配的内存
How to correctly count allocated memory
我有那个任务:
Assume that in addition to the scaling factor, there is a factor responsible for deallocating unnecessary memory after removing elements from the list. You will be given two factors: one for increasing and one for reducing the allocated memory. Following that will be queries for abstract addition and removal of clusters of elements.
Your task is to return the allocated memory for each 'count' query at that moment.
Multiplication on a fraction is rounded up, division by a fraction is rounded down.
The initially allocated memory is 2.
Input: In the first line there's a number of queries and both the scaling (≥1.1) and downscaling (≥1.1) factors.
Following that are queries of these types: add count; remove count; count.
Output: For every 'count' query, write down the amount of allocated memory at the moment.
输入 1:
3 2.0 2.0
加 100
删除 50
计数
输出 1:
64
输入2:
12 1.1 1.1
加 10000
删除 500
计数
增加 255342
删除 255
计数
增加 243
删除 2435
计数
增加 666554
删除 346
计数
不幸的是,我不知道 test2 的输出应该是什么。
我的代码与 test1 一起正常工作,但与 test2 一起失败。
谁能帮我?可能是四舍五入的问题,也可能不是。
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queryNumber = scanner.nextInt();
double upScalingFactor = scanner.nextDouble();
double downScalingFactor = scanner.nextDouble();
int allocatedMemory = 2;
int numOfElementsToStore = 0;
for (int i = 0; i < queryNumber; i++) {
String operationType = scanner.next();
switch (operationType) {
case "add":
int amountToAdd = scanner.nextInt();
numOfElementsToStore += amountToAdd;
while (allocatedMemory <= numOfElementsToStore) {
allocatedMemory = (int) Math.ceil(allocatedMemory * upScalingFactor);
}
break;
case "delete":
int amountToSubtract = scanner.nextInt();
numOfElementsToStore -= amountToSubtract;
while (Math.floor(allocatedMemory / downScalingFactor) >= numOfElementsToStore) {
allocatedMemory = (int) Math.floor(allocatedMemory / downScalingFactor);
}
break;
case "count":
System.out.println(allocatedMemory);
break;
}
}
}
}
您不应该在 while 循环中转换 multiply/divide 结果。并始终使用 Math.floor()
.
为输入2更正输出:
9660
271469
271469
937184
我有那个任务:
Assume that in addition to the scaling factor, there is a factor responsible for deallocating unnecessary memory after removing elements from the list. You will be given two factors: one for increasing and one for reducing the allocated memory. Following that will be queries for abstract addition and removal of clusters of elements. Your task is to return the allocated memory for each 'count' query at that moment.
Multiplication on a fraction is rounded up, division by a fraction is rounded down.
The initially allocated memory is 2.
Input: In the first line there's a number of queries and both the scaling (≥1.1) and downscaling (≥1.1) factors.
Following that are queries of these types: add count; remove count; count.
Output: For every 'count' query, write down the amount of allocated memory at the moment.
输入 1:
3 2.0 2.0
加 100
删除 50
计数
输出 1:
64
输入2:
12 1.1 1.1
加 10000
删除 500
计数
增加 255342
删除 255
计数
增加 243
删除 2435
计数
增加 666554
删除 346
计数
不幸的是,我不知道 test2 的输出应该是什么。 我的代码与 test1 一起正常工作,但与 test2 一起失败。 谁能帮我?可能是四舍五入的问题,也可能不是。
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queryNumber = scanner.nextInt();
double upScalingFactor = scanner.nextDouble();
double downScalingFactor = scanner.nextDouble();
int allocatedMemory = 2;
int numOfElementsToStore = 0;
for (int i = 0; i < queryNumber; i++) {
String operationType = scanner.next();
switch (operationType) {
case "add":
int amountToAdd = scanner.nextInt();
numOfElementsToStore += amountToAdd;
while (allocatedMemory <= numOfElementsToStore) {
allocatedMemory = (int) Math.ceil(allocatedMemory * upScalingFactor);
}
break;
case "delete":
int amountToSubtract = scanner.nextInt();
numOfElementsToStore -= amountToSubtract;
while (Math.floor(allocatedMemory / downScalingFactor) >= numOfElementsToStore) {
allocatedMemory = (int) Math.floor(allocatedMemory / downScalingFactor);
}
break;
case "count":
System.out.println(allocatedMemory);
break;
}
}
}
}
您不应该在 while 循环中转换 multiply/divide 结果。并始终使用 Math.floor()
.
为输入2更正输出:
9660
271469
271469
937184