Codeforces 282A Bit++ Solution & Explanation

Difficulty : 800

Problem Description

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).

Input

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.

Output

Print a single integer — the final value of x.

Examples

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

Solution

++ and — can be front of X or behind it, so we need to use contains to check the input string.

If it contains the substring “++”. If it does, it increments a counter (res) by 1; otherwise, it decrements the counter by 1.

C# Solution

Solution1 – Contains

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 Solution

Solution1

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 Solution

Solution1

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 Solution

Solution1

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

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

Conclusion

Now we can live on Bitland without any problem~

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts or clicking ads, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 282A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *