Codeforces 282A Bit++ – 翻譯與解答

難易度 : 800

英文原文如下

The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.

The language is that peculiar as it has exactly one variable, called x. Also, there are two operations:

  • Operation ++ increases the value of variable x by 1.
  • Operation — decreases the value of variable x by 1.

A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters “+”, “-“, “X”. Executing a statement means applying the operation it contains.

A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.

You’re given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).

中文翻譯

Bitland 這個地方的經典程式語言是 Bit++,這種語言非常複雜且特殊。

這種語言之所以如此特殊,是因為它只有一個名為 x 的變數。此外,有兩種操作:

  • 操作 ++ 將變數 x 的值增加1
  • 操作 — 將變數 x 的值減少1

Bit++語言中的陳述式是一個序列,由一個操作和一個變數 x 組成。陳述式中沒有空格,也就是說,它只能包含字符 “+”, “-“, “X”。執行一個陳述式意味著執行它所有的操作。

Bit++語言中的程式是一系列陳述,每個陳述都需要執行。執行一個程式意味著執行其中包含的所有陳述。

給定了一個Bit++語言的程式。變數 x 的初始值是0。執行該程式並找到它的最終值(在執行此程式時的變數值)。

輸入

The first line contains a single integer n (1 ≤ n ≤ 150) — the number of statements in the programme.

Next n lines contain a statement each. Each statement contains exactly one operation (++ or –) and exactly one variable x (denoted as letter «X»). Thus, there are no empty statements. The operation and the variable can be written in any order.

第一行包含了一個整數 n (1 ≤ n ≤ 150) – 程式中陳述式的數量。

接下來 n 行各自包含了一句陳述式。每一句陳述式包含了一個操作(++ 或 –)和一個變數 x (以字母 «X»表示)。因此,沒有空的陳述式。另外操作跟變數可以以任何順序撰寫。

輸出

Print a single integer — the final value of x.

印出一個整數 – x 的最終值

範例

Input1
++X
Output1
Input2
X++
–X
Output0

解題思路

看完題目就能很輕鬆的發現,要是使用string的contain就能很輕鬆判斷當行是 ++ 或是 –了

因為一行陳述式中就包含了一個操作以及變數x而已

(雖然看完題目也不是件容易的事情就是了…冏)

C#解決方案

方案1

int n = int.Parse(Console.ReadLine());  //total count of statements
int res = 0;


for(int i=0;i<n;i++)
{
    string tmp = Console.ReadLine();
    if(tmp.Contains("++")){
        res+=1;
    }
    if(tmp.Contains("--")){
        res-=1;
    }
}
Console.WriteLine(res);

結果在隔了兩個月後,寫著這系列的第二題時,回來更新了解答

既然一行中只有 【++】 、【 –】 其中之一,那幹嘛不用 if-else來判斷,根本不用兩個contains啊

方案2

int n = int.Parse(Console.ReadLine());  //total count of statements
int res = 0;


for(int i=0;i<n;i++)
{
    string tmp = Console.ReadLine();
    if(tmp.Contains("++")){
        res+=1;
    }
    else{
        res-=1;
    }
}
Console.WriteLine(res);

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine()); //total count of statements
        int res = 0;

        for (int i = 0; i < n; i++) 
        {
            String tmp = scanner.nextLine();
            if(tmp.contains("++")){
                res+=1;
            }
            else{
                res-=1;
            }
        }

        System.out.println(res);
    }
}

Python3解決方案

方案1

n = int(input())  #total line
res = 0

for i in range(n):
    tmp = input()
    if("++" in tmp):
        res = res+1
        
    else:
        res = res-1
        
print(res)

JavaScript解決方案

方案1

var n = readline();
var res = 0;

for (var i = 0; i < n; i++) {
    var str = readline();
    if(str.includes("++")){
        res++;
    }
    else{
        res--;
    }
}
print(res);

結論

好了,現在我們也可以在Bitland上暢通無阻囉哈哈哈~

(2023.12.27更新 Bitland的故事尚未完結,這個系列的第二題(282B)這裡看 : Codeforces 282B Painting Eggs 蛋的彩繪 )

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

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

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

題目連結 : Problem – 282A – Codeforces

一些其他的文章

發佈留言

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