练习cf1987A. Upload More RAM
题目如下
A. Upload More RAM
Oh no, the ForceCodes servers are running out of memory! Luckily, you can help them out by uploading some of your RAM!
You want to upload 𝑛 GBs of RAM. Every second, you will upload either 0 or 1 GB of RAM. However, there is a restriction on your network speed: in any 𝑘 consecutive seconds, you can upload only at most 1 GB of RAM in total.
Find the minimum number of seconds needed to upload 𝑛 GBs of RAM!
Input
Each test contains multiple test cases. The first line of input contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases. The description of the test cases follows.
The first and only line of each test case contains two integers 𝑛 and 𝑘 (1≤𝑛,𝑘≤100) — the number of GBs that you want to upload and the length of the time window respectively.
Output
For each test case, output a single integer — the minimum number of seconds needed to upload 𝑛 GBs of RAM.
题目大意
在长度为 k 的连续秒内,最多只能上传 1 GB,上传 n GB 至少需要多长时间。
题目分析
当k=1时,每秒上传1GB时时间最短为n;当 k>1时,每次上传1GB都必须间隔k秒,所以需要1 + (n-1)*k 秒。
完整代码
点击查看代码
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, k;
scanf("%d%d", &n, &k);
int sum;
if(k == 1){
sum = n;
}else{
sum = 1 + (n - 1) * k;
}
printf("%d\n", sum);
}
return 0;
}

浙公网安备 33010602011771号