Codeforces 71B Progress Bar 進度條

難易度 : 1300

英文原文如下

A progress bar is an element of graphical interface that displays the progress of a process for this very moment before it is completed. Let’s take a look at the following form of such a bar.

A bar is represented as n squares, located in line. To add clarity, let’s number them with positive integers from 1 to n from the left to the right. Each square has saturation (ai for the i-th square), which is measured by an integer from 0 to k. When the bar for some i (1 ≤ i ≤ n) is displayed, squares 1, 2, … , i - 1 has the saturation k, squares i + 1, i + 2, … , n has the saturation 0, and the saturation of the square i can have any value from 0 to k.

So some first squares of the progress bar always have the saturation k. Some last squares always have the saturation 0. And there is no more than one square that has the saturation different from 0 and k.

The degree of the process’s completion is measured in percents. Let the process be t% completed. Then the following inequation is fulfilled:

An example of such a bar can be seen on the picture.

For the given nkt determine the measures of saturation for all the squares ai of the progress bar.

中文翻譯

進度條是圖形介面中,用來在完成前顯示某個過程在目前時間點之前的進度。現在讓我們來看一下這樣一個進度條的形式。

這樣的進度條被表示為 n 個方塊,位於同一條線上。為了更清晰,讓我們用整數由左到右為它們編號,範圍從 1 到 n。每個方塊有飽和度(第 i 個方塊的 ai),其值介於 0 到 k 之間的整數。當顯示某個 i(1 ≤ i ≤ n)的進度條時,方塊 1, 2, …, i-1 的飽和度為 k,方塊 i+1, i+2, …, n 的飽和度為 0,而方塊 i 的飽和度可以是介於 0 到 k 之間的任意值。

意思就是,前幾個方塊都會為 k,而後幾個都會為 0,且最多只有一塊方塊的數值會介於 0 與 k 之間。

而整個進度的程度是以百分比來顯示的,要是已經完成了 t%,則接下來的不等式成立 :

這邊可以看到一個翻例的圖片 :

對於給定的 n、kt,確定進度條所有方塊的飽和度 ai 。

輸入

We are given 3 space-separated integers nkt (1 ≤ n, k ≤ 100, 0 ≤ t ≤ 100).

給予3個用空白間隔開的整數 nkt (1 ≤ n, k ≤ 100, 0 ≤ t ≤ 100)

輸出

Print n numbers. The i-th of them should be equal to ai.

印出 n 個數字,第 i 個應該要等於 ai

範例

輸入10 10 54
輸出10 10 10 10 10 4 0 0 0 0
輸入11 13 37
輸出13 13 13 13 0 0 0 0 0 0 0

解題思路

說起來,當初一看到這題的說明,我差點暈倒,字多的跟什麼一樣啊…

還好其實題目本身不複雜,簡單來說

  • n : 方塊數
  • k : 方塊數值的最大值
  • t : 整體進度 (%)

然後我們的目標是要將進度條圖形化出來

所以以範例2為例:

11個方塊,方塊最大值為13,目前進度為 37%

故整體數值為 11*13 = 143 (100%的話),那37%就是 11*13*0.37 = 52.91

然後根據公式來看,是不會進位的,所以數值應該為 52

故可以得到前4個為 13(填滿),其他為 0 的答案。

C#解決方案

方案1

string[] first = Console.ReadLine().Split(' '); //split by space
int n = int.Parse(first[0]);
int k = int.Parse(first[1]);
int t = int.Parse(first[2]);

int res = n*k*t/100;
int full = res/k;
int left = res%k;

int[]resArr = new int[n];

for(int i=0; i<n; i++){
    resArr[i] = i<full ? k : (i==full ? left : 0);
}

Console.WriteLine(String.Join(" ", resArr));

利用了 C# 以及Java 整數計算會自動捨去的原理,就不需要math.floor去取得整數了

然後使用了一個簡單的三元不等式去對答案的陣列進行填充。

Java解決方案

方案1

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] first  = scanner.nextLine().split(" ");
        
        int n = Integer.parseInt(first[0]);
        int k = Integer.parseInt(first[1]);
        int t = Integer.parseInt(first[2]); 
        
        int res = n*k*t/100;
        int full = res/k;
        int left = res%k;
        
        int[]resArr = new int[n];

        for(int i=0; i<n; i++){
            resArr[i] = i<full ? k : (i==full ? left : 0);
        }
        System.out.println(String.join(" ", Arrays.stream(resArr).mapToObj(String::valueOf).toArray(String[]::new)));
    }
}

結論

又是考驗著英文能力的一題~

這一題就是出自於這個系列的第二題 – Codeforces Beta Round 65 (Div. 2)

第一題之前寫過解析了,可以在這裡找到 ➡ Codeforces 71A 過長的單字 – 翻譯與解答

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 71B – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *