Competitive Programming Python: Repeated sum of digits Error

前端 未结 1 819
無奈伤痛
無奈伤痛 2021-01-20 06:01

Don\'t know It\'s right place to ask this or not. But I was feeling completely stuck. Couldn\'t find better way to solve it.

I am newbie in competitive Programming.

相关标签:
1条回答
  • 2021-01-20 06:44

    You have not implemented the code properly. You are iterating over i from 0 to t. Why?? The methodology goes as follows:

    N = 12345

    Calculate the sum of digits(1+2+3+4+5 = 15).

    Check if it is less than 10. If so, then the current sum is the answer.. If not.. follow the same procedure by setting N = 15

    Here is the code:

    def sum(inp):
        while inp > 9:
            num = inp
            s = 0
            while num > 0:
                s = s + num%10
                num = num/10
            inp = s
    
        return inp
    
    t = int(raw_input())
    for i in range(t):
        n = int(raw_input())
        print sum(n)
    

    Edit: I think you are iterating till t because you have considered t to be the number of testcases. So, inside the for loop, you should take another input for N for each of the testcase. [This is based on the input and output that you have provided in the question]

    Edit-2: I have changed the code a bit. The question asks us to find the repeated sum of t numbers. For that, I have added a loop where we input a number n corresponding to each testcase and find its repeated sum.

    0 讨论(0)
提交回复
热议问题