Share roman to integer leetcode c++,

bichhai873

New member
..

Số La Mã là một hệ thống các số viết sử dụng các chữ cái từ bảng chữ cái Latin.Hệ thống số La Mã không phải là vị trí, có nghĩa là vị trí của một chữ số không ảnh hưởng đến giá trị của nó.Ví dụ: số 4 được viết là IV, trong khi số 9 được viết là IX.

Chuyển đổi một chữ số La Mã thành một số nguyên là một nhiệm vụ tương đối đơn giản.Bước đầu tiên là xác định các chữ số riêng lẻ trong chữ số La Mã.Khi các chữ số đã được xác định, chúng có thể được chuyển đổi thành các giá trị số nguyên tương ứng của chúng.Cuối cùng, các giá trị số nguyên của các chữ số riêng lẻ có thể được thêm vào với nhau để có được kết quả cuối cùng.

Sau đây là một ví dụ về cách chuyển đổi chữ số La Mã thành một số nguyên trong C ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

int romantoint (chuỗi Roman) {
// Tạo bản đồ các chữ số La Mã cho các giá trị số nguyên tương ứng của chúng.
const map <char, int> romannumerals = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};

// Khởi tạo tổng giá trị thành 0.
int tổng = 0;

// lặp lại chữ số La Mã, bắt đầu từ nhân vật ngoài cùng bên phải.
for (int i = roman.length ()-1; i> = 0; i--) {
// Nhận giá trị số nguyên của ký tự hiện tại.
int value = romannumerals.at (Roman );

// Nếu ký tự tiếp theo có giá trị lớn hơn, hãy trừ giá trị hiện tại từ tổng số.
if (i <roman.length () - 1 && romannumerals.at (roman [i + 1])> value) {
Tổng -= Giá trị;
} khác {
// Nếu không, thêm giá trị hiện tại vào tổng số.
Tổng += giá trị;
}
}

// Trả về tổng giá trị.
trả lại tổng số;
}

int main () {
// Nhận chữ số La Mã từ người dùng.
chuỗi La Mã;
cout << "Nhập một chữ số La Mã:";
cin >> La Mã;

// Chuyển đổi chữ số La Mã thành một số nguyên.
int integer = romantoint (La Mã);

// In giá trị số nguyên.
cout << "Giá trị số nguyên của" << roman << "là" << số nguyên << endl;

trả lại 0;
}
`` `

## Người giới thiệu

* [Số La Mã cho các số nguyên trong C ++] (https://www.tutorialspoint.com/cpp/cpp_roman_numerals.htm)
* [Cách chuyển đổi chữ số La Mã thành số nguyên trong C ++] (geeksforgeek.org - geeksforgeek Resources and Information.)
* [Số La Mã cho các số nguyên trong C ++ với các ví dụ] (https://www.codegrepper.com/code-examples/cpp/roman+numerals+to+Integers+in+C++
=======================================
#RomanToInteger #LeetCode #C++ #CodingInterview #algorithms ## Roman to Integer in C++

Roman numerals are a system of writing numbers that uses letters from the Latin alphabet. The Roman numeral system is not positional, meaning that the position of a numeral does not affect its value. For example, the number 4 is written as IV, while the number 9 is written as IX.

Converting a Roman numeral to an integer is a relatively simple task. The first step is to identify the individual numerals in the Roman numeral. Once the numerals have been identified, they can be converted to their corresponding integer values. Finally, the integer values of the individual numerals can be added together to get the final result.

The following is an example of how to convert a Roman numeral to an integer in C++:

```c++
#include <iostream>

using namespace std;

int romanToInt(string roman) {
// Create a map of Roman numerals to their corresponding integer values.
const map<char, int> romanNumerals = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};

// Initialize the total value to 0.
int total = 0;

// Iterate over the Roman numeral, starting from the rightmost character.
for (int i = roman.length() - 1; i >= 0; i--) {
// Get the integer value of the current character.
int value = romanNumerals.at(roman);

// If the next character has a greater value, subtract the current value from the total.
if (i < roman.length() - 1 && romanNumerals.at(roman[i + 1]) > value) {
total -= value;
} else {
// Otherwise, add the current value to the total.
total += value;
}
}

// Return the total value.
return total;
}

int main() {
// Get the Roman numeral from the user.
string roman;
cout << "Enter a Roman numeral: ";
cin >> roman;

// Convert the Roman numeral to an integer.
int integer = romanToInt(roman);

// Print the integer value.
cout << "The integer value of " << roman << " is " << integer << endl;

return 0;
}
```

## References

* [Roman Numerals to Integers in C++](https://www.tutorialspoint.com/cpp/cpp_roman_numerals.htm)
* [How to Convert Roman Numerals to Integers in C++](https://www.geeksforgeeks.org/convert-roman-numerals-to-integers-in-cpp/)
* [Roman Numerals to Integers in C++ with Examples](https://www.codegrepper.com/code-examples/cpp/roman+numerals+to+integers+in+c+++with+examples)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top