The third simulation game of the 12th Blue Bridge Cup-add thousands of separators

1. Problem description:

When writing a large integer, in order to make it easier to see the digits, a comma is usually added between the digits to separate the digits. Specifically, from right to left, every three digits are divided into segments, and one is added between adjacent segments. comma. For example, 1234567 is written as 1,234,567. For example, 17179869184 is written as 17,179,869,184. Given an integer, please add the delimiter to this integer and output it.

Input format The
input line contains an integer v.
Output format
Output the integer with the delimiter added.

Sample input
1234567
sample output
1,234,567
sample input
17179869184
sample output
17,179,869,184
Data size and convention
For 50% of the evaluation use cases, 0 <= v <10^9 (10 to the 9th power).
For all evaluation cases, 0 <= v <10^18 (10 to the 18th power).

2. Thinking analysis:

Analyzing the question, you can know that you can first use the input function to receive the string output from the console, and then traverse the string in reverse order, and count through a count variable, so that you can add a comma every three digits

3. The code is as follows:

if __name__ == '__main__':
    s = input()
    count = 1
    res = ""
    for i in range(len(s) - 1, -1, -1):
        res = s[i] + res
        if count % 3 == 0:
            res = "," + res
        count += 1
    # strip()方法去除多余的逗号
    print(res.strip(","))

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115147778